summaryrefslogtreecommitdiff
path: root/common-user
diff options
context:
space:
mode:
authorRichard Henderson <richard.henderson@linaro.org>2026-05-29 18:12:25 -0700
committerRichard Henderson <richard.henderson@linaro.org>2026-07-10 14:39:43 -0700
commitc56da35d5d963d338158bf7e061cc96c67fafd7f (patch)
tree53d14288d11582b2913074feedac83bf6ef7277e /common-user
parent6fe7a5bba780196f52956dc976744b37f529ad17 (diff)
downloadqemu-c56da35d5d963d338158bf7e061cc96c67fafd7f.tar.gz
qemu-c56da35d5d963d338158bf7e061cc96c67fafd7f.zip
common-user: Move mmap_min_addr from linux-user
Introduce user/mmap-min-addr.h. Initialize the variable from a constructor instead of main. Reviewed-by: Helge Deller <deller@gmx.de> Reviewed-by: Warner Losh <imp@bsdimp.com> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Diffstat (limited to 'common-user')
-rw-r--r--common-user/meson.build1
-rw-r--r--common-user/mmap-min-addr.c37
2 files changed, 38 insertions, 0 deletions
diff --git a/common-user/meson.build b/common-user/meson.build
index ac9de5b9e3..f9e2e83f9a 100644
--- a/common-user/meson.build
+++ b/common-user/meson.build
@@ -5,6 +5,7 @@ endif
common_user_inc += include_directories('host/' / host_arch)
user_ss.add(files(
+ 'mmap-min-addr.c',
'safe-syscall.S',
'safe-syscall-error.c',
))
diff --git a/common-user/mmap-min-addr.c b/common-user/mmap-min-addr.c
new file mode 100644
index 0000000000..b2b3762386
--- /dev/null
+++ b/common-user/mmap-min-addr.c
@@ -0,0 +1,37 @@
+/*
+ * Utility function to get the minimum mmap address.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "user/mmap-min-addr.h"
+
+uintptr_t mmap_min_addr;
+
+static void __attribute__((constructor)) init(void)
+{
+#ifdef __linux__
+ /*
+ * We prefer to not make NULL pointers accessible to QEMU.
+ * If something goes wrong below, fall back to 1 page.
+ */
+ size_t min_addr = qemu_real_host_page_size();
+ /*
+ * Read in mmap_min_addr kernel parameter. This value is used
+ * When loading the ELF image to determine whether guest_base
+ * is needed. It is also used in mmap_find_vma.
+ */
+ FILE *fp = fopen("/proc/sys/vm/mmap_min_addr", "r");
+
+ if (fp) {
+ unsigned long tmp;
+ if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
+ min_addr = MAX(min_addr, tmp);
+ }
+ fclose(fp);
+ }
+ mmap_min_addr = min_addr;
+#else
+# error
+#endif
+}