From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kohei Tokunaga <ktokunaga.mail@gmail.com>
Subject: [PULL 26/30] util: exclude mmap-alloc.c from compilation target on Emscripten
Date: Tue, 6 May 2025 17:29:21 +0200 [thread overview]
Message-ID: <20250506152927.222671-27-pbonzini@redhat.com> (raw)
In-Reply-To: <20250506152927.222671-1-pbonzini@redhat.com>
From: Kohei Tokunaga <ktokunaga.mail@gmail.com>
Emscripten does not support partial unmapping of mmapped memory
regions[1]. This limitation prevents correct implementation of qemu_ram_mmap
and qemu_ram_munmap, which rely on partial unmap behavior.
As a workaround, this commit excludes mmap-alloc.c from the Emscripten
build. Instead, for Emscripten build, this modifies qemu_anon_ram_alloc to
use qemu_memalign in place of qemu_ram_mmap, and disable memory backends
that rely on mmap, such as memory-backend-file and memory-backend-shm.
[1] https://github.com/emscripten-core/emscripten/blob/d4a74336f23214bf3304d9eb0d03966786b30a36/system/lib/libc/emscripten_mmap.c#L61
Signed-off-by: Kohei Tokunaga <ktokunaga.mail@gmail.com>
Link: https://lore.kernel.org/r/76834f933ee4f14eeb5289d21c59d306886e58e9.1745820062.git.ktokunaga.mail@gmail.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
system/memory.c | 2 +-
system/physmem.c | 9 +++++----
util/oslib-posix.c | 28 ++++++++++++++++++++++++++++
backends/meson.build | 6 ++++--
util/meson.build | 4 +++-
5 files changed, 41 insertions(+), 8 deletions(-)
diff --git a/system/memory.c b/system/memory.c
index 71434e7ad02..63b983efcdb 100644
--- a/system/memory.c
+++ b/system/memory.c
@@ -1627,7 +1627,7 @@ bool memory_region_init_resizeable_ram(MemoryRegion *mr,
return true;
}
-#ifdef CONFIG_POSIX
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
bool memory_region_init_ram_from_file(MemoryRegion *mr,
Object *owner,
const char *name,
diff --git a/system/physmem.c b/system/physmem.c
index 16cf557d1a1..4a7a2e5e12b 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -1243,7 +1243,7 @@ long qemu_maxrampagesize(void)
return pagesize;
}
-#ifdef CONFIG_POSIX
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
static int64_t get_file_size(int fd)
{
int64_t size;
@@ -1978,7 +1978,7 @@ out_free:
}
}
-#ifdef CONFIG_POSIX
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, ram_addr_t max_size,
qemu_ram_resize_cb resized, MemoryRegion *mr,
uint32_t ram_flags, int fd, off_t offset,
@@ -2158,7 +2158,8 @@ RAMBlock *qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
assert(!host ^ (ram_flags & RAM_PREALLOC));
assert(max_size >= size);
-#ifdef CONFIG_POSIX /* ignore RAM_SHARED for Windows */
+ /* ignore RAM_SHARED for Windows and emscripten*/
+#if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN)
if (!host) {
if (!share_flags && current_machine->aux_ram_share) {
ram_flags |= RAM_SHARED;
@@ -2255,7 +2256,7 @@ static void reclaim_ramblock(RAMBlock *block)
;
} else if (xen_enabled()) {
xen_invalidate_map_cache_entry(block->host);
-#ifndef _WIN32
+#if !defined(_WIN32) && !defined(EMSCRIPTEN)
} else if (block->fd >= 0) {
qemu_ram_munmap(block->fd, block->host, block->max_length);
close(block->fd);
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index a697c602c60..4ff577e5de6 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -58,6 +58,7 @@
#include <lwp.h>
#endif
+#include "qemu/memalign.h"
#include "qemu/mmap-alloc.h"
#define MAX_MEM_PREALLOC_THREAD_COUNT 16
@@ -210,11 +211,21 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared,
const uint32_t qemu_map_flags = (shared ? QEMU_MAP_SHARED : 0) |
(noreserve ? QEMU_MAP_NORESERVE : 0);
size_t align = QEMU_VMALLOC_ALIGN;
+#ifndef EMSCRIPTEN
void *ptr = qemu_ram_mmap(-1, size, align, qemu_map_flags, 0);
if (ptr == MAP_FAILED) {
return NULL;
}
+#else
+ /*
+ * qemu_ram_mmap is not implemented for Emscripten. Use qemu_memalign
+ * for the anonymous allocation. noreserve is ignored as there is no swap
+ * space on Emscripten, and shared is ignored as there is no other
+ * processes on Emscripten.
+ */
+ void *ptr = qemu_memalign(align, size);
+#endif
if (alignment) {
*alignment = align;
@@ -227,7 +238,16 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared,
void qemu_anon_ram_free(void *ptr, size_t size)
{
trace_qemu_anon_ram_free(ptr, size);
+#ifndef EMSCRIPTEN
qemu_ram_munmap(-1, ptr, size);
+#else
+ /*
+ * qemu_ram_munmap is not implemented for Emscripten and qemu_memalign
+ * was used for the allocation. Use the corresponding freeing function
+ * here.
+ */
+ qemu_vfree(ptr);
+#endif
}
void qemu_socket_set_block(int fd)
@@ -588,7 +608,15 @@ bool qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
{
static gsize initialized;
int ret;
+#ifndef EMSCRIPTEN
size_t hpagesize = qemu_fd_getpagesize(fd);
+#else
+ /*
+ * mmap-alloc.c is excluded from Emscripten build, so qemu_fd_getpagesize
+ * is unavailable. Fallback to the lower level implementation.
+ */
+ size_t hpagesize = qemu_real_host_page_size();
+#endif
size_t numpages = DIV_ROUND_UP(sz, hpagesize);
bool use_madv_populate_write;
struct sigaction act;
diff --git a/backends/meson.build b/backends/meson.build
index da714b93d1e..9b88d226851 100644
--- a/backends/meson.build
+++ b/backends/meson.build
@@ -12,8 +12,10 @@ system_ss.add([files(
if host_os != 'windows'
system_ss.add(files('rng-random.c'))
- system_ss.add(files('hostmem-file.c'))
- system_ss.add([files('hostmem-shm.c'), rt])
+ if host_os != 'emscripten'
+ system_ss.add(files('hostmem-file.c'))
+ system_ss.add([files('hostmem-shm.c'), rt])
+ endif
endif
if host_os == 'linux'
system_ss.add(files('hostmem-memfd.c'))
diff --git a/util/meson.build b/util/meson.build
index 780b5977a89..e5cd327e276 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -11,7 +11,9 @@ if host_os != 'windows'
endif
util_ss.add(files('compatfd.c'))
util_ss.add(files('event_notifier-posix.c'))
- util_ss.add(files('mmap-alloc.c'))
+ if host_os != 'emscripten'
+ util_ss.add(files('mmap-alloc.c'))
+ endif
freebsd_dep = []
if host_os == 'freebsd'
freebsd_dep = util
--
2.49.0
next prev parent reply other threads:[~2025-05-06 15:35 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-06 15:28 [PULL 00/30] Rust, wasm changes for 2025-05-06 Paolo Bonzini
2025-05-06 15:28 ` [PULL 01/30] lcitool: use newer Rust for Debian and Ubuntu Paolo Bonzini
2025-09-03 13:54 ` Marc-André Lureau
2025-09-03 13:59 ` Paolo Bonzini
2025-09-04 19:38 ` Marc-André Lureau
2025-09-05 5:16 ` Paolo Bonzini
2025-09-05 5:17 ` Paolo Bonzini
2025-09-05 12:06 ` Alex Bennée
2025-05-06 15:28 ` [PULL 02/30] meson, cargo: require Rust 1.77.0 Paolo Bonzini
2025-05-06 15:28 ` [PULL 03/30] rust: use std::ffi instead of std::os::raw Paolo Bonzini
2025-05-06 15:28 ` [PULL 04/30] rust: let bilge use "let ... else" Paolo Bonzini
2025-05-06 15:29 ` [PULL 05/30] rust: qemu_api_macros: make pattern matching more readable and efficient Paolo Bonzini
2025-05-06 15:29 ` [PULL 06/30] rust: use MaybeUninit::zeroed() in const context Paolo Bonzini
2025-05-06 15:29 ` [PULL 07/30] rust: qom: fix TODO about zeroability of classes Paolo Bonzini
2025-05-06 15:29 ` [PULL 08/30] rust: enable clippy::ptr_cast_constness Paolo Bonzini
2025-05-06 15:29 ` [PULL 09/30] rust: remove offset_of replacement Paolo Bonzini
2025-05-06 15:29 ` [PULL 10/30] rust: replace c_str! with c"" literals Paolo Bonzini
2025-05-06 15:29 ` [PULL 11/30] docs: rust: update for newer minimum supported version Paolo Bonzini
2025-05-06 15:29 ` [PULL 12/30] target/i386/emulate: fix target_ulong format strings Paolo Bonzini
2025-05-06 15:29 ` [PULL 13/30] rust: clippy: enable uninlined_format_args lint Paolo Bonzini
2025-05-06 15:29 ` [PULL 14/30] ci: run RISC-V cross jobs by default Paolo Bonzini
2025-05-06 15:29 ` [PULL 15/30] docs: build-system: fix typo Paolo Bonzini
2025-05-06 15:29 ` [PULL 16/30] target/arm/helper.c: Fix type conflict of GLib function pointers Paolo Bonzini
2025-05-06 15:29 ` [PULL 17/30] target/i386/cpu.c: " Paolo Bonzini
2025-05-06 15:29 ` [PULL 18/30] target/ppc: " Paolo Bonzini
2025-05-06 15:29 ` [PULL 19/30] target/s390x: " Paolo Bonzini
2025-05-06 15:29 ` [PULL 20/30] include/glib-compat.h: Poison g_list_sort and g_slist_sort Paolo Bonzini
2025-05-06 15:29 ` [PULL 21/30] util/cacheflush.c: Update cache flushing mechanism for Emscripten Paolo Bonzini
2025-05-06 15:29 ` [PULL 22/30] block: Add including of ioctl header for Emscripten build Paolo Bonzini
2025-05-06 15:29 ` [PULL 23/30] block: Fix type conflict of the copy_file_range stub Paolo Bonzini
2025-05-06 15:29 ` [PULL 24/30] include/qemu/osdep.h: Add Emscripten-specific OS dependencies Paolo Bonzini
2025-05-06 15:29 ` [PULL 25/30] Disable options unsupported on Emscripten Paolo Bonzini
2025-05-06 15:29 ` Paolo Bonzini [this message]
2025-05-06 15:29 ` [PULL 27/30] util: Add coroutine backend for emscripten Paolo Bonzini
2025-05-06 15:29 ` [PULL 28/30] meson: Add wasm build in build scripts Paolo Bonzini
2025-05-06 15:29 ` [PULL 29/30] tests: Add Dockerfile containing dependencies for Emscripten build Paolo Bonzini
2025-05-06 15:29 ` [PULL 30/30] gitlab: Enable CI for wasm build Paolo Bonzini
2025-05-08 14:26 ` [PULL 00/30] Rust, wasm changes for 2025-05-06 Stefan Hajnoczi
2025-05-08 16:08 ` Paolo Bonzini
2025-05-08 16:53 ` Stefan Hajnoczi
2025-05-08 17:05 ` Thomas Huth
2025-05-08 17:15 ` Paolo Bonzini
2025-05-09 14:11 ` Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250506152927.222671-27-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=ktokunaga.mail@gmail.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).