Skip to contentSkip to conversation

Linux user patches

GIT PULL Applied Helge Deller 3 participants 7 messages Jun 18 – Jun 24

lore
Share & tools
lore
#0 lore raw

From: Helge Deller <[email protected]>

The following changes since commit c7cf7c810153d6f5f31aa2d5c0dee9087f6b4dff:

Merge tag 'firmware-20260617-pull-request' of https://gitlab.com/kraxel/qemu into staging (2026-06-17 10:17:29 -0400)

are available in the Git repository at:

https://github.com/hdeller/qemu-hppa.git tags/linux-user-pull-request

for you to fetch changes up to 54e08dbe8f2aeca57e3b1a5eab09a9fec88c1c67:

linux-user/xtensa: fix unlock of uninitialized frame pointer on sigreturn (2026-06-18 23:43:35 +0200)

----------------------------------------------------------------
linux-user patches

One frame pointer memory locking fix for the xtensa architecture, a generic loader fix for programs with modified headers, and two patches to emulate /proc/cpuinfo for loogarch and m68k CPUs.

----------------------------------------------------------------

Akshit Yadav (1):
linux-user: Fix AT_PHDR when program headers are relocated into their
own segment

Helge Deller (2):
linux-user: Implement /proc/cpuinfo for loongarch cpus
linux-user: Implement /proc/cpuinfo for m68k CPU

Matt Turner (1):
linux-user/xtensa: fix unlock of uninitialized frame pointer on
sigreturn

 linux-user/elfload.c                 | 21 +++++++-
 linux-user/loongarch64/elfload.c     | 20 +++++++
 linux-user/loongarch64/target_proc.h | 79 +++++++++++++++++++++++++++-
 linux-user/m68k/target_proc.h        | 46 ++++++++++++++++
 linux-user/qemu.h                    |  1 +
 linux-user/xtensa/signal.c           |  3 +-
 6 files changed, 167 insertions(+), 3 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

[PULL 1/4] linux-user: Fix AT_PHDR when program headers are relocated into their own segment

From: Akshit Yadav <[email protected]>

When a binary is patched or relocated such that the program header table is moved into a separate PT_LOAD segment (rather than sitting at the start of the first loadable segment), QEMU's AT_PHDR auxv entry becomes incorrect. The loader was computing AT_PHDR as load_addr + e_phoff, which assumes the headers are mapped 1:1 from file offset 0. This breaks when the headers are elsewhere.

The Linux kernel instead locates the PT_LOAD segment that contains e_phoff, then computes the in-memory address as p_vaddr + (e_phoff - p_offset). This correctly handles relocated headers.

Fix by:
1. Add phdr_addr field to image_info to cache the resolved address.
2. Initialize to load_addr + e_phoff (fallback for headers outside any PT_LOAD).
3. In the PT_LOAD mapping loop, detect if the segment contains e_phoff and override with the segment-relative address.
4. Use info->phdr_addr for AT_PHDR instead of the incorrect formula.

---
 linux-user/elfload.c | 21 ++++++++++++++++++++-
 linux-user/qemu.h    |  1 +
 2 files changed, 21 insertions(+), 1 deletion(-)
linux-user/elfload.c +20 -1
index b05b8b0c6b..8049c8ae62 100644
@@ -699,7 +699,7 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
     /* There must be exactly DLINFO_ITEMS entries here, or the assert
      * on info->auxv_len will trigger.
      */
-    NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
+    NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->phdr_addr));
     NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
     NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
     NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
@@ -1469,6 +1469,12 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
     info->data_offset = load_bias;
     info->load_addr = load_addr;
     info->entry = ehdr->e_entry + load_bias;
+    /*
+     * Fallback for AT_PHDR if the program headers do not fall within
+     * any PT_LOAD segment (see the loop below, which overrides this with
+     * the correct in-memory address when a containing segment is found).
+     */
+    info->phdr_addr = load_addr + ehdr->e_phoff;
     info->start_code = -1;
     info->end_code = 0;
     info->start_data = -1;
@@ -1523,6 +1529,19 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
             vaddr_ef = vaddr + eppnt->p_filesz;
             vaddr_em = vaddr + eppnt->p_memsz;
 
+            /*
+             * If this segment contains the program headers, record their
+             * in-memory address for AT_PHDR. This matches the kernel, which
+             * locates the headers via the containing PT_LOAD rather than
+             * assuming load_addr + e_phoff (false when the phdrs are not
+             * mapped 1:1 from file offset 0, e.g. relocated into their own
+             * segment by a binary patcher).
+             */
+            if (eppnt->p_offset <= ehdr->e_phoff &&
+                ehdr->e_phoff < eppnt->p_offset + eppnt->p_filesz) {
+                info->phdr_addr = vaddr + (ehdr->e_phoff - eppnt->p_offset);
+            }
+
             /*
              * Some segments may be completely empty, with a non-zero p_memsz
              * but no backing file segment.
linux-user/qemu.h +1 -0
index 07fe801628..2268493141 100644
@@ -26,6 +26,7 @@
 struct image_info {
         abi_ulong       load_bias;
         abi_ulong       load_addr;
+        abi_ulong       phdr_addr;
         abi_ulong       start_code;
         abi_ulong       end_code;
         abi_ulong       start_data;
Signature · 1 line
2.54.0
#3 lore raw

[PULL 3/4] linux-user: Implement /proc/cpuinfo for m68k CPU

In reply to Helge Deller

From: Helge Deller <[email protected]>

Mimic the entries for /proc/cpuinfo to what can be seen on the debian porterbox mitchy.debian.org.

Cc Thomas Huth <[email protected]>
Cc Laurent Vivier <[email protected]>
---
 linux-user/m68k/target_proc.h | 46 +++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
linux-user/m68k/target_proc.h +46 -0
index 3df8f28e22..dca77d8483 100644
@@ -1,6 +1,8 @@
 /*
  * M68K specific proc functions for linux-user
  *
+ * Copyright (c) 2026 Helge Deller
+ *
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
 #ifndef M68K_TARGET_PROC_H
@@ -13,4 +15,48 @@ static int open_hardware(CPUArchState *cpu_env, int fd)
 }
 #define HAVE_ARCH_PROC_HARDWARE
 
+
+static int open_cpuinfo(CPUArchState *cpu_env, int fd)
+{
+    const char *cpu, *fpu;
+    struct timespec res;
+    double freq_mhz;
+
+    if (clock_getres(CLOCK_REALTIME, &res) == -1) {
+        res.tv_nsec = 1;
+    }
+    freq_mhz = 1000.0 / res.tv_nsec;
+
+    if (m68k_feature(cpu_env, M68K_FEATURE_M68010)) {
+        cpu = "68010";
+    } else if (m68k_feature(cpu_env, M68K_FEATURE_M68020)) {
+        cpu = "68020";
+    } else if (m68k_feature(cpu_env, M68K_FEATURE_M68030)) {
+        cpu = "68030";
+    } else if (m68k_feature(cpu_env, M68K_FEATURE_M68040)) {
+        cpu = "68040";
+    } else if (m68k_feature(cpu_env, M68K_FEATURE_M68060)) {
+        cpu = "68060";
+    } else {
+        cpu = "680x0";
+    }
+
+    if (m68k_feature(cpu_env, M68K_FEATURE_FPU)) {
+        fpu = cpu;
+    } else {
+	fpu = "none(soft float)";
+    }
+
+    dprintf(fd, "CPU:\t\t%s\n"
+                "MMU:\t\t%s\n"
+                "FPU:\t\t%s\n"
+                "Clocking:\t%.1fMHz\n"
+                "Model:\t\tQEMU user v" QEMU_VERSION "\n",
+                cpu, cpu, fpu, freq_mhz);
+    /* dropped BogoMips and Calibration for now */
+
+    return 0;
+}
+#define HAVE_ARCH_PROC_CPUINFO
+
 #endif /* M68K_TARGET_PROC_H */
Signature · 1 line
2.54.0
#4 lore raw

[PULL 2/4] linux-user: Implement /proc/cpuinfo for loongarch cpus

In reply to Helge Deller

From: Helge Deller <[email protected]>

Mimic the entries for /proc/cpuinfo to what can be seen on the debian porterbox loomis.debian.org.

Cc Song Gao <[email protected]>
Cc Bibo Mao <[email protected]>
Cc Jiaxun Yang <[email protected]>
---
 linux-user/loongarch64/elfload.c     | 20 +++++++
 linux-user/loongarch64/target_proc.h | 79 +++++++++++++++++++++++++++-
 2 files changed, 98 insertions(+), 1 deletion(-)
linux-user/loongarch64/elfload.c +20 -0
index e53957e36d..2242f20202 100644
@@ -28,6 +28,26 @@ enum {
     HWCAP_LOONGARCH_LBT_MIPS = (1 << 12),
 };
 
+const char *elf_hwcap_str(uint32_t bit)
+{
+    static const char *hwcap_str[] = {
+    [__builtin_ctz(HWCAP_LOONGARCH_CPUCFG  )] = "cpucfg",
+    [__builtin_ctz(HWCAP_LOONGARCH_LAM     )] = "lam",
+    [__builtin_ctz(HWCAP_LOONGARCH_UAL     )] = "lam_bh",
+    [__builtin_ctz(HWCAP_LOONGARCH_FPU     )] = "fpu",
+    [__builtin_ctz(HWCAP_LOONGARCH_LSX     )] = "lsx",
+    [__builtin_ctz(HWCAP_LOONGARCH_LASX    )] = "lasx",
+    [__builtin_ctz(HWCAP_LOONGARCH_CRC32   )] = "crc32",
+    [__builtin_ctz(HWCAP_LOONGARCH_COMPLEX )] = "complex",
+    [__builtin_ctz(HWCAP_LOONGARCH_CRYPTO  )] = "crypto",
+    [__builtin_ctz(HWCAP_LOONGARCH_LVZ     )] = "lvz",
+    [__builtin_ctz(HWCAP_LOONGARCH_LBT_X86 )] = "lbt_x86",
+    [__builtin_ctz(HWCAP_LOONGARCH_LBT_ARM )] = "lbt_arm",
+    [__builtin_ctz(HWCAP_LOONGARCH_LBT_MIPS)] = "lbt_mips",
+    };
+
+    return bit < ARRAY_SIZE(hwcap_str) ? hwcap_str[bit] : NULL;
+}
 abi_ulong get_elf_hwcap(CPUState *cs)
 {
     LoongArchCPU *cpu = LOONGARCH_CPU(cs);
linux-user/loongarch64/target_proc.h +78 -1

79 lines not on this page · raw message

Signature · 1 line
2.54.0
#5 lore raw

[PULL 4/4] linux-user/xtensa: fix unlock of uninitialized frame pointer on sigreturn

In reply to Helge Deller

From: Matt Turner <[email protected]>

If lock_user_struct fails, frame is uninitialized but the badframe label unconditionally calls unlock_user_struct on it. Handle the lock failure inline so badframe is only reached with a valid lock.

---
 linux-user/xtensa/signal.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
linux-user/xtensa/signal.c +2 -1
index e3f9da322b..4990c50045 100644
@@ -355,7 +355,8 @@ long do_rt_sigreturn(CPUXtensaState *env)
 
     trace_user_do_rt_sigreturn(env, frame_addr);
     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
-        goto badframe;
+        force_sig(TARGET_SIGSEGV);
+        return -QEMU_ESIGRETURN;
     }
     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
     set_sigmask(&set);
Signature · 1 line
2.54.0
#2 lore raw

Re: [PULL 1/4] linux-user: Fix AT_PHDR when program headers are relocated into their own segment

In reply to Helge Deller
Quoted 19 lines · Helge Deller

From: Akshit Yadav <[email protected]>

When a binary is patched or relocated such that the program header table is moved into a separate PT_LOAD segment (rather than sitting at the start of the first loadable segment), QEMU's AT_PHDR auxv entry becomes incorrect. The loader was computing AT_PHDR as load_addr + e_phoff, which assumes the headers are mapped 1:1 from file offset 0. This breaks when the headers are elsewhere.

The Linux kernel instead locates the PT_LOAD segment that contains e_phoff, then computes the in-memory address as p_vaddr + (e_phoff - p_offset). This correctly handles relocated headers.

Fix by:
1. Add phdr_addr field to image_info to cache the resolved address.
2. Initialize to load_addr + e_phoff (fallback for headers outside any PT_LOAD).
3. In the PT_LOAD mapping loop, detect if the segment contains e_phoff and override with the segment-relative address.
4. Use info->phdr_addr for AT_PHDR instead of the incorrect formula.

Quoted 6 lines
---
  linux-user/elfload.c | 21 ++++++++++++++++++++-
  linux-user/qemu.h    |  1 +
  2 files changed, 21 insertions(+), 1 deletion(-)

Despite being a sort-of niche issue, it looks like a good candidate for stable qemu series too, - this didn't come out of void and had real impact.

I'm picking this one up for current stable qemu series (10.0, 10.2, 11.0), please let me know if I shouldn't.

Thanks,

/mjt