Skip to contentSkip to conversation

linux-user, configure: fix CPU canonicalization

PATCH In review Paolo Bonzini 3 participants 9 messages Aug 8, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover linux-user, configure: fix CPU canonicalization

The CPU model has to be canonicalized to what Meson wants in the cross file, to what Linux uses for its asm-$ARCH directories, and to what QEMU uses for its user-mode emulation host/$ARCH directories. Do all three in a single case statement, and check that the Linux and QEMU directories actually exist.

At a small cost in repeated lines, this ensures that there are no hidden ordering requirements between the case statements. In particular, commit 89e5b7935e9 ("configure: Fix linux-user host detection for riscv64", 2023-08-06) broke ppc64le because it assigned host_arch based on a non-canonicalized version of $cpu.

While doing this, I noticed that linux-user won't work on x32, alpha and 32-bit s390 these days, due to missing common-user/ fragments. The first two patches clean up the directories.

v1->v2: fix s390x compilation; rearrange case terminators

Paolo Bonzini (3):
configure: fix detection for x32 linux-user
linux-user: cleanup unused linux-user/include/host directories
configure: unify case statements for CPU canonicalization

 configure                                   | 173 ++++++++++++--------
 linux-user/include/host/alpha/host-signal.h |  55 -------
 linux-user/include/host/s390/host-signal.h  | 138 ----------------
 linux-user/include/host/s390x/host-signal.h | 139 +++++++++++++++-
 linux-user/include/host/x32/host-signal.h   |   1 -
 5 files changed, 240 insertions(+), 266 deletions(-)
 delete mode 100644 linux-user/include/host/alpha/host-signal.h
 delete mode 100644 linux-user/include/host/s390/host-signal.h
 delete mode 100644 linux-user/include/host/x32/host-signal.h
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/3 configure: fix detection for x32 linux-user

x32 uses the same signal handling fragments as x86_64, since host_arch is set to x86_64 when Meson runs. Remove the unnecessary forwarder and set the host_arch variable properly in configure.

---
 configure                                 | 2 ++
 linux-user/include/host/x32/host-signal.h | 1 -
 2 files changed, 2 insertions(+), 1 deletion(-)
 delete mode 100644 linux-user/include/host/x32/host-signal.h
configure +2 -0
index 98dc78280e6..484d38d81f4 100755
@@ -472,6 +472,8 @@ fi
 case "$cpu" in
   riscv*)
     host_arch=riscv ;;
+  x32)
+    host_arch=x86_64 ;;
   *)
     host_arch="$cpu" ;;
 esac
linux-user/include/host/x32/host-signal.h deleted +0 -1
deleted file mode 100644
index 26800591d3b..00000000000
@@ -1 +0,0 @@
-#include "../x86_64/host-signal.h"
Signature · 1 line
2.41.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] configure: fix detection for x32 linux-user

Quoted 3 lines · Paolo Bonzini

x32 uses the same signal handling fragments as x86_64, since host_arch is set to x86_64 when Meson runs. Remove the unnecessary forwarder and set the host_arch variable properly in configure.

/mjt

#3 lore raw

PATCH v2 2/3 linux-user: cleanup unused linux-user/include/host directories

In reply to Paolo Bonzini

Alpha and 31-bit s390 lack the assembly fragment to handle signals occurring at the same time as system calls, so they cannot run linux-user emulation anymore. Drop the host-signal.h files for them.

---
 linux-user/include/host/alpha/host-signal.h |  55 --------
 linux-user/include/host/s390/host-signal.h  | 138 -------------------
 linux-user/include/host/s390x/host-signal.h | 139 +++++++++++++++++++-
 3 files changed, 138 insertions(+), 194 deletions(-)
 delete mode 100644 linux-user/include/host/alpha/host-signal.h
 delete mode 100644 linux-user/include/host/s390/host-signal.h
linux-user/include/host/alpha/host-signal.h deleted +0 -55
deleted file mode 100644
index 4f9e2abc4b0..00000000000
@@ -1,55 +0,0 @@
-/*
- * host-signal.h: signal info dependent on the host architecture
- *
- * Copyright (c) 2003-2005 Fabrice Bellard
- * Copyright (c) 2021 Linaro Limited
- *
- * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
- * See the COPYING file in the top-level directory.
- */
-
-#ifndef ALPHA_HOST_SIGNAL_H
-#define ALPHA_HOST_SIGNAL_H
-
-/* The third argument to a SA_SIGINFO handler is ucontext_t. */
-typedef ucontext_t host_sigcontext;
-
-static inline uintptr_t host_signal_pc(host_sigcontext *uc)
-{
-    return uc->uc_mcontext.sc_pc;
-}
-
-static inline void host_signal_set_pc(host_sigcontext *uc, uintptr_t pc)
-{
-    uc->uc_mcontext.sc_pc = pc;
-}
-
-static inline void *host_signal_mask(host_sigcontext *uc)
-{
-    return &uc->uc_sigmask;
-}
-
-static inline bool host_signal_write(siginfo_t *info, host_sigcontext *uc)
-{
-    uint32_t *pc = (uint32_t *)host_signal_pc(uc);
-    uint32_t insn = *pc;
-
-    /* XXX: need kernel patch to get write flag faster */
-    switch (insn >> 26) {
-    case 0x0d: /* stw */
-    case 0x0e: /* stb */
-    case 0x0f: /* stq_u */
-    case 0x24: /* stf */
-    case 0x25: /* stg */
-    case 0x26: /* sts */
-    case 0x27: /* stt */
-    case 0x2c: /* stl */
-    case 0x2d: /* stq */
-    case 0x2e: /* stl_c */
-    case 0x2f: /* stq_c */
-        return true;
-    }
-    return false;
-}
-
-#endif
linux-user/include/host/s390/host-signal.h deleted +0 -138

138 lines not on this page · raw message

linux-user/include/host/s390x/host-signal.h +138 -1

139 lines not on this page · raw message

Signature · 1 line
2.41.0
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] linux-user: cleanup unused linux-user/include/host directories

Quoted 5 lines · Paolo Bonzini

Alpha and 31-bit s390 lack the assembly fragment to handle signals occurring at the same time as system calls, so they cannot run linux-user emulation anymore. Drop the host-signal.h files for them.

Quoted 8 lines
---
  linux-user/include/host/alpha/host-signal.h |  55 --------
  linux-user/include/host/s390/host-signal.h  | 138 -------------------
  linux-user/include/host/s390x/host-signal.h | 139 +++++++++++++++++++-
  3 files changed, 138 insertions(+), 194 deletions(-)

delete mode 100644 linux-user/include/host/alpha/host-signal.h delete mode 100644 linux-user/include/host/s390/host-signal.h

I think this cleanup can be postproned to after 8.1 release, while other 2 patches in this series are needed for 8.1 to fix current breakage with riscv.

linux-user/include/host/s390/ subdir will be be effectively "orphaned" after the subsequent patch, just like alpha/ in there now, it can be cleaned up later just as well.

But anyway,

git format-patch should probably have a way to detect renames, no? Or does it not work if the target file did exist before the patch?

/mjt

#5 lore raw

PATCH v2 3/3 configure: unify case statements for CPU canonicalization

In reply to Paolo Bonzini

The CPU model has to be canonicalized to what Meson wants in the cross file, to what Linux uses for its asm-$ARCH directories, and to what QEMU uses for its user-mode emulation host/$ARCH directories. Do all three in a single case statement, and check that the Linux and QEMU directories actually exist.

At a small cost in repeated lines, this ensures that there are no hidden ordering requirements between the case statements. In particular, commit 89e5b7935e9 ("configure: Fix linux-user host detection for riscv64", 2023-08-06) broke ppc64le because it assigned host_arch based on a non-canonicalized version of $cpu.

Fixes 89e5b7935e9 ("configure: Fix linux-user host detection for riscv64", 2023-08-06)
---
 configure | 175 +++++++++++++++++++++++++++++++-----------------------
 1 file changed, 102 insertions(+), 73 deletions(-)
configure +102 -73
index 484d38d81f4..24bfb9277b9 100755
@@ -469,59 +469,119 @@ else
   echo "WARNING: unrecognized host CPU, proceeding with 'uname -m' output '$cpu'"
 fi
 
-case "$cpu" in
-  riscv*)
-    host_arch=riscv ;;
-  x32)
-    host_arch=x86_64 ;;
-  *)
-    host_arch="$cpu" ;;
-esac
-
-# Normalise host CPU name and set multilib cflags.  The canonicalization
-# isn't really necessary, because the architectures that we check for
-# should not hit the 'uname -m' case, but better safe than sorry.
+# Normalise host CPU name to the values used by Meson cross files and in source
+# directories, and set multilib cflags.  The canonicalization isn't really
+# necessary, because the architectures that we check for should not hit the
+# 'uname -m' case, but better safe than sorry in case --cpu= is used.
+#
 # Note that this case should only have supported host CPUs, not guests.
+# Please keep it sorted and synchronized with meson.build's host_arch.
+host_arch=
+linux_arch=
 case "$cpu" in
+  aarch64)
+    host_arch=aarch64
+    linux_arch=arm64
+    ;;
+
   armv*b|armv*l|arm)
-    cpu="arm" ;;
+    cpu=arm
+    host_arch=arm
+    linux_arch=arm
+    ;;
 
   i386|i486|i586|i686)
     cpu="i386"
-    CPU_CFLAGS="-m32" ;;
+    host_arch=i386
+    linux_arch=x86
+    CPU_CFLAGS="-m32"
+    ;;
+
+  loongarch*)
+    cpu=loongarch64
+    host_arch=loongarch64
+    ;;
+
+  mips64*)
+    cpu=mips64
+    host_arch=mips
+    linux_arch=mips
+    ;;
+  mips*)
+    cpu=mips
+    host_arch=mips
+    linux_arch=mips
+    ;;
+
+  ppc)
+    host_arch=ppc
+    linux_arch=powerpc
+    CPU_CFLAGS="-m32"
+    ;;
+  ppc64)
+    host_arch=ppc64
+    linux_arch=powerpc
+    CPU_CFLAGS="-m64 -mbig-endian"
+    ;;
+  ppc64le)
+    cpu=ppc64
+    host_arch=ppc64
+    linux_arch=powerpc
+    CPU_CFLAGS="-m64 -mlittle-endian"
+    ;;
+
+  riscv32 | riscv64)
+    host_arch=riscv
+    linux_arch=riscv
+    ;;
+
+  s390)
+    linux_arch=s390
+    CPU_CFLAGS="-m31"
+    ;;
+  s390x)
+    host_arch=s390x
+    linux_arch=s390
+    CPU_CFLAGS="-m64"
+    ;;
+
+  sparc|sun4[cdmuv])
+    cpu=sparc
+    CPU_CFLAGS="-m32 -mv8plus -mcpu=ultrasparc"
+    ;;
+  sparc64)
+    host_arch=sparc64
+    CPU_CFLAGS="-m64 -mcpu=ultrasparc"
+    ;;
+
   x32)
     cpu="x86_64"
-    CPU_CFLAGS="-mx32" ;;
+    host_arch=x86_64
+    linux_arch=x86
+    CPU_CFLAGS="-mx32"
+    ;;
   x86_64|amd64)
     cpu="x86_64"
+    host_arch=x86_64
+    linux_arch=x86
     # ??? Only extremely old AMD cpus do not have cmpxchg16b.
     # If we truly care, we should simply detect this case at
     # runtime and generate the fallback to serial emulation.
-    CPU_CFLAGS="-m64 -mcx16" ;;
-
-  mips*)
-    cpu="mips" ;;
-
-  ppc)
-    CPU_CFLAGS="-m32" ;;
-  ppc64)
-    CPU_CFLAGS="-m64 -mbig-endian" ;;
-  ppc64le)
-    cpu="ppc64"
-    CPU_CFLAGS="-m64 -mlittle-endian" ;;
-
-  s390)
-    CPU_CFLAGS="-m31" ;;
-  s390x)
-    CPU_CFLAGS="-m64" ;;
-
-  sparc|sun4[cdmuv])
-    cpu="sparc"
-    CPU_CFLAGS="-m32 -mv8plus -mcpu=ultrasparc" ;;
-  sparc64)
-    CPU_CFLAGS="-m64 -mcpu=ultrasparc" ;;
+    CPU_CFLAGS="-m64 -mcx16"
+    ;;
 esac
 
+if test -n "$host_arch" && {
+    ! test -d "$source_path/linux-user/include/host/$host_arch" ||
+    ! test -d "$source_path/common-user/host/$host_arch"; }; then
+    error_exit "linux-user/include/host/$host_arch does not exist." \
+       "This is a bug in the configure script, please report it."
+fi
+if test -n "$linux_arch" && ! test -d "$source_path/linux-headers/asm-$linux_arch"; then
+    error_exit "linux-headers/asm-$linux_arch does not exist." \
+       "This is a bug in the configure script, please report it."
+fi
+
 check_py_version() {
     # We require python >= 3.7.
     # NB: a True python conditional creates a non-zero return code (Failure)
@@ -812,7 +872,7 @@ default_target_list=""
 mak_wilds=""
 
 if [ "$linux_user" != no ]; then
-    if [ "$targetos" = linux ] && [ -d "$source_path/linux-user/include/host/$host_arch" ]; then
+    if [ "$targetos" = linux ] && [ -n "$host_arch" ]; then
         linux_user=yes
     elif [ "$linux_user" = yes ]; then
         error_exit "linux-user not supported on this architecture"
@@ -1717,40 +1777,9 @@ echo "PKG_CONFIG=${pkg_config}" >> $config_host_mak
 echo "CC=$cc" >> $config_host_mak
 echo "EXESUF=$EXESUF" >> $config_host_mak
 
-# use included Linux headers
-if test "$linux" = "yes" ; then
-  mkdir -p linux-headers
-  case "$cpu" in
-  i386|x86_64)
-    linux_arch=x86
-    ;;
-  ppc|ppc64)
-    linux_arch=powerpc
-    ;;
-  s390x)
-    linux_arch=s390
-    ;;
-  aarch64)
-    linux_arch=arm64
-    ;;
-  loongarch*)
-    linux_arch=loongarch
-    ;;
-  mips64)
-    linux_arch=mips
-    ;;
-  riscv32|riscv64)
-    linux_arch=riscv
-    ;;
-  *)
-    # For most CPUs the kernel architecture name and QEMU CPU name match.
-    linux_arch="$cpu"
-    ;;
-  esac
-    # For non-KVM architectures we will not have asm headers
-    if [ -e "$source_path/linux-headers/asm-$linux_arch" ]; then
-      symlink "$source_path/linux-headers/asm-$linux_arch" linux-headers/asm
-    fi
+# use included Linux headers for KVM architectures
+if test "$linux" = "yes" && test -n "$linux_arch"; then
+  symlink "$source_path/linux-headers/asm-$linux_arch" linux-headers/asm
 fi
 
 for target in $target_list; do
Signature · 1 line
2.41.0
Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] configure: unify case statements for CPU canonicalization

Quoted 12 lines · Paolo Bonzini

The CPU model has to be canonicalized to what Meson wants in the cross file, to what Linux uses for its asm-$ARCH directories, and to what QEMU uses for its user-mode emulation host/$ARCH directories. Do all three in a single case statement, and check that the Linux and QEMU directories actually exist.

At a small cost in repeated lines, this ensures that there are no hidden ordering requirements between the case statements. In particular, commit 89e5b7935e9 ("configure: Fix linux-user host detection for riscv64", 2023-08-06) broke ppc64le because it assigned host_arch based on a non-canonicalized version of $cpu.

Quoted 7 lines
Fixes 89e5b7935e9 ("configure: Fix linux-user host detection for riscv64", 2023-08-06)
---
  configure | 175 +++++++++++++++++++++++++++++++-----------------------
  1 file changed, 102 insertions(+), 73 deletions(-)

A nice cleanup.

/mjt

Tested-by on the v2 series
#7 lore raw
In reply to Paolo Bonzini
Quoted 30 lines · Paolo Bonzini

The CPU model has to be canonicalized to what Meson wants in the cross file, to what Linux uses for its asm-$ARCH directories, and to what QEMU uses for its user-mode emulation host/$ARCH directories. Do all three in a single case statement, and check that the Linux and QEMU directories actually exist.

At a small cost in repeated lines, this ensures that there are no hidden ordering requirements between the case statements. In particular, commit 89e5b7935e9 ("configure: Fix linux-user host detection for riscv64", 2023-08-06) broke ppc64le because it assigned host_arch based on a non-canonicalized version of $cpu.

While doing this, I noticed that linux-user won't work on x32, alpha and 32-bit s390 these days, due to missing common-user/ fragments. The first two patches clean up the directories.

v1->v2: fix s390x compilation; rearrange case terminators

Paolo Bonzini (3):
configure: fix detection for x32 linux-user
linux-user: cleanup unused linux-user/include/host directories
configure: unify case statements for CPU canonicalization

  configure                                   | 173 ++++++++++++--------
  linux-user/include/host/alpha/host-signal.h |  55 -------
  linux-user/include/host/s390/host-signal.h  | 138 ----------------
  linux-user/include/host/s390x/host-signal.h | 139 +++++++++++++++-
  linux-user/include/host/x32/host-signal.h   |   1 -
  5 files changed, 240 insertions(+), 266 deletions(-)
Quoted 3 lines

delete mode 100644 linux-user/include/host/alpha/host-signal.h delete mode 100644 linux-user/include/host/s390/host-signal.h delete mode 100644 linux-user/include/host/x32/host-signal.h

Tested on s390x and ppc64le hosts.

/mjt

Acked-by Tested-by on the v2 series
#8 lore raw
In reply to Paolo Bonzini
Quoted 33 lines · Paolo Bonzini

The CPU model has to be canonicalized to what Meson wants in the cross
file, to what Linux uses for its asm-$ARCH directories, and to what QEMU uses for its user-mode emulation host/$ARCH directories.  Do all three in a single case statement, and check that the Linux and QEMU directories actually exist.

At a small cost in repeated lines, this ensures that there are no hidden
ordering requirements between the case statements.  In particular, commit
89e5b7935e9 ("configure: Fix linux-user host detection for riscv64", 2023-08-06) broke ppc64le because it assigned host_arch based on a non-canonicalized version of $cpu.

While doing this, I noticed that linux-user won't work on x32, alpha and 32-bit s390 these days, due to missing common-user/ fragments. The first two patches clean up the directories.

v1->v2: fix s390x compilation; rearrange case terminators

Paolo Bonzini (3):
  configure: fix detection for x32 linux-user
  linux-user: cleanup unused linux-user/include/host directories
  configure: unify case statements for CPU canonicalization

 configure                                   | 173 ++++++++++++------ --
 linux-user/include/host/alpha/host-signal.h |  55 ------- linux-user/include/host/s390/host-signal.h  | 138 ---------------- linux-user/include/host/s390x/host-signal.h | 139 +++++++++++++++- linux-user/include/host/x32/host-signal.h   |   1 -

Quoted 4 lines
 5 files changed, 240 insertions(+), 266 deletions(-)

 delete mode 100644 linux-user/include/host/alpha/host-signal.h delete mode 100644 linux-user/include/host/s390/host-signal.h delete mode 100644 linux-user/include/host/x32/host-signal.h

Now it works, thanks!