From: Helge Deller <deller@kernel.org>
To: qemu-devel@nongnu.org
Cc: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>,
Laurent Vivier <laurent@vivier.eu>,
Yoshinori Sato <yoshinori.sato@nifty.com>,
Max Filippov <jcmvbkbc@gmail.com>, Helge Deller <deller@gmx.de>,
Matt Turner <mattst88@gmail.com>,
Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Subject: [PATCH 07/10] linux-user/sparc: flush register windows before core dump
Date: Sun, 7 Jun 2026 16:03:53 +0200 [thread overview]
Message-ID: <20260607140356.10702-8-deller@kernel.org> (raw)
In-Reply-To: <20260607140356.10702-1-deller@kernel.org>
From: Matt Turner <mattst88@gmail.com>
Without this, only the crash frame's window is spilled to the
stack; all deeper call frames remain in the register file and
are absent from the core's memory segments. Stack unwinding
fails past the first DWARF step because the callers' register
save areas contain stale/garbage data.
The real kernel calls flush_all_user_windows() at the top of
do_coredump(). Mirror that via a weak target_flush_windows()
hook called from dump_core_and_abort(), with the SPARC override
calling the existing flush_windows() in cpu_loop.c.
Signed-off-by: Matt Turner <mattst88@gmail.com>
Cc: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Signed-off-by: Helge Deller <deller@gmx.de>
---
linux-user/elfload.c | 9 ++++++++
linux-user/sparc/cpu_loop.c | 3 ++-
linux-user/sparc/cpu_loop.h | 7 +++++++
linux-user/sparc/elfload.c | 39 +++++++++++++++++++++++++++++------
linux-user/sparc/target_elf.h | 17 +++++++++++----
5 files changed, 64 insertions(+), 11 deletions(-)
create mode 100644 linux-user/sparc/cpu_loop.h
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index f7625c0952..b05b8b0c6b 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2445,6 +2445,9 @@ static int wmr_write_region(void *opaque, vaddr start,
* handler (provided that target process haven't registered
* handler for that) that does the dump when signal is received.
*/
+#ifdef TARGET_SPARC
+#include "sparc/cpu_loop.h"
+#endif
static int elf_core_dump(int signr, const CPUArchState *env)
{
const CPUState *cpu = env_cpu_const(env);
@@ -2468,6 +2471,12 @@ static int elf_core_dump(int signr, const CPUArchState *env)
cpu_list_lock();
mmap_lock();
+#ifdef TARGET_SPARC
+ CPU_FOREACH(cpu_iter) {
+ flush_windows(cpu_env(cpu_iter));
+ }
+#endif
+
/* By unprotecting, we merge vmas that might be split. */
walk_memory_regions(NULL, wmr_page_unprotect_regions);
diff --git a/linux-user/sparc/cpu_loop.c b/linux-user/sparc/cpu_loop.c
index ab633eeae3..0aacda9448 100644
--- a/linux-user/sparc/cpu_loop.c
+++ b/linux-user/sparc/cpu_loop.c
@@ -22,6 +22,7 @@
#include "user-internals.h"
#include "user/cpu_loop.h"
#include "signal-common.h"
+#include "sparc/cpu_loop.h"
#define SPARC64_STACK_BIAS 2047
@@ -119,7 +120,7 @@ static void restore_window(CPUSPARCState *env)
#endif
}
-static void flush_windows(CPUSPARCState *env)
+void flush_windows(CPUSPARCState *env)
{
int offset, cwp1;
diff --git a/linux-user/sparc/cpu_loop.h b/linux-user/sparc/cpu_loop.h
new file mode 100644
index 0000000000..fb6e82d372
--- /dev/null
+++ b/linux-user/sparc/cpu_loop.h
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef SPARC_CPU_LOOP_H
+#define SPARC_CPU_LOOP_H
+
+void flush_windows(CPUSPARCState *env);
+
+#endif
diff --git a/linux-user/sparc/elfload.c b/linux-user/sparc/elfload.c
index e6387ec891..181f1e00b5 100644
--- a/linux-user/sparc/elfload.c
+++ b/linux-user/sparc/elfload.c
@@ -12,16 +12,41 @@ void elf_core_copy_regs(target_elf_gregset_t *r, const CPUArchState *env)
CPUSPARCState *e = (CPUSPARCState *)env;
int i;
+ memset(r, 0, sizeof(*r));
+
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
+ /* Linux kernel layout for sparc64 (arch/sparc/include/asm/elf_64.h):
+ * [0..7] G0-G7
+ * [8..15] O0-O7
+ * [16..23] L0-L7
+ * [24..31] I0-I7
+ * [32] TSTATE
+ * [33] TPC
+ * [34] TNPC
+ * [35] Y
+ */
for (i = 0; i < 8; i++) {
- r->regs[i] = tswap64(env->gregs[i]);
- r->regs[8 + i] = tswap64(env->regwptr[WREG_O0 + i]);
+ r->regs[i] = tswap64(env->gregs[i]);
+ r->regs[8 + i] = tswap64(env->regwptr[WREG_O0 + i]);
+ r->regs[16 + i] = tswap64(env->regwptr[WREG_L0 + i]);
+ r->regs[24 + i] = tswap64(env->regwptr[WREG_I0 + i]);
}
- r->regs[16] = tswap64(sparc64_tstate(e));
- r->regs[17] = tswap64(env->pc);
- r->regs[18] = tswap64(env->npc);
- r->regs[19] = tswap64(env->y);
+ r->regs[32] = tswap64(sparc64_tstate(e));
+ r->regs[33] = tswap64(env->pc);
+ r->regs[34] = tswap64(env->npc);
+ r->regs[35] = tswap64(env->y);
#else
+ /* Linux kernel layout for sparc32 (arch/sparc/include/asm/elf_32.h):
+ * [0] PSR
+ * [1] PC
+ * [2] NPC
+ * [3] Y
+ * [4..11] G0-G7
+ * [12..19] O0-O7
+ * [20..27] L0-L7
+ * [28..35] I0-I7
+ * [36..37] reserved (stack_check)
+ */
r->regs[0] = tswap32(cpu_get_psr(e));
r->regs[1] = tswap32(env->pc);
r->regs[2] = tswap32(env->npc);
@@ -29,6 +54,8 @@ void elf_core_copy_regs(target_elf_gregset_t *r, const CPUArchState *env)
for (i = 0; i < 8; i++) {
r->regs[4 + i] = tswap32(env->gregs[i]);
r->regs[12 + i] = tswap32(env->regwptr[WREG_O0 + i]);
+ r->regs[20 + i] = tswap32(env->regwptr[WREG_L0 + i]);
+ r->regs[28 + i] = tswap32(env->regwptr[WREG_I0 + i]);
}
#endif
}
diff --git a/linux-user/sparc/target_elf.h b/linux-user/sparc/target_elf.h
index edb0b3103c..365af864b0 100644
--- a/linux-user/sparc/target_elf.h
+++ b/linux-user/sparc/target_elf.h
@@ -24,12 +24,21 @@
#define HAVE_ELF_CORE_DUMP 1
/*
- * Matches the kernel's elf_gregset_t (ELF_NGREG = 20).
- * sparc32/sparc32plus: psr, pc, npc, y, u_regs[16] (g0-g7, o0-o7)
- * sparc64: u_regs[16] (g0-g7, o0-o7), tstate, pc, npc, y
+ * Matches the kernel's elf_gregset_t.
+ * sparc32/sparc32plus (ELF_NGREG = 38):
+ * psr, pc, npc, y, u_regs[16] (g0-g7, o0-o7),
+ * reg_window[16] (l0-l7, i0-i7), stack_check[2]
+ * sparc64 (ELF_NGREG = 36):
+ * u_regs[16] (g0-g7, o0-o7), reg_window[16] (l0-l7, i0-i7),
+ * tstate, tpc, tnpc, y
*/
+#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
+# define TARGET_ELF_NGREG 36
+#else
+# define TARGET_ELF_NGREG 38
+#endif
typedef struct target_elf_gregset_t {
- abi_ulong regs[20];
+ abi_ulong regs[TARGET_ELF_NGREG];
} target_elf_gregset_t;
#endif
--
2.54.0
next prev parent reply other threads:[~2026-06-07 14:04 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-07 14:03 [PATCH 00/10] linux-user patches for alpha, sparc, sh4 and xtensa Helge Deller
2026-06-07 14:03 ` [PATCH 01/10] linux-user: implement fsmount(2) series of syscalls Helge Deller
2026-06-07 14:03 ` [PATCH 02/10] linux-user/strace: add fsmount " Helge Deller
2026-06-07 14:03 ` [PATCH 03/10] linux-user/alpha: add coredump support Helge Deller
2026-06-07 15:30 ` Richard Henderson
2026-06-07 14:03 ` [PATCH 04/10] linux-user/sparc: " Helge Deller
2026-06-07 14:03 ` [PATCH 05/10] linux-user/sparc: restore L/I registers from RSA in sparc64_set_context Helge Deller
2026-06-07 14:03 ` [PATCH 06/10] linux-user/sparc: call block_signals() before set_sigmask() in setcontext Helge Deller
2026-06-07 14:03 ` Helge Deller [this message]
2026-06-07 14:03 ` [PATCH 08/10] target/sh4: decode_gusa: recognize add#imm with prior mov Rm, Rn Helge Deller
2026-06-07 14:03 ` [PATCH 09/10] linux-user/xtensa: restore FP rounding mode on sigreturn Helge Deller
2026-06-07 20:06 ` Max Filippov
2026-06-07 14:03 ` [PATCH 10/10] target/xtensa: add cpu_set_fcr/fsr helpers to sync fp_status Helge Deller
2026-06-07 20:26 ` Max Filippov
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=20260607140356.10702-8-deller@kernel.org \
--to=deller@kernel.org \
--cc=deller@gmx.de \
--cc=jcmvbkbc@gmail.com \
--cc=laurent@vivier.eu \
--cc=mark.cave-ayland@ilande.co.uk \
--cc=mattst88@gmail.com \
--cc=pierrick.bouvier@oss.qualcomm.com \
--cc=qemu-devel@nongnu.org \
--cc=yoshinori.sato@nifty.com \
/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.