From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: imp@bsdimp.com
Subject: [PATCH] bsd-user: Properly allocate guest virtual address space
Date: Thu, 27 Jul 2023 09:11:48 -0700 [thread overview]
Message-ID: <20230727161148.444988-1-richard.henderson@linaro.org> (raw)
Do not hard-code guest_base at 32GB.
Do not override mmap_next_start for reserved_va.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
Hi Warner,
With the blitz-trial branch you provided, the host libc allocates
thread-local storage within the [32GB, 36GB) region that you currently
assume is free.
The armv7-hello program happens to map on top of this thread-local
storage, and then we crash later accessing some host TLS variable.
While the linux-user probe_guest_base is significantly more complex,
we are also trying to handle 32-bit hosts. I think freebsd is always
assuming 64-bit hosts, which makes this simpler.
r~
---
bsd-user/main.c | 37 +++++++++++++++++++------------------
1 file changed, 19 insertions(+), 18 deletions(-)
diff --git a/bsd-user/main.c b/bsd-user/main.c
index f500ec292b..9760aad9f6 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -50,20 +50,8 @@
int do_strace;
-/*
- * Going hand in hand with the va space needed (see below), we need
- * to find a host address to map the guest to. Assume that qemu
- * itself doesn't need memory above 32GB (or that we don't collide
- * with anything interesting). This is selected rather arbitrarily,
- * but seems to produce good results in tests to date.
- */
-# if HOST_LONG_BITS >= 64
-uintptr_t guest_base = 0x800000000ul; /* at 32GB */
-bool have_guest_base = true;
-#else
-uintptr_t guest_base; /* TODO: use sysctl to find big enough hole */
+uintptr_t guest_base;
bool have_guest_base;
-#endif
static bool opt_one_insn_per_tb;
static const char *cpu_model;
static const char *cpu_type;
@@ -522,10 +510,6 @@ int main(int argc, char **argv)
target_environ = envlist_to_environ(envlist, NULL);
envlist_free(envlist);
- if (reserved_va) {
- mmap_next_start = reserved_va + 1;
- }
-
{
Error *err = NULL;
if (seed_optarg != NULL) {
@@ -543,7 +527,24 @@ int main(int argc, char **argv)
* Now that page sizes are configured we can do
* proper page alignment for guest_base.
*/
- guest_base = HOST_PAGE_ALIGN(guest_base);
+ if (have_guest_base) {
+ if (guest_base & ~qemu_host_page_mask) {
+ error_report("Selected guest base not host page aligned");
+ exit(1);
+ }
+ } else if (reserved_va) {
+ void *p = mmap(NULL, reserved_va + 1, PROT_NONE, MAP_GUARD, -1, 0);
+ if (p == MAP_FAILED) {
+ const char *err = strerror(errno);
+ char *sz = size_to_str(reserved_va + 1);
+
+ error_report("Cannot allocate %s bytes for guest address space: %s",
+ sz, err);
+ exit(1);
+ }
+ guest_base = (uintptr_t)p;
+ have_guest_base = true;
+ }
if (loader_exec(filename, argv + optind, target_environ, regs, info,
&bprm) != 0) {
--
2.41.0
next reply other threads:[~2023-07-27 17:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-27 16:11 Richard Henderson [this message]
2023-07-28 0:09 ` [PATCH] bsd-user: Properly allocate guest virtual address space Warner Losh
2023-07-28 0:31 ` Richard Henderson
2023-07-28 4:06 ` Warner Losh
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=20230727161148.444988-1-richard.henderson@linaro.org \
--to=richard.henderson@linaro.org \
--cc=imp@bsdimp.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).