On 3/28/25 13:04, Richard Henderson wrote:
The only place we really need to know the minimum is within
page-vary-target.c.  Rename the target/arm TARGET_PAGE_BITS_MIN
to TARGE_PAGE_BITS_LEGACY to emphasize what it really means.
Move the assertions related to minimum page size as well.


s/TARGE_PAGE_BITS_LEGACY/TARGET_PAGE_BITS_LEGACY

Signed-off-by: Richard Henderson <richard.hender...@linaro.org>
---
  include/exec/cpu-defs.h    | 10 ++--------
  include/exec/poison.h      |  1 +
  include/exec/target_page.h |  1 -
  include/qemu/osdep.h       |  6 ++++++
  target/alpha/cpu-param.h   |  1 -
  target/arm/cpu-param.h     |  3 +--
  target/ppc/cpu-param.h     |  1 -
  accel/tcg/cputlb.c         |  1 -
  page-vary-target.c         | 39 +++++++++++++++++++++++++++++++++++---
  9 files changed, 46 insertions(+), 17 deletions(-)

diff --git a/include/exec/cpu-defs.h b/include/exec/cpu-defs.h
index 9f955f53fd..e01acb7c90 100644
--- a/include/exec/cpu-defs.h
+++ b/include/exec/cpu-defs.h
@@ -34,14 +34,8 @@
  #ifndef TARGET_VIRT_ADDR_SPACE_BITS
  # error TARGET_VIRT_ADDR_SPACE_BITS must be defined in cpu-param.h
  #endif
-#ifndef TARGET_PAGE_BITS
-# ifdef TARGET_PAGE_BITS_VARY
-#  ifndef TARGET_PAGE_BITS_MIN
-#   error TARGET_PAGE_BITS_MIN must be defined in cpu-param.h
-#  endif
-# else
-#  error TARGET_PAGE_BITS must be defined in cpu-param.h
-# endif
+#if !defined(TARGET_PAGE_BITS) && !defined(TARGET_PAGE_BITS_VARY)
+# error TARGET_PAGE_BITS must be defined in cpu-param.h
  #endif
#include "exec/target_long.h"
diff --git a/include/exec/poison.h b/include/exec/poison.h
index 4180a5a489..c4f7ee22bf 100644
--- a/include/exec/poison.h
+++ b/include/exec/poison.h
@@ -44,6 +44,7 @@
  #pragma GCC poison TARGET_FMT_lu
#pragma GCC poison TARGET_PHYS_ADDR_SPACE_BITS
+#pragma GCC poison TARGET_PAGE_BITS_LEGACY
#pragma GCC poison CONFIG_ALPHA_DIS
  #pragma GCC poison CONFIG_HPPA_DIS
diff --git a/include/exec/target_page.h b/include/exec/target_page.h
index e4bd7f7767..ca0ebbc8bb 100644
--- a/include/exec/target_page.h
+++ b/include/exec/target_page.h
@@ -41,7 +41,6 @@ extern const TargetPageBits target_page;
  # endif
  # define TARGET_PAGE_SIZE    (-(int)TARGET_PAGE_MASK)
  #else
-# define TARGET_PAGE_BITS_MIN TARGET_PAGE_BITS
  # define TARGET_PAGE_SIZE    (1 << TARGET_PAGE_BITS)
  # define TARGET_PAGE_MASK    ((TARGET_PAGE_TYPE)-1 << TARGET_PAGE_BITS)
  #endif
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 4397a90680..321a52d7f0 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -50,6 +50,12 @@
   */
  #pragma GCC poison TARGET_WORDS_BIGENDIAN
+/*
+ * TARGET_PAGE_BITS_MIN was repaced by TARGET_PAGE_BITS_LEGACY
+ * for system mode.  Prevent it from creeping back in.
+ */

s/repaced/replaced

+#pragma GCC poison TARGET_PAGE_BITS_MIN
+
  #include "qemu/compiler.h"
/* Older versions of C++ don't get definitions of various macros from
diff --git a/target/alpha/cpu-param.h b/target/alpha/cpu-param.h
index ff06e41497..63989e71c0 100644
--- a/target/alpha/cpu-param.h
+++ b/target/alpha/cpu-param.h
@@ -18,7 +18,6 @@
   * a 4k minimum to match x86 host, which can minimize emulation issues.
   */
  # define TARGET_PAGE_BITS_VARY
-# define TARGET_PAGE_BITS_MIN 12
  # define TARGET_VIRT_ADDR_SPACE_BITS  63
  #else
  # define TARGET_PAGE_BITS 13
diff --git a/target/arm/cpu-param.h b/target/arm/cpu-param.h
index 896b35bd6d..a7ae42d17d 100644
--- a/target/arm/cpu-param.h
+++ b/target/arm/cpu-param.h
@@ -24,7 +24,6 @@
  # else
  /* Allow user-only to vary page size from 4k */
  #  define TARGET_PAGE_BITS_VARY
-#  define TARGET_PAGE_BITS_MIN  12
  # endif
  # else
  #  define TARGET_PAGE_BITS 12
@@ -35,7 +34,7 @@
   * have to support 1K tiny pages.
   */
  # define TARGET_PAGE_BITS_VARY
-# define TARGET_PAGE_BITS_MIN  10
+# define TARGET_PAGE_BITS_LEGACY 10
  #endif /* !CONFIG_USER_ONLY */
/* ARM processors have a weak memory model */
diff --git a/target/ppc/cpu-param.h b/target/ppc/cpu-param.h
index 6c4525fdf3..553ad2f4c6 100644
--- a/target/ppc/cpu-param.h
+++ b/target/ppc/cpu-param.h
@@ -33,7 +33,6 @@
  #ifdef CONFIG_USER_ONLY
  /* Allow user-only to vary page size from 4k */
  # define TARGET_PAGE_BITS_VARY
-# define TARGET_PAGE_BITS_MIN 12
  #else
  # define TARGET_PAGE_BITS 12
  #endif
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
index 39314e86f3..0de46903dd 100644
--- a/accel/tcg/cputlb.c
+++ b/accel/tcg/cputlb.c
@@ -49,7 +49,6 @@
  #endif
  #include "tcg/tcg-ldst.h"
-QEMU_BUILD_BUG_ON(TLB_FLAGS_MASK & ((1u < TARGET_PAGE_BITS_MIN) - 1)); /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */
  /* #define DEBUG_TLB */
diff --git a/page-vary-target.c b/page-vary-target.c
index 6251d948cf..d83f9a6a90 100644
--- a/page-vary-target.c
+++ b/page-vary-target.c
@@ -23,19 +23,45 @@
  #include "exec/page-vary.h"
  #include "exec/target_page.h"
+
+/*
+ * For system mode, the minimum comes from the number of bits
+ * required for maximum alignment (6) and the number of bits
+ * required for TLB_FLAGS_MASK (3).
+ *
+ * For user mode, TARGET_PAGE_BITS_VARY is a hack to allow the target
+ * page size to match the host page size.  Mostly, this reduces the
+ * ordinary target page size to run on a host with 4KiB pages (i.e. x86).
+ * There is no true minimum required by the implementation, but keep the
+ * same minimum as for system mode for sanity.
+ * See linux-user/mmap.c, mmap_h_lt_g and mmap_h_gt_g.
+ */
+#define TARGET_PAGE_BITS__MIN 9
+
+#ifndef TARGET_PAGE_BITS_VARY
+QEMU_BUILD_BUG_ON(TARGET_PAGE_BITS < TARGET_PAGE_BITS__MIN);
+#endif
+
+#ifndef CONFIG_USER_ONLY
+#include "exec/tlb-flags.h"
+
+QEMU_BUILD_BUG_ON(TLB_FLAGS_MASK & ((1u < TARGET_PAGE_BITS__MIN) - 1));
+
  int migration_legacy_page_bits(void)
  {
  #ifdef TARGET_PAGE_BITS_VARY
-    return TARGET_PAGE_BITS_MIN;
+    QEMU_BUILD_BUG_ON(TARGET_PAGE_BITS_LEGACY < TARGET_PAGE_BITS__MIN);
+    return TARGET_PAGE_BITS_LEGACY;
  #else
      return TARGET_PAGE_BITS;
  #endif
  }
+#endif
bool set_preferred_target_page_bits(int bits)
  {
+    assert(bits >= TARGET_PAGE_BITS__MIN);
  #ifdef TARGET_PAGE_BITS_VARY
-    assert(bits >= TARGET_PAGE_BITS_MIN);
      return set_preferred_target_page_bits_common(bits);
  #else
      return true;
@@ -44,5 +70,12 @@ bool set_preferred_target_page_bits(int bits)
void finalize_target_page_bits(void)
  {
-    finalize_target_page_bits_common(TARGET_PAGE_BITS_MIN);
+#ifndef TARGET_PAGE_BITS_VARY
+    finalize_target_page_bits_common(TARGET_PAGE_BITS);
+#elif defined(CONFIG_USER_ONLY)
+    assert(target_page.bits != 0);
+    finalize_target_page_bits_common(target_page.bits);
+#else
+    finalize_target_page_bits_common(TARGET_PAGE_BITS_LEGACY);
+#endif
  }

Great!

Reviewed-by: Pierrick Bouvier <pierrick.bouv...@linaro.org>

Any chance we would rename TARGET_PAGE_BITS__MIN? (MIN_ALL? ALL_MIN? ARCH_MIN? any other idea)
I know it's restricted to this file only, but the __ is surprising.

Reply via email to