All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Henderson <rth@twiddle.net>
To: qemu-devel@nongnu.org
Cc: aurelien@aurel32.net
Subject: [Qemu-devel] [PATCH v3 29/30] linux-user: Tidy and enforce reserved_va initialization
Date: Tue, 18 Jul 2017 10:02:54 -1000	[thread overview]
Message-ID: <20170718200255.31647-30-rth@twiddle.net> (raw)
In-Reply-To: <20170718200255.31647-1-rth@twiddle.net>

We had a check using TARGET_VIRT_ADDR_SPACE_BITS to make sure
that the allocation coming in from the command-line option was
not too large, but that didn't include target-specific knowledge
about other restrictions on user-space.

Remove several target-specific hacks in linux-user/main.c.

For MIPS and Nios, we can replace them with proper adjustments
to the respective target's TARGET_VIRT_ADDR_SPACE_BITS definition.

For ARM, we had no existing ifdef but I suspect that the current
default value of 0xf7000000 was chosen with this in mind.  Define
a workable value in linux-user/arm/, and also document why the
special case is required.

Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 linux-user/arm/target_cpu.h |  4 ++++
 target/mips/mips-defs.h     |  6 +++++-
 target/nios2/cpu.h          |  6 +++++-
 linux-user/main.c           | 38 +++++++++++++++++++++++++-------------
 4 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/linux-user/arm/target_cpu.h b/linux-user/arm/target_cpu.h
index d888219..c4f79eb 100644
--- a/linux-user/arm/target_cpu.h
+++ b/linux-user/arm/target_cpu.h
@@ -19,6 +19,10 @@
 #ifndef ARM_TARGET_CPU_H
 #define ARM_TARGET_CPU_H
 
+/* We need to be able to map the commpage.
+   See validate_guest_space in linux-user/elfload.c.  */
+#define MAX_RESERVED_VA  0xfff00000ul
+
 static inline void cpu_clone_regs(CPUARMState *env, target_ulong newsp)
 {
     if (newsp) {
diff --git a/target/mips/mips-defs.h b/target/mips/mips-defs.h
index 047554e..d239069 100644
--- a/target/mips/mips-defs.h
+++ b/target/mips/mips-defs.h
@@ -15,7 +15,11 @@
 #else
 #define TARGET_LONG_BITS 32
 #define TARGET_PHYS_ADDR_SPACE_BITS 40
-#define TARGET_VIRT_ADDR_SPACE_BITS 32
+# ifdef CONFIG_USER_ONLY
+#  define TARGET_VIRT_ADDR_SPACE_BITS 31
+# else
+#  define TARGET_VIRT_ADDR_SPACE_BITS 32
+#endif
 #endif
 
 /* Masks used to mark instructions to indicate which ISA level they
diff --git a/target/nios2/cpu.h b/target/nios2/cpu.h
index 13931f3..da3f637 100644
--- a/target/nios2/cpu.h
+++ b/target/nios2/cpu.h
@@ -227,7 +227,11 @@ qemu_irq *nios2_cpu_pic_init(Nios2CPU *cpu);
 void nios2_check_interrupts(CPUNios2State *env);
 
 #define TARGET_PHYS_ADDR_SPACE_BITS 32
-#define TARGET_VIRT_ADDR_SPACE_BITS 32
+#ifdef CONFIG_USER_ONLY
+# define TARGET_VIRT_ADDR_SPACE_BITS 31
+#else
+# define TARGET_VIRT_ADDR_SPACE_BITS 32
+#endif
 
 #define cpu_init(cpu_model) CPU(cpu_nios2_init(cpu_model))
 
diff --git a/linux-user/main.c b/linux-user/main.c
index 30f0ae1..7693a62 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -60,23 +60,38 @@ do {                                                                    \
     }                                                                   \
 } while (0)
 
-#if (TARGET_LONG_BITS == 32) && (HOST_LONG_BITS == 64)
 /*
  * When running 32-on-64 we should make sure we can fit all of the possible
  * guest address space into a contiguous chunk of virtual host memory.
  *
  * This way we will never overlap with our own libraries or binaries or stack
  * or anything else that QEMU maps.
+ *
+ * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
+ * of the address for the kernel.  Some cpus rely on this and user space
+ * uses the high bit(s) for pointer tagging and the like.  For them, we
+ * must preserve the expected address space.
  */
-# if defined(TARGET_MIPS) || defined(TARGET_NIOS2)
-/*
- * MIPS only supports 31 bits of virtual address space for user space.
- * Nios2 also only supports 31 bits.
- */
-unsigned long reserved_va = 0x77000000;
+#ifndef MAX_RESERVED_VA
+# if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
+#  if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
+      (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
+/* There are a number of places where we assign reserved_va to a variable
+   of type abi_ulong and expect it to fit.  Avoid the last page.  */
+#   define MAX_RESERVED_VA  (0xfffffffful & TARGET_PAGE_MASK)
+#  else
+#   define MAX_RESERVED_VA  (1ul << TARGET_VIRT_ADDR_SPACE_BITS)
+#  endif
 # else
-unsigned long reserved_va = 0xf7000000;
+#  define MAX_RESERVED_VA  0
 # endif
+#endif
+
+/* That said, reserving *too* much vm space via mmap can run into problems
+   with rlimits, oom due to page table creation, etc.  We will still try it,
+   if directed by the command-line option, but not by default.  */
+#if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32
+unsigned long reserved_va = MAX_RESERVED_VA;
 #else
 unsigned long reserved_va;
 #endif
@@ -3976,11 +3991,8 @@ static void handle_arg_reserved_va(const char *arg)
         unsigned long unshifted = reserved_va;
         p++;
         reserved_va <<= shift;
-        if (((reserved_va >> shift) != unshifted)
-#if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
-            || (reserved_va > (1ul << TARGET_VIRT_ADDR_SPACE_BITS))
-#endif
-            ) {
+        if (reserved_va >> shift != unshifted
+            || (MAX_RESERVED_VA && reserved_va > MAX_RESERVED_VA)) {
             fprintf(stderr, "Reserved virtual address too big\n");
             exit(EXIT_FAILURE);
         }
-- 
2.9.4

  parent reply	other threads:[~2017-07-18 20:04 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-18 20:02 [Qemu-devel] [PATCH v3 00/30] target/sh4 improvements Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 01/30] target/sh4: Use cmpxchg for movco Richard Henderson
2017-07-18 20:19   ` Aurelien Jarno
2017-07-18 21:36     ` Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 02/30] target/sh4: Consolidate end-of-TB tests Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 03/30] target/sh4: Introduce TB_FLAG_ENVFLAGS_MASK Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 04/30] target/sh4: Keep env->flags clean Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 05/30] target/sh4: Adjust TB_FLAG_PENDING_MOVCA Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 06/30] target/sh4: Handle user-space atomics Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 07/30] target/sh4: Recognize common gUSA sequences Richard Henderson
2017-07-18 20:32   ` Aurelien Jarno
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 08/30] linux-user/sh4: Notice gUSA regions during signal delivery Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 09/30] linux-user/sh4: Clean env->flags on signal boundaries Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 10/30] target/sh4: Hoist register bank selection Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 11/30] target/sh4: Unify cpu_fregs into FREG Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 12/30] target/sh4: Pass DisasContext to fpr64 routines Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 14/30] target/sh4: Eliminate unused XREG macro Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 15/30] target/sh4: Merge DREG into fpr64 routines Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 16/30] target/sh4: Load/store Dr as 64-bit quantities Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 17/30] target/sh4: Simplify 64-bit fp reg-reg move Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 18/30] target/sh4: Unify code for CHECK_NOT_DELAY_SLOT Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 19/30] target/sh4: Unify code for CHECK_PRIVILEGED Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 20/30] target/sh4: Unify code for CHECK_FPU_ENABLED Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 21/30] target/sh4: Tidy misc illegal insn checks Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 22/30] target/sh4: Introduce CHECK_FPSCR_PR_* Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 23/30] target/sh4: Introduce CHECK_SH4A Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 24/30] target/sh4: Implement fpchg Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 25/30] target/sh4: Add missing FPSCR.PR == 0 checks Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 26/30] target/sh4: Implement fsrra Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 27/30] target/sh4: Use tcg_gen_lookup_and_goto_ptr Richard Henderson
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 28/30] tcg: Fix off-by-one in assert in page_set_flags Richard Henderson
2017-07-18 20:02 ` Richard Henderson [this message]
2017-07-18 20:02 ` [Qemu-devel] [PATCH v3 30/30] linux-user/sh4: Reduce TARGET_VIRT_ADDR_SPACE_BITS to 31 Richard Henderson
2017-07-18 21:02 ` [Qemu-devel] [PATCH v3 00/30] target/sh4 improvements Aurelien Jarno

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=20170718200255.31647-30-rth@twiddle.net \
    --to=rth@twiddle.net \
    --cc=aurelien@aurel32.net \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.