From nobody Sun Sep 28 16:30:37 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1756337111; cv=none; d=zohomail.com; s=zohoarc; b=LOgulti0q1K86r4JowCReT9GPevOteU50UcnXhMoIadLp5qfSHny6gwa5IZ6tGhCYRvqlITm3rmxGlL05i3Tq6y0iqdBr/VHulBReQc/PRPuxqLp1mZTX+vI1h3NJNqapd8r6lq4o7Ts++hjijTOwyT91mEyKT4TTynEmmGOQEM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756337111; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zolOYltkv8RezxxcgBErduiyzlKHX3ba4myoc0G9Zok=; b=JmuMZkAdsPEFqrzJ86ByiWQL2CeZsIecJg/hjNQoPetag3FkQI1zMVuD89k20XVxZ4lpde18zUuT66ZqEq6E9Kz9RvxNdbkyqtdFHFPzUSoNzaSthiyfX3IQmhcuEX0E/yuqEDxjlmLnsk/hpACPzeryZtejHS9JafizVlygjM8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1756337111864773.7512913956746; Wed, 27 Aug 2025 16:25:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1urPTH-0005lp-S2; Wed, 27 Aug 2025 19:22:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1urPSp-0004Ff-Eu for qemu-devel@nongnu.org; Wed, 27 Aug 2025 19:22:00 -0400 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1urPSg-0004Hp-Mx for qemu-devel@nongnu.org; Wed, 27 Aug 2025 19:21:59 -0400 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-7720f231151so531773b3a.1 for ; Wed, 27 Aug 2025 16:21:48 -0700 (PDT) Received: from stoup.. ([144.6.121.55]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7703ffebd33sm13986886b3a.29.2025.08.27.16.21.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Aug 2025 16:21:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756336905; x=1756941705; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zolOYltkv8RezxxcgBErduiyzlKHX3ba4myoc0G9Zok=; b=SjbOvKeDprO66bRCGakaxkR4F/qdLaFR0agk/mjr+67PGPCWXQp/AVc8ErleO9//SS R0d9Ryd/6XyJA4DZK1zO3KD4W7QVNihxV5GY5wu00KZ+kICRELovEUr6meK+qVOVJc1J euo4v+qfgzTZukHEN2MtV/uSJwxeXUx1cMAxwO6cUE2WR/bPPR78FdVWAyVRnkGFYGUE s2krZ1q4ekpQ+YVGI1kYkgwEIvrEv560vHyL2tH1nEiIqwiKobZoIutmZTkmYt+CSKh9 aKJpk8v8JYQupwyi4HB/d0DtaWHnCxIz7fpukr8ZGiYHo1oE8udAXc/akEhs49ap5cKz Aijg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756336905; x=1756941705; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zolOYltkv8RezxxcgBErduiyzlKHX3ba4myoc0G9Zok=; b=HtnN9jNLvJJPrw3PRgSJd5cbb2gnvWU+jJlTBRQQjygNppmzKzNTJBE2176iRA0o2f l4bjSLccoXsAOGT7TIqjzjfH9+qbqJ9dGIlDloaajCb9oATpJYxh8MGLWVdHZezHPA6u pJuD/9NFFCwSsorSxMZ8aS24GDb4+mqPa4o2jAHrfqlVTIjJdHU20qOFjucXsmV5aLKE sctvs8QQS9Peik/2qJGYQzFsRAJvC6GGsvzB+DETrVWmTqE0Jcu9K47m90LfX+mBporS FthOrSTteYXu9zfQ5jDJ55w9m4UWvLebfO9z96B7clVzOhpKAAbiKcEZxC9fpvf4kooY ACPQ== X-Gm-Message-State: AOJu0YzVjw7xCKdr2oVeHpgeaGYwY/HmPwaW2zp1Yw6TFAGm6P2MQz2F LEjG2eYKJZbW1YlXV3oOVGYSmtGj352I3AHCm/U9WCaK/hg8XieKIRh/tcUPpEYI+cv83PsqPEm kHaS+gRw= X-Gm-Gg: ASbGnct+UwFCHD56HeYboqxD7A0qA2MgGeq8nlgGMU19qU/rU7qVUmtfgBQLZ71hQLO 7VL9Ii3GvCDo2EzC97Oa0626DOIjb+5132OFxP3/0RgmXu5QU7/E6RY8+a/QNFCW9PHRMRTLKdt 5Wyr6Gr6ZZhZ1UAVpLfIPKisbFDN2Ly8ul8mYBbsOTJ/o02v+kLrpecZHxO8G8COjGdnirj+NLi 293XVbBu8nmGxdO098lYSYrJ1cj9BuZa+IBsoQcgetgL+oAYKtPfwd8QvtfqlOQPkPEcoHiU1uj jmpLds2WbfuoJ8qWHxlygK9+G4N14nY8guW4Iv6sXb+ZH1CJFLu+MgXuc9MGKfRxrijRbvkml79 bedPHTXQTWn9xpMv1RZORfr4Bzg== X-Google-Smtp-Source: AGHT+IGEN9d8Z1HWeaBkbROoUhKrHOKCFJMONeu3vywGY5ScbeiOxthcc48/6xRk9vfXGKDG/tgMfg== X-Received: by 2002:a05:6a00:1147:b0:76e:7ae5:ec90 with SMTP id d2e1a72fcca58-7702fad63b5mr23534959b3a.26.1756336905438; Wed, 27 Aug 2025 16:21:45 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Peter Maydell Subject: [PULL 26/46] linux-user/i386: Create init_main_thread Date: Thu, 28 Aug 2025 09:20:03 +1000 Message-ID: <20250827232023.50398-27-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250827232023.50398-1-richard.henderson@linaro.org> References: <20250827232023.50398-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::436; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x436.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1756337128916124100 Content-Type: text/plain; charset="utf-8" Merge init_thread and target_cpu_copy_regs. There's no point going through a target_pt_regs intermediate. Temporarily introduce HAVE_INIT_MAIN_THREAD during conversion. Reviewed-by: Peter Maydell Signed-off-by: Richard Henderson --- linux-user/qemu.h | 1 + linux-user/elfload.c | 29 +++++------------------ linux-user/i386/cpu_loop.c | 48 +++++++++++++++++--------------------- 3 files changed, 29 insertions(+), 49 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 0c3cfe93a1..8a9500d4f4 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -361,5 +361,6 @@ CPUArchState *cpu_copy(CPUArchState *env); =20 typedef struct target_pt_regs target_pt_regs; void target_cpu_copy_regs(CPUArchState *env, target_pt_regs *regs); +void init_main_thread(CPUState *cs, struct image_info *info); =20 #endif /* QEMU_H */ diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 6fce74f45a..89f3972253 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -149,17 +149,12 @@ typedef abi_int target_pid_t; =20 #ifdef TARGET_I386 =20 +#define HAVE_INIT_MAIN_THREAD + #ifdef TARGET_X86_64 #define ELF_CLASS ELFCLASS64 #define ELF_ARCH EM_X86_64 =20 -static inline void init_thread(struct target_pt_regs *regs, struct image_i= nfo *infop) -{ - regs->rax =3D 0; - regs->rsp =3D infop->start_stack; - regs->rip =3D infop->entry; -} - #define ELF_NREG 27 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; =20 @@ -237,22 +232,6 @@ static bool init_guest_commpage(void) =20 #define EXSTACK_DEFAULT true =20 -static inline void init_thread(struct target_pt_regs *regs, - struct image_info *infop) -{ - regs->esp =3D infop->start_stack; - regs->eip =3D infop->entry; - - /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program - starts %edx contains a pointer to a function which might be - registered using `atexit'. This provides a mean for the - dynamic linker to call DT_FINI functions for shared libraries - that have been loaded before the code runs. - - A value of 0 tells we have no such handler. */ - regs->edx =3D 0; -} - #define ELF_NREG 17 typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; =20 @@ -3621,8 +3600,12 @@ static int elf_core_dump(int signr, const CPUArchSta= te *env) =20 void do_init_main_thread(CPUState *cs, struct image_info *infop) { +#ifdef HAVE_INIT_MAIN_THREAD + init_main_thread(cs, infop); +#else target_pt_regs regs =3D { }; =20 init_thread(®s, infop); target_cpu_copy_regs(cpu_env(cs), ®s); +#endif } diff --git a/linux-user/i386/cpu_loop.c b/linux-user/i386/cpu_loop.c index d96d5553fa..7b2d8b03d8 100644 --- a/linux-user/i386/cpu_loop.c +++ b/linux-user/i386/cpu_loop.c @@ -331,11 +331,10 @@ static void target_cpu_free(void *obj) g_free(obj); } =20 -void target_cpu_copy_regs(CPUArchState *env, target_pt_regs *regs) +void init_main_thread(CPUState *cpu, struct image_info *info) { - CPUState *cpu =3D env_cpu(env); + CPUArchState *env =3D cpu_env(cpu); bool is64 =3D (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) !=3D= 0; - int i; =20 OBJECT(cpu)->free =3D target_cpu_free; env->cr[0] =3D CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK; @@ -361,28 +360,25 @@ void target_cpu_copy_regs(CPUArchState *env, target_p= t_regs *regs) /* flags setup : we activate the IRQs by default as in user mode */ env->eflags |=3D IF_MASK; =20 - /* linux register setup */ -#ifndef TARGET_ABI32 - env->regs[R_EAX] =3D regs->rax; - env->regs[R_EBX] =3D regs->rbx; - env->regs[R_ECX] =3D regs->rcx; - env->regs[R_EDX] =3D regs->rdx; - env->regs[R_ESI] =3D regs->rsi; - env->regs[R_EDI] =3D regs->rdi; - env->regs[R_EBP] =3D regs->rbp; - env->regs[R_ESP] =3D regs->rsp; - env->eip =3D regs->rip; -#else - env->regs[R_EAX] =3D regs->eax; - env->regs[R_EBX] =3D regs->ebx; - env->regs[R_ECX] =3D regs->ecx; - env->regs[R_EDX] =3D regs->edx; - env->regs[R_ESI] =3D regs->esi; - env->regs[R_EDI] =3D regs->edi; - env->regs[R_EBP] =3D regs->ebp; - env->regs[R_ESP] =3D regs->esp; - env->eip =3D regs->eip; -#endif + /* + * Linux register setup. + * + * SVR4/i386 ABI (pages 3-31, 3-32) says that when the program + * starts %edx contains a pointer to a function which might be + * registered using `atexit'. This provides a mean for the + * dynamic linker to call DT_FINI functions for shared libraries + * that have been loaded before the code runs. + * A value of 0 tells we have no such handler. + * + * This applies to x86_64 as well as i386. + * + * That said, the kernel's ELF_PLAT_INIT simply zeros all of the gener= al + * registers. Note that x86_cpu_reset_hold will set %edx to cpuid_ver= sion; + * clear all general registers defensively. + */ + memset(env->regs, 0, sizeof(env->regs)); + env->regs[R_ESP] =3D info->start_stack; + env->eip =3D info->entry; =20 /* linux interrupt setup */ #ifndef TARGET_ABI32 @@ -394,7 +390,7 @@ void target_cpu_copy_regs(CPUArchState *env, target_pt_= regs *regs) PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); idt_table =3D g2h_untagged(env->idt.base); - for (i =3D 0; i < 20; i++) { + for (int i =3D 0; i < 20; i++) { set_idt(i, 0, is64); } set_idt(3, 3, is64); --=20 2.43.0