From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Subject: [PATCH 24/89] linux-user/arm: Create init_main_thread
Date: Tue, 29 Jul 2025 13:58:57 -1000	[thread overview]
Message-ID: <20250730000003.599084-25-richard.henderson@linaro.org> (raw)
In-Reply-To: <20250730000003.599084-1-richard.henderson@linaro.org>
Merge init_thread and target_cpu_copy_regs.
There's no point going through a target_pt_regs intermediate.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 linux-user/arm/cpu_loop.c | 53 ++++++++++++++++++++++++++++++++-------
 linux-user/elfload.c      | 41 +-----------------------------
 2 files changed, 45 insertions(+), 49 deletions(-)
diff --git a/linux-user/arm/cpu_loop.c b/linux-user/arm/cpu_loop.c
index 1f3bb96484..8974b35e8d 100644
--- a/linux-user/arm/cpu_loop.c
+++ b/linux-user/arm/cpu_loop.c
@@ -480,19 +480,54 @@ void cpu_loop(CPUARMState *env)
     }
 }
 
-void target_cpu_copy_regs(CPUArchState *env, target_pt_regs *regs)
+void init_main_thread(CPUState *cs, struct image_info *info)
 {
-    cpsr_write(env, regs->uregs[16], CPSR_USER | CPSR_EXEC,
-               CPSRWriteByInstr);
-    for (int i = 0; i < 16; i++) {
-        env->regs[i] = regs->uregs[i];
+    CPUARMState *env = cpu_env(cs);
+    abi_ptr stack = info->start_stack;
+    abi_ptr entry = info->entry;
+
+    cpsr_write(env, ARM_CPU_MODE_USR | (entry & 1 ? CPSR_T : 0),
+               CPSR_USER | CPSR_EXEC, CPSRWriteByInstr);
+
+    env->regs[15] = entry & 0xfffffffe;
+    env->regs[13] = stack;
+
+    /* FIXME - what to for failure of get_user()? */
+    /* FIXME - a modern kernel does not do this? */
+    get_user_ual(env->regs[2], stack + 8); /* envp */
+    get_user_ual(env->regs[1], stack + 4); /* envp */
+
+    /*
+     * Per the SVR4 ABI, r0 contains a pointer to a function to be
+     * registered with atexit.  A value of 0 means we have no such handler.
+     */
+    env->regs[0] = 0;
+
+    /* For uClinux PIC binaries.  */
+    /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
+    env->regs[10] = info->start_data;
+
+    /* Support ARM FDPIC.  */
+    if (info_is_fdpic(info)) {
+        /*
+         * As described in the ABI document, r7 points to the loadmap info
+         * prepared by the kernel. If an interpreter is needed, r8 points
+         * to the interpreter loadmap and r9 points to the interpreter
+         * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
+         * r9 points to the main program PT_DYNAMIC info.
+         */
+        env->regs[7] = info->loadmap_addr;
+        if (info->interpreter_loadmap_addr) {
+            /* Executable is dynamically loaded.  */
+            env->regs[8] = info->interpreter_loadmap_addr;
+            env->regs[9] = info->interpreter_pt_dynamic_addr;
+        } else {
+            env->regs[8] = 0;
+            env->regs[9] = info->pt_dynamic_addr;
+        }
     }
 
     if (TARGET_BIG_ENDIAN) {
-        CPUState *cpu = env_cpu(env);
-        TaskState *ts = get_task_state(cpu);
-        struct image_info *info = ts->info;
-
         /* Enable BE8.  */
         if (EF_ARM_EABI_VERSION(info->elf_flags) >= EF_ARM_EABI_VER4
             && (info->elf_flags & EF_ARM_BE8)) {
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 553d632d46..a9a1d7222a 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -299,46 +299,7 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *en
 #define ELF_CLASS       ELFCLASS32
 #define EXSTACK_DEFAULT true
 
-static inline void init_thread(struct target_pt_regs *regs,
-                               struct image_info *infop)
-{
-    abi_long stack = infop->start_stack;
-    memset(regs, 0, sizeof(*regs));
-
-    regs->uregs[16] = ARM_CPU_MODE_USR;
-    if (infop->entry & 1) {
-        regs->uregs[16] |= CPSR_T;
-    }
-    regs->uregs[15] = infop->entry & 0xfffffffe;
-    regs->uregs[13] = infop->start_stack;
-    /* FIXME - what to for failure of get_user()? */
-    get_user_ual(regs->uregs[2], stack + 8); /* envp */
-    get_user_ual(regs->uregs[1], stack + 4); /* envp */
-    /* XXX: it seems that r0 is zeroed after ! */
-    regs->uregs[0] = 0;
-    /* For uClinux PIC binaries.  */
-    /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
-    regs->uregs[10] = infop->start_data;
-
-    /* Support ARM FDPIC.  */
-    if (info_is_fdpic(infop)) {
-        /* As described in the ABI document, r7 points to the loadmap info
-         * prepared by the kernel. If an interpreter is needed, r8 points
-         * to the interpreter loadmap and r9 points to the interpreter
-         * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
-         * r9 points to the main program PT_DYNAMIC info.
-         */
-        regs->uregs[7] = infop->loadmap_addr;
-        if (infop->interpreter_loadmap_addr) {
-            /* Executable is dynamically loaded.  */
-            regs->uregs[8] = infop->interpreter_loadmap_addr;
-            regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
-        } else {
-            regs->uregs[8] = 0;
-            regs->uregs[9] = infop->pt_dynamic_addr;
-        }
-    }
-}
+#define HAVE_INIT_MAIN_THREAD
 
 #define ELF_NREG    18
 typedef target_elf_greg_t  target_elf_gregset_t[ELF_NREG];
-- 
2.43.0
next prev parent reply	other threads:[~2025-07-30  0:21 UTC|newest]
Thread overview: 187+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-29 23:58 [PATCH for-10.2 00/89] linux-user: Split up elfload.c Richard Henderson
2025-07-29 23:58 ` [PATCH 01/89] linux-user: Create target/elfload.c files Richard Henderson
2025-08-01 14:47   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 02/89] linux-user: Move get_elf_hwcap to {i386, x86_64}/elfload.c Richard Henderson
2025-08-01 14:51   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 03/89] linux-user: Move hwcap functions to {arm, aarch64}/elfload.c Richard Henderson
2025-08-01 14:53   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 04/89] linux-user: Move get_elf_hwcap to sparc/elfload.c Richard Henderson
2025-08-01 14:53   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 05/89] linux-user: Move hwcap functions to ppc/elfload.c Richard Henderson
2025-08-01 14:54   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 06/89] linux-user: Move get_elf_hwcap to loongarch64/elfload.c Richard Henderson
2025-08-01 14:54   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 07/89] linux-user: Move get_elf_hwcap to mips/elfload.c Richard Henderson
2025-08-01 14:55   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 08/89] linux-user: Move get_elf_hwcap to sh4/elfload.c Richard Henderson
2025-08-01 14:56   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 09/89] linux-user: Move hwcap functions to s390x/elfload.c Richard Henderson
2025-08-01 14:56   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 10/89] linux-user: Move get_elf_hwcap to riscv/elfload.c Richard Henderson
2025-08-01 14:57   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 11/89] linux-user: Remove ELF_HWCAP Richard Henderson
2025-08-01 15:06   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 12/89] linux-user: Remove ELF_HWCAP2 Richard Henderson
2025-08-01 15:09   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 13/89] linux-user: Move get_elf_platform to {i386, x86_64}/elfload.c Richard Henderson
2025-08-01 15:26   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 14/89] linux-user/i386: Return const data from get_elf_platform Richard Henderson
2025-08-01 15:28   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 15/89] linux-user: Move get_elf_platform to arm/elfload.c Richard Henderson
2025-08-01 15:30   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 16/89] linux-user/loongarch64: Create get_elf_platform Richard Henderson
2025-08-01 15:30   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 17/89] linux-user/hppa: " Richard Henderson
2025-08-01 15:32   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 18/89] linux-user: Remove ELF_PLATFORM Richard Henderson
2025-08-01 15:33   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 19/89] linux-user: Move get_elf_base_platform to mips/elfload.c Richard Henderson
2025-08-01 15:35   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 20/89] linux-user: Move target_cpu_copy_regs decl to qemu.h Richard Henderson
2025-08-01 15:36   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 21/89] linux-user: Unify init of semihosting fields in TaskState Richard Henderson
2025-08-01 16:05   ` Peter Maydell
2025-08-01 21:04     ` Richard Henderson
2025-07-29 23:58 ` [PATCH 22/89] linux-user: Create do_init_main_thread Richard Henderson
2025-08-01 16:13   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 23/89] linux-user/i386: Create init_main_thread Richard Henderson
2025-08-01 16:17   ` Peter Maydell
2025-08-01 21:14     ` Richard Henderson
2025-07-29 23:58 ` Richard Henderson [this message]
2025-08-01 16:33   ` [PATCH 24/89] linux-user/arm: " Peter Maydell
2025-07-29 23:58 ` [PATCH 25/89] linux-user/aarch64: " Richard Henderson
2025-08-01 16:33   ` Peter Maydell
2025-07-29 23:58 ` [PATCH 26/89] linux-user/sparc: " Richard Henderson
2025-08-01 17:24   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 27/89] linux-user/ppc: " Richard Henderson
2025-08-01 17:26   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 28/89] linux-user/loongarch64: " Richard Henderson
2025-08-01 17:27   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 29/89] linux-user/mips: " Richard Henderson
2025-08-01 17:27   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 30/89] linux-user/microblaze: " Richard Henderson
2025-08-01 17:28   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 31/89] linux-user/openrisc: " Richard Henderson
2025-08-01 17:29   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 32/89] linux-user/sh4: " Richard Henderson
2025-08-01 17:30   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 33/89] linux-user/m68k: " Richard Henderson
2025-08-01 17:30   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 34/89] linux-user/alpha: " Richard Henderson
2025-08-01 17:31   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 35/89] linux-user/s390x: " Richard Henderson
2025-08-01 17:32   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 36/89] linux-user/riscv: " Richard Henderson
2025-08-01 17:32   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 37/89] linux-user/hppa: " Richard Henderson
2025-08-01 17:33   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 38/89] linux-user/xtensa: " Richard Henderson
2025-08-01 17:34   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 39/89] linux-user/hexagon: " Richard Henderson
2025-08-01 17:35   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 40/89] linux-user: Remove do_init_main_thread Richard Henderson
2025-08-01 17:37   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 41/89] linux-user/x86_64: Split out target_coredump.c.inc Richard Henderson
2025-08-01 17:45   ` Peter Maydell
2025-08-01 21:44     ` Richard Henderson
2025-08-02 11:13       ` Peter Maydell
2025-07-29 23:59 ` [PATCH 42/89] linux-user/i386: " Richard Henderson
2025-08-01 17:46   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 43/89] linux-user/arm: " Richard Henderson
2025-08-01 17:46   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 44/89] linux-user/aarch64: " Richard Henderson
2025-08-01 17:46   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 45/89] linux-user/ppc: " Richard Henderson
2025-08-01 17:46   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 46/89] linux-user/loongarch64: " Richard Henderson
2025-08-01 17:47   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 47/89] linux-user/mips: " Richard Henderson
2025-08-01 17:47   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 48/89] linux-user/microblaze: " Richard Henderson
2025-08-01 17:47   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 49/89] target/openrisc: " Richard Henderson
2025-08-01 17:48   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 50/89] target/sh4: " Richard Henderson
2025-08-01 17:49   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 51/89] linux-user/m68k: " Richard Henderson
2025-08-01 17:49   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 52/89] linux-user/s390x: " Richard Henderson
2025-08-01 17:49   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 53/89] linux-user/xtensa: " Richard Henderson
2025-08-01 17:50   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 54/89] linux-user: Unify the include of target_coredump.c.inc Richard Henderson
2025-08-01 17:50   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 55/89] linux-user: Move init_guest_commpage to x86_64/elfload.c Richard Henderson
2025-08-01 18:07   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 56/89] linux-user: Move init_guest_commpage to arm/elfload.c Richard Henderson
2025-08-01 18:08   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 57/89] linux-user: Move init_guest_commpage to hppa/elfload.c Richard Henderson
2025-08-01 18:08   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 58/89] linux-user: Remove INIT_GUEST_COMMPAGE Richard Henderson
2025-08-01 18:09   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 59/89] linux-user: Move get_vdso_image_info to arm/elfload.c Richard Henderson
2025-08-01 18:11   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 60/89] linux-user: Remove ELF_EXEC_PAGESIZE Richard Henderson
2025-08-01 18:12   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 61/89] linux-user: Move get_elf_cpu_model to target/elfload.c Richard Henderson
2025-08-02 20:14   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 62/89] linux-user: Move ppc uabi/asm/elf.h workaround to osdep.h Richard Henderson
2025-08-02 20:22   ` Peter Maydell
2025-08-02 22:00     ` Richard Henderson
2025-07-29 23:59 ` [PATCH 63/89] linux-user: Move elf parameters to {i386, x86_64}/target_elf.h Richard Henderson
2025-08-02 20:24   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 64/89] linux-user: Move elf parameters to {arm, aarch64}/target_elf.h Richard Henderson
2025-08-02 20:24   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 65/89] linux-user: Move elf parameters to sparc/target_elf.h Richard Henderson
2025-08-02 20:25   ` Peter Maydell
2025-08-02 20:28   ` Peter Maydell
2025-08-02 22:05     ` Richard Henderson
2025-07-29 23:59 ` [PATCH 66/89] linux-user: Move elf parameters to ppc/target_elf.h Richard Henderson
2025-08-02 20:26   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 67/89] linux-user: Move elf parameters to loongarch64/target_elf.h Richard Henderson
2025-08-02 20:26   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 68/89] linux-user: Move elf parameters to {mips, mips64}/target_elf.h Richard Henderson
2025-08-02 20:27   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 69/89] linux-user: Move elf parameters to microblaze/target_elf.h Richard Henderson
2025-08-02 20:28   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 70/89] linux-user: Move elf parameters to openrisc/target_elf.h Richard Henderson
2025-08-02 20:29   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 71/89] linux-user: Move elf parameters to sh4/target_elf.h Richard Henderson
2025-08-02 20:29   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 72/89] linux-user: Move elf parameters to m68k/target_elf.h Richard Henderson
2025-08-02 20:30   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 73/89] linux-user: Move elf parameters to alpha/target_elf.h Richard Henderson
2025-08-02 20:30   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 74/89] linux-user: Move elf parameters to s390x/target_elf.h Richard Henderson
2025-08-02 20:30   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 75/89] linux-user: Move elf parameters to riscv/target_elf.h Richard Henderson
2025-08-02 20:31   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 76/89] linux-user: Move elf parameters to hppa/target_elf.h Richard Henderson
2025-08-02 20:31   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 77/89] linux-user: Move elf parameters to xtensa/target_elf.h Richard Henderson
2025-08-02 20:31   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 78/89] linux-user: Move elf parameters to hexagon/target_elf.h Richard Henderson
2025-08-02 20:32   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 79/89] linux-user: Standardize on ELF_MACHINE not ELF_ARCH Richard Henderson
2025-08-02 20:34   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 80/89] linux-user: Rename elf_check_arch Richard Henderson
2025-08-02 20:35   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 81/89] linux-user: Remove ELIBBAD from elfload.c Richard Henderson
2025-08-02 20:37   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 82/89] linux-user: Remove MAP_DENYWRITE " Richard Henderson
2025-08-02 20:38   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 83/89] linux-user: Move arch_parse_elf_property to aarch64/elfload.c Richard Henderson
2025-08-02 20:39   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 84/89] linux-user: Remove a.out declarations from elfload.c Richard Henderson
2025-08-02 20:41   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 85/89] linux-user/sparc: Move target_pt_regs to signal.c Richard Henderson
2025-08-02 20:41   ` Peter Maydell
2025-07-29 23:59 ` [PATCH 86/89] linux-user/microblaze: " Richard Henderson
2025-08-02 20:40   ` Peter Maydell
2025-07-30  0:00 ` [PATCH 87/89] linux-user/openrisc: " Richard Henderson
2025-08-02 20:43   ` Peter Maydell
2025-07-30  0:00 ` [PATCH 88/89] linux-user/s390x: Move target_psw_t " Richard Henderson
2025-08-02 20:42   ` Peter Maydell
2025-07-30  0:00 ` [PATCH 89/89] linux-user: Remove struct target_pt_regs from target_syscall.h Richard Henderson
2025-08-02 20:45   ` Peter Maydell
2025-08-02 20:51 ` [PATCH for-10.2 00/89] linux-user: Split up elfload.c Peter Maydell
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=20250730000003.599084-25-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).