From nobody Sat Sep 6 16:56:44 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=1756974086; cv=none; d=zohomail.com; s=zohoarc; b=a2+5f1EipTdFUGFSjf0Ytogr8pqHfVyX8EkByhQOCREP5fX3VL6BYzjx5xMb9evRBMkhdo+d2FykiK4OTeGgxU3FibIrZoScaPGACnPo2mghXnp60cDCLLIXU/OyBLV4/nj8TO8T7liIdeAgx7bBhN13AXXnFhXYV6VWEA1Y9B4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1756974086; 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=KM2ZDhNYkLW1KWUOdLmFVe8gLAQ7wgd1StjOThDS9B0=; b=Zq/PR2YE39QXKSHLFSNU5ljZ2trv1TpeQWlvAdDqD6QdxAD8br1Yvw4CFoMYqd/wRmqnJqkEn/KUl4dQJ2EQjJ+tJomZDu46xkRNCpVSBG7ut/4AdCOJ01LKTygNZZTEK67pqrTRFGZ5Bm8XB94WWKKQw2bkvjj7+zfOv/0lm7g= 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 1756974086349532.687772956092; Thu, 4 Sep 2025 01:21:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uu58u-00042D-2u; Thu, 04 Sep 2025 04:16:31 -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 1uu558-0006ba-8A for qemu-devel@nongnu.org; Thu, 04 Sep 2025 04:12:37 -0400 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uu54Z-0004Cg-RB for qemu-devel@nongnu.org; Thu, 04 Sep 2025 04:12:32 -0400 Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-b043da5a55fso107840166b.0 for ; Thu, 04 Sep 2025 01:11:59 -0700 (PDT) Received: from draig.lan ([185.126.160.19]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b041f6fb232sm1080452766b.87.2025.09.04.01.11.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Sep 2025 01:11:55 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 30E81601A8; Thu, 04 Sep 2025 09:11:34 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756973518; x=1757578318; 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=KM2ZDhNYkLW1KWUOdLmFVe8gLAQ7wgd1StjOThDS9B0=; b=JISPeP60TaaTAmN88DaImEbB/tI2yBQj4Bl+oV9mNYxY66JWiLSm9X6Lp4NmPIPjgG Kz4yRRBqXIS2ymgpsfyHUHauwsqPGk9d18Xo983MlC6PR/2JMXkNexjsI5qDQlSAJhPK rS7OZfTGP4/vXI2VKK9gRN0saArVXaXABOWncUXXqoeXRBZR198DYyeqQaVnNxvYodr1 cPB0vVoiP3Ahid7IZXqVitFSg98hdzws24587LeOV0jHRlV0q2IJbQxKKEoZwylO1OG3 mj+98AxuFMvQAgDr8PkpaNts7tW9NDN0YAngMuzEomhLHGL3MqGAyM++bYy15IRqlifG Mbvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756973518; x=1757578318; 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=KM2ZDhNYkLW1KWUOdLmFVe8gLAQ7wgd1StjOThDS9B0=; b=vVI3Rk0vHFxDMqE5K1XQokHhoqew4So4kdD8ZWPjZqRdaNzeUncNs9DhSE4As5I5mh GzDJQWcIIvNEC3YAogXwzoyS9tr9JMJ7V42aQR5MqYeWZ38xNZXAW5EzytbE7tarKisi QSfBhghECRM+/AApdsqrbrkYtsJZslfALl9uo7GLSrEDSK7aBjJsAQRXeIyU+1Ehduzz +APkV6G++HPchDo9cLcfM5kKJxVDqyJNyXWrX1E90C/znQDeBYuiwR2Fsl7RadOdLfV/ /0/JK1Y/NAchBlNL5jiXWgc3WZHV26PL29t+v8/YApTSZZ0Th2Oh9NXN7RHwB0yZNv8Q Mk+w== X-Gm-Message-State: AOJu0YzE3BmEmo93Sv4N0VZ3IITASfx3GeVOH5PDAPmM0x9k0OfrTEg8 i0cC2P3ZTPPhcyx0Gusj/tSue+59M3NXgDIwNrGesA+BMpNSzzsxKAdHq95ENQREwicIE200rsO AQiiGpRisaQ== X-Gm-Gg: ASbGnculkVvBfvvIbgvA2yPHOk3r/RXT5HjKpz04U1l1A2KXiY5YKLQVN3fEjhC6tML 6EPi0IzcoaQGI7UmKqltIFRYu2ov3hl2/LHd2CMH6czN2WHPh6m1O1DOt02EJIPlVcb4YhQnHGh r5U+5XTgnGDXEYdRL9mq5DnNl//oD+qqLb/BfAsHGmVm5ZSWIyf0dnkeOjXF3/2Aqf1JAdOAG6F JktP49qo2F3ElsJRr66B7BwbChR/tv5A63QKKAz6KvOTW9Prjt019it4raCrMMvUZZMpgG0ltoE yaCZeR07Im61JcnARAZKK3Uds0pPGN4NCqZ/WP4U3rHMXH43NRsjC3afhcNRVQon0BuebSaLZ1w l5Skoaxvut6zTKvTjozUxukHd2LQatoKXPw== X-Google-Smtp-Source: AGHT+IE6k0GqNytWpGYUw8aMikHxjbuztoWFqvzmVO/on4V3dK6nYD0ba2/sDJcNk3Tb6kLpXPbyDw== X-Received: by 2002:a17:907:961a:b0:b04:2edd:280b with SMTP id a640c23a62f3a-b042edd2a50mr1478196866b.39.1756973517480; Thu, 04 Sep 2025 01:11:57 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Reinoud Zandijk , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Huacai Chen , Zhao Liu , Paolo Bonzini , Alistair Francis , qemu-arm@nongnu.org, Fam Zheng , Helge Deller , Matthew Rosato , Fabiano Rosas , qemu-rust@nongnu.org, Bibo Mao , qemu-riscv@nongnu.org, Thanos Makatos , Liu Zhiwei , Riku Voipio , Cameron Esfahani , Alexander Graf , Laurent Vivier , Harsh Prateek Bora , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , qemu-ppc@nongnu.org, Stafford Horne , Sunil Muthuswamy , Jagannathan Raman , Igor Mammedov , Brian Cain , Phil Dennis-Jordan , devel@lists.libvirt.org, Mads Ynddal , Elena Ufimtseva , Peter Xu , Jason Herne , Michael Rolnik , Weiwei Li , Laurent Vivier , Ilya Leoshkevich , qemu-block@nongnu.org, Peter Maydell , Kostiantyn Kostiuk , Kyle Evans , David Hildenbrand , "Edgar E. Iglesias" , Warner Losh , Daniel Henrique Barboza , John Snow , Yoshinori Sato , Aleksandar Rikalo , Alistair Francis , Marcelo Tosatti , Yonggang Luo , Radoslaw Biernacki , Artyom Tarasenko , Yanan Wang , Eduardo Habkost , Aurelien Jarno , Richard Henderson , qemu-s390x@nongnu.org, Alex Williamson , "Michael S. Tsirkin" , Ani Sinha , Roman Bolshakov , Manos Pitsidianakis , Chinmay Rath , Thomas Huth , Cleber Rosa , kvm@vger.kernel.org, Song Gao , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Halil Pasic , Eric Farman , Palmer Dabbelt , Leif Lindholm , Christian Borntraeger , Michael Roth , Mauro Carvalho Chehab , Jiaxun Yang , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Mark Cave-Ayland , Marcel Apfelbaum , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Nicholas Piggin , John Levon Subject: [PATCH v2 049/281] semihosting: Initialize heap once per process Date: Thu, 4 Sep 2025 09:07:23 +0100 Message-ID: <20250904081128.1942269-50-alex.bennee@linaro.org> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250904081128.1942269-1-alex.bennee@linaro.org> References: <20250904081128.1942269-1-alex.bennee@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=2a00:1450:4864:20::62f; envelope-from=alex.bennee@linaro.org; helo=mail-ej1-x62f.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=unavailable 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: 1756974087846116600 Content-Type: text/plain; charset="utf-8" From: Richard Henderson While semihosting isn't really thread aware, the current implementation allocates space for the heap per-thread. Remove the heap_base and heap_limit fields from TaskState. Replace with static variables within do_common_semihosting. Reviewed-by: Peter Maydell Signed-off-by: Richard Henderson --- linux-user/qemu.h | 5 ----- linux-user/aarch64/cpu_loop.c | 7 ------- linux-user/arm/cpu_loop.c | 25 +++++++++++-------------- linux-user/m68k/cpu_loop.c | 8 -------- linux-user/riscv/cpu_loop.c | 4 ---- semihosting/arm-compat-semi.c | 22 +++++++++------------- 6 files changed, 20 insertions(+), 51 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index b6621536b36..4d6fad28c63 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -121,11 +121,6 @@ struct TaskState { abi_ulong child_tidptr; #ifdef TARGET_M68K abi_ulong tp_value; -#endif -#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV) - /* Extra fields for semihosted binaries. */ - abi_ulong heap_base; - abi_ulong heap_limit; #endif int used; /* non zero if used */ struct image_info *info; diff --git a/linux-user/aarch64/cpu_loop.c b/linux-user/aarch64/cpu_loop.c index b65999a75bf..030a630c936 100644 --- a/linux-user/aarch64/cpu_loop.c +++ b/linux-user/aarch64/cpu_loop.c @@ -140,9 +140,6 @@ void cpu_loop(CPUARMState *env) void target_cpu_copy_regs(CPUArchState *env, target_pt_regs *regs) { ARMCPU *cpu =3D env_archcpu(env); - CPUState *cs =3D env_cpu(env); - TaskState *ts =3D get_task_state(cs); - struct image_info *info =3D ts->info; int i; =20 if (!(arm_feature(env, ARM_FEATURE_AARCH64))) { @@ -167,8 +164,4 @@ void target_cpu_copy_regs(CPUArchState *env, target_pt_= regs *regs) if (cpu_isar_feature(aa64_pauth, cpu)) { qemu_guest_getrandom_nofail(&env->keys, sizeof(env->keys)); } - - ts->heap_base =3D info->brk; - /* This will be filled in on the first SYS_HEAPINFO call. */ - ts->heap_limit =3D 0; } diff --git a/linux-user/arm/cpu_loop.c b/linux-user/arm/cpu_loop.c index e40d6beafa2..9d54422736c 100644 --- a/linux-user/arm/cpu_loop.c +++ b/linux-user/arm/cpu_loop.c @@ -492,19 +492,16 @@ void target_cpu_copy_regs(CPUArchState *env, target_p= t_regs *regs) for(i =3D 0; i < 16; i++) { env->regs[i] =3D regs->uregs[i]; } -#if TARGET_BIG_ENDIAN - /* Enable BE8. */ - if (EF_ARM_EABI_VERSION(info->elf_flags) >=3D EF_ARM_EABI_VER4 - && (info->elf_flags & EF_ARM_BE8)) { - env->uncached_cpsr |=3D CPSR_E; - env->cp15.sctlr_el[1] |=3D SCTLR_E0E; - } else { - env->cp15.sctlr_el[1] |=3D SCTLR_B; - } - arm_rebuild_hflags(env); -#endif =20 - ts->heap_base =3D info->brk; - /* This will be filled in on the first SYS_HEAPINFO call. */ - ts->heap_limit =3D 0; + if (TARGET_BIG_ENDIAN) { + /* Enable BE8. */ + if (EF_ARM_EABI_VERSION(info->elf_flags) >=3D EF_ARM_EABI_VER4 + && (info->elf_flags & EF_ARM_BE8)) { + env->uncached_cpsr |=3D CPSR_E; + env->cp15.sctlr_el[1] |=3D SCTLR_E0E; + } else { + env->cp15.sctlr_el[1] |=3D SCTLR_B; + } + arm_rebuild_hflags(env); + } } diff --git a/linux-user/m68k/cpu_loop.c b/linux-user/m68k/cpu_loop.c index 3aaaf02ca4e..23693f33582 100644 --- a/linux-user/m68k/cpu_loop.c +++ b/linux-user/m68k/cpu_loop.c @@ -94,10 +94,6 @@ void cpu_loop(CPUM68KState *env) =20 void target_cpu_copy_regs(CPUArchState *env, target_pt_regs *regs) { - CPUState *cpu =3D env_cpu(env); - TaskState *ts =3D get_task_state(cpu); - struct image_info *info =3D ts->info; - env->pc =3D regs->pc; env->dregs[0] =3D regs->d0; env->dregs[1] =3D regs->d1; @@ -116,8 +112,4 @@ void target_cpu_copy_regs(CPUArchState *env, target_pt_= regs *regs) env->aregs[6] =3D regs->a6; env->aregs[7] =3D regs->usp; env->sr =3D regs->sr; - - ts->heap_base =3D info->brk; - /* This will be filled in on the first SYS_HEAPINFO call. */ - ts->heap_limit =3D 0; } diff --git a/linux-user/riscv/cpu_loop.c b/linux-user/riscv/cpu_loop.c index 541de765ffa..2dd30c7b288 100644 --- a/linux-user/riscv/cpu_loop.c +++ b/linux-user/riscv/cpu_loop.c @@ -108,8 +108,4 @@ void target_cpu_copy_regs(CPUArchState *env, target_pt_= regs *regs) error_report("Incompatible ELF: RVE cpu requires RVE ABI binary"); exit(EXIT_FAILURE); } - - ts->heap_base =3D info->brk; - /* This will be filled in on the first SYS_HEAPINFO call. */ - ts->heap_limit =3D 0; } diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index bc04b02eba8..bcd13cd6dfd 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -666,7 +666,7 @@ void do_common_semihosting(CPUState *cs) int i; #ifdef CONFIG_USER_ONLY TaskState *ts =3D get_task_state(cs); - target_ulong limit; + static abi_ulong heapbase, heaplimit; #else LayoutInfo info =3D common_semi_find_bases(cs); #endif @@ -678,24 +678,20 @@ void do_common_semihosting(CPUState *cs) * Some C libraries assume the heap immediately follows .bss, = so * allocate it using sbrk. */ - if (!ts->heap_limit) { - abi_ulong ret; - - ts->heap_base =3D do_brk(0); - limit =3D ts->heap_base + COMMON_SEMI_HEAP_SIZE; + if (!heaplimit) { + heapbase =3D do_brk(0); /* Try a big heap, and reduce the size if that fails. */ - for (;;) { - ret =3D do_brk(limit); + for (abi_ulong size =3D COMMON_SEMI_HEAP_SIZE; ; size >>= =3D 1) { + abi_ulong limit =3D heapbase + size; + abi_ulong ret =3D do_brk(limit); if (ret >=3D limit) { + heaplimit =3D limit; break; } - limit =3D (ts->heap_base >> 1) + (limit >> 1); } - ts->heap_limit =3D limit; } - - retvals[0] =3D ts->heap_base; - retvals[1] =3D ts->heap_limit; + retvals[0] =3D heapbase; + retvals[1] =3D heaplimit; /* * Note that semihosting is *not* thread aware. * Always return the stack base of the main thread. --=20 2.47.2