Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 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 | */ |
Markus Armbruster | a9c9427 | 2016-06-22 19:11:19 +0200 | [diff] [blame] | 12 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Markus Armbruster | a9c9427 | 2016-06-22 19:11:19 +0200 | [diff] [blame] | 14 | #include "qemu/mmap-alloc.h" |
Cao jin | 4a3ecf2 | 2016-11-02 21:44:46 +0800 | [diff] [blame] | 15 | #include "qemu/host-utils.h" |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 16 | |
Michael S. Tsirkin | 7197fb4 | 2015-12-02 21:14:12 +0200 | [diff] [blame] | 17 | #define HUGETLBFS_MAGIC 0x958458f6 |
| 18 | |
| 19 | #ifdef CONFIG_LINUX |
| 20 | #include <sys/vfs.h> |
| 21 | #endif |
| 22 | |
| 23 | size_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 | } |
| 38 | #endif |
| 39 | |
| 40 | return getpagesize(); |
| 41 | } |
| 42 | |
Alexey Kardashevskiy | 9c60766 | 2017-03-02 13:36:11 +1100 | [diff] [blame] | 43 | size_t qemu_mempath_getpagesize(const char *mem_path) |
| 44 | { |
| 45 | #ifdef CONFIG_LINUX |
| 46 | struct statfs fs; |
| 47 | int ret; |
| 48 | |
| 49 | do { |
| 50 | ret = statfs(mem_path, &fs); |
| 51 | } while (ret != 0 && errno == EINTR); |
| 52 | |
| 53 | if (ret != 0) { |
| 54 | fprintf(stderr, "Couldn't statfs() memory path: %s\n", |
| 55 | strerror(errno)); |
| 56 | exit(1); |
| 57 | } |
| 58 | |
| 59 | if (fs.f_type == HUGETLBFS_MAGIC) { |
| 60 | /* It's hugepage, return the huge page size */ |
| 61 | return fs.f_bsize; |
| 62 | } |
| 63 | #endif |
| 64 | |
| 65 | return getpagesize(); |
| 66 | } |
| 67 | |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 68 | void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) |
| 69 | { |
| 70 | /* |
| 71 | * Note: this always allocates at least one extra page of virtual address |
| 72 | * space, even if size is already aligned. |
| 73 | */ |
| 74 | size_t total = size + align; |
Michael S. Tsirkin | 7197fb4 | 2015-12-02 21:14:12 +0200 | [diff] [blame] | 75 | #if defined(__powerpc64__) && defined(__linux__) |
| 76 | /* On ppc64 mappings in the same segment (aka slice) must share the same |
| 77 | * page size. Since we will be re-allocating part of this segment |
Michael S. Tsirkin | 097a50d | 2015-12-03 10:35:31 +0200 | [diff] [blame] | 78 | * from the supplied fd, we should make sure to use the same page size, to |
| 79 | * this end we mmap the supplied fd. In this case, set MAP_NORESERVE to |
| 80 | * avoid allocating backing store memory. |
| 81 | * We do this unless we are using the system page size, in which case |
| 82 | * anonymous memory is OK. |
Michael S. Tsirkin | 7197fb4 | 2015-12-02 21:14:12 +0200 | [diff] [blame] | 83 | */ |
| 84 | int anonfd = fd == -1 || qemu_fd_getpagesize(fd) == getpagesize() ? -1 : fd; |
| 85 | int flags = anonfd == -1 ? MAP_ANONYMOUS : MAP_NORESERVE; |
| 86 | void *ptr = mmap(0, total, PROT_NONE, flags | MAP_PRIVATE, anonfd, 0); |
| 87 | #else |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 88 | void *ptr = mmap(0, total, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
Michael S. Tsirkin | 7197fb4 | 2015-12-02 21:14:12 +0200 | [diff] [blame] | 89 | #endif |
Cao jin | 4a3ecf2 | 2016-11-02 21:44:46 +0800 | [diff] [blame] | 90 | size_t offset; |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 91 | void *ptr1; |
| 92 | |
| 93 | if (ptr == MAP_FAILED) { |
Michael S. Tsirkin | 9d4ec93 | 2015-10-25 17:07:45 +0200 | [diff] [blame] | 94 | return MAP_FAILED; |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 95 | } |
| 96 | |
Cao jin | 4a3ecf2 | 2016-11-02 21:44:46 +0800 | [diff] [blame] | 97 | assert(is_power_of_2(align)); |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 98 | /* Always align to host page size */ |
| 99 | assert(align >= getpagesize()); |
| 100 | |
Cao jin | 4a3ecf2 | 2016-11-02 21:44:46 +0800 | [diff] [blame] | 101 | offset = QEMU_ALIGN_UP((uintptr_t)ptr, align) - (uintptr_t)ptr; |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 102 | ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE, |
| 103 | MAP_FIXED | |
| 104 | (fd == -1 ? MAP_ANONYMOUS : 0) | |
| 105 | (shared ? MAP_SHARED : MAP_PRIVATE), |
| 106 | fd, 0); |
| 107 | if (ptr1 == MAP_FAILED) { |
| 108 | munmap(ptr, total); |
Michael S. Tsirkin | 9d4ec93 | 2015-10-25 17:07:45 +0200 | [diff] [blame] | 109 | return MAP_FAILED; |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 110 | } |
| 111 | |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 112 | if (offset > 0) { |
Cao jin | 6e4c890 | 2016-11-02 21:44:47 +0800 | [diff] [blame] | 113 | munmap(ptr, offset); |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Leave a single PROT_NONE page allocated after the RAM block, to serve as |
| 118 | * a guard page guarding against potential buffer overflows. |
| 119 | */ |
Cao jin | 6e4c890 | 2016-11-02 21:44:47 +0800 | [diff] [blame] | 120 | total -= offset; |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 121 | if (total > size + getpagesize()) { |
Cao jin | 6e4c890 | 2016-11-02 21:44:47 +0800 | [diff] [blame] | 122 | munmap(ptr1 + size + getpagesize(), total - size - getpagesize()); |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 123 | } |
| 124 | |
Cao jin | 6e4c890 | 2016-11-02 21:44:47 +0800 | [diff] [blame] | 125 | return ptr1; |
Michael S. Tsirkin | 794e8f3 | 2015-09-24 14:41:17 +0300 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void qemu_ram_munmap(void *ptr, size_t size) |
| 129 | { |
| 130 | if (ptr) { |
| 131 | /* Unmap both the RAM block and the guard page */ |
| 132 | munmap(ptr, size + getpagesize()); |
| 133 | } |
| 134 | } |