blob: f71ea038c839ded999464b57a926035baf0eee68 [file] [log] [blame]
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +03001/*
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 */
Markus Armbrustera9c94272016-06-22 19:11:19 +020012
Peter Maydellaafd7582016-01-29 17:49:55 +000013#include "qemu/osdep.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020014#include "qemu/mmap-alloc.h"
Cao jin4a3ecf22016-11-02 21:44:46 +080015#include "qemu/host-utils.h"
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +030016
Michael S. Tsirkin7197fb42015-12-02 21:14:12 +020017#define HUGETLBFS_MAGIC 0x958458f6
18
19#ifdef CONFIG_LINUX
20#include <sys/vfs.h>
21#endif
22
23size_t qemu_fd_getpagesize(int fd)
24{
25#ifdef CONFIG_LINUX
26 struct statfs fs;
27 int ret;
28
29 if (fd != -1) {
30 do {
31 ret = fstatfs(fd, &fs);
32 } while (ret != 0 && errno == EINTR);
33
34 if (ret == 0 && fs.f_type == HUGETLBFS_MAGIC) {
35 return fs.f_bsize;
36 }
37 }
Peter Maydell57d1f6d2017-12-08 16:57:28 +000038#ifdef __sparc__
39 /* SPARC Linux needs greater alignment than the pagesize */
40 return QEMU_VMALLOC_ALIGN;
41#endif
Michael S. Tsirkin7197fb42015-12-02 21:14:12 +020042#endif
43
44 return getpagesize();
45}
46
Alexey Kardashevskiy9c607662017-03-02 13:36:11 +110047size_t qemu_mempath_getpagesize(const char *mem_path)
48{
49#ifdef CONFIG_LINUX
50 struct statfs fs;
51 int ret;
52
David Gibson0de6e2a2018-04-03 14:55:11 +100053 if (mem_path) {
54 do {
55 ret = statfs(mem_path, &fs);
56 } while (ret != 0 && errno == EINTR);
Alexey Kardashevskiy9c607662017-03-02 13:36:11 +110057
David Gibson0de6e2a2018-04-03 14:55:11 +100058 if (ret != 0) {
59 fprintf(stderr, "Couldn't statfs() memory path: %s\n",
60 strerror(errno));
61 exit(1);
62 }
Alexey Kardashevskiy9c607662017-03-02 13:36:11 +110063
David Gibson0de6e2a2018-04-03 14:55:11 +100064 if (fs.f_type == HUGETLBFS_MAGIC) {
65 /* It's hugepage, return the huge page size */
66 return fs.f_bsize;
67 }
Alexey Kardashevskiy9c607662017-03-02 13:36:11 +110068 }
Peter Maydell57d1f6d2017-12-08 16:57:28 +000069#ifdef __sparc__
70 /* SPARC Linux needs greater alignment than the pagesize */
71 return QEMU_VMALLOC_ALIGN;
72#endif
Alexey Kardashevskiy9c607662017-03-02 13:36:11 +110073#endif
74
75 return getpagesize();
76}
77
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +030078void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
79{
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -020080 int flags;
81 int guardfd;
82 size_t offset;
83 size_t total;
84 void *guardptr;
85 void *ptr;
86
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +030087 /*
88 * Note: this always allocates at least one extra page of virtual address
89 * space, even if size is already aligned.
90 */
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -020091 total = size + align;
92
Michael S. Tsirkin7197fb42015-12-02 21:14:12 +020093#if defined(__powerpc64__) && defined(__linux__)
94 /* On ppc64 mappings in the same segment (aka slice) must share the same
95 * page size. Since we will be re-allocating part of this segment
Michael S. Tsirkin097a50d2015-12-03 10:35:31 +020096 * from the supplied fd, we should make sure to use the same page size, to
97 * this end we mmap the supplied fd. In this case, set MAP_NORESERVE to
98 * avoid allocating backing store memory.
99 * We do this unless we are using the system page size, in which case
100 * anonymous memory is OK.
Michael S. Tsirkin7197fb42015-12-02 21:14:12 +0200101 */
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -0200102 flags = MAP_PRIVATE;
103 if (fd == -1 || qemu_fd_getpagesize(fd) == getpagesize()) {
104 guardfd = -1;
105 flags |= MAP_ANONYMOUS;
106 } else {
107 guardfd = fd;
108 flags |= MAP_NORESERVE;
109 }
Michael S. Tsirkin7197fb42015-12-02 21:14:12 +0200110#else
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -0200111 guardfd = -1;
112 flags = MAP_PRIVATE | MAP_ANONYMOUS;
Michael S. Tsirkin7197fb42015-12-02 21:14:12 +0200113#endif
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300114
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -0200115 guardptr = mmap(0, total, PROT_NONE, flags, guardfd, 0);
116
117 if (guardptr == MAP_FAILED) {
Michael S. Tsirkin9d4ec932015-10-25 17:07:45 +0200118 return MAP_FAILED;
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300119 }
120
Cao jin4a3ecf22016-11-02 21:44:46 +0800121 assert(is_power_of_2(align));
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300122 /* Always align to host page size */
123 assert(align >= getpagesize());
124
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -0200125 flags = MAP_FIXED;
126 flags |= fd == -1 ? MAP_ANONYMOUS : 0;
127 flags |= shared ? MAP_SHARED : MAP_PRIVATE;
128 offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
129
130 ptr = mmap(guardptr + offset, size, PROT_READ | PROT_WRITE, flags, fd, 0);
131
132 if (ptr == MAP_FAILED) {
133 munmap(guardptr, total);
Michael S. Tsirkin9d4ec932015-10-25 17:07:45 +0200134 return MAP_FAILED;
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300135 }
136
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300137 if (offset > 0) {
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -0200138 munmap(guardptr, offset);
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300139 }
140
141 /*
142 * Leave a single PROT_NONE page allocated after the RAM block, to serve as
143 * a guard page guarding against potential buffer overflows.
144 */
Cao jin6e4c8902016-11-02 21:44:47 +0800145 total -= offset;
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300146 if (total > size + getpagesize()) {
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -0200147 munmap(ptr + size + getpagesize(), total - size - getpagesize());
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300148 }
149
Murilo Opsfelder Araujo2044c3e2019-01-30 21:36:04 -0200150 return ptr;
Michael S. Tsirkin794e8f32015-09-24 14:41:17 +0300151}
152
153void qemu_ram_munmap(void *ptr, size_t size)
154{
155 if (ptr) {
156 /* Unmap both the RAM block and the guard page */
157 munmap(ptr, size + getpagesize());
158 }
159}