diff --git a/qemu/include/qemu/mmap-alloc.h b/qemu/include/qemu/mmap-alloc.h index 14fbd3bf..8371afe1 100644 --- a/qemu/include/qemu/mmap-alloc.h +++ b/qemu/include/qemu/mmap-alloc.h @@ -5,6 +5,6 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared); -void qemu_ram_munmap(void *ptr, size_t size); +void qemu_ram_munmap(int fd, void *ptr, size_t size); #endif diff --git a/qemu/util/mmap-alloc.c b/qemu/util/mmap-alloc.c index 0b68602e..974995ef 100644 --- a/qemu/util/mmap-alloc.c +++ b/qemu/util/mmap-alloc.c @@ -20,6 +20,7 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) int flags; int guardfd; size_t offset; + size_t pagesize; size_t total; void *guardptr; void *ptr; @@ -40,7 +41,8 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) * anonymous memory is OK. */ flags = MAP_PRIVATE; - if (fd == -1 || qemu_fd_getpagesize(fd) == getpagesize()) { + pagesize = qemu_fd_getpagesize(fd); + if (fd == -1 || pagesize == getpagesize()) { guardfd = -1; flags |= MAP_ANONYMOUS; } else { @@ -49,6 +51,7 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) } #else guardfd = -1; + pagesize = getpagesize(); flags = MAP_PRIVATE | MAP_ANONYMOUS; #endif guardptr = mmap(0, total, PROT_NONE, flags, guardfd, 0); @@ -60,7 +63,7 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) /* Make sure align is a power of 2 */ assert(is_power_of_2(align)); /* Always align to host page size */ - assert(align >= getpagesize()); + assert(align >= pagesize); flags = MAP_FIXED; flags |= fd == -1 ? MAP_ANONYMOUS : 0; @@ -83,17 +86,24 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared) * a guard page guarding against potential buffer overflows. */ total -= offset; - if (total > size + getpagesize()) { - munmap(ptr + size + getpagesize(), total - size - getpagesize()); + if (total > size + pagesize) { + munmap(ptr + size + pagesize, total - size - pagesize); } return ptr; } -void qemu_ram_munmap(void *ptr, size_t size) +void qemu_ram_munmap(int fd, void *ptr, size_t size) { + size_t pagesize; + if (ptr) { /* Unmap both the RAM block and the guard page */ - munmap(ptr, size + getpagesize()); +#if defined(__powerpc64__) && defined(__linux__) + pagesize = qemu_fd_getpagesize(fd); +#else + pagesize = getpagesize(); +#endif + munmap(ptr, size + pagesize); } } diff --git a/qemu/util/oslib-posix.c b/qemu/util/oslib-posix.c index b97e2aa9..8d289772 100644 --- a/qemu/util/oslib-posix.c +++ b/qemu/util/oslib-posix.c @@ -133,5 +133,5 @@ void qemu_vfree(void *ptr) void qemu_anon_ram_free(void *ptr, size_t size) { - qemu_ram_munmap(ptr, size); + qemu_ram_munmap(-1, ptr, size); }