From nobody Tue Feb 10 12:43:20 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1614952589; cv=none; d=zohomail.com; s=zohoarc; b=TrIwKo6TobJi7+aBQ1QermY+VwQe4fpljPNfD5IYuHFHSkq1l4smFAbDRr0V6NNNBSHNAIHKDgMRDI6Lgq8mBwLguyom9f4MRZwe6gv/l84WyOQymFUYKqyGY81g8LFNPs/fVXrQKcKa1rWEqvVrmVZKF7BAB1RPi6sRLdRmWAk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614952589; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=qs3X4Vrdn0QMj8+l/h+TrHu30GN7onIaZQm+5Oo555k=; b=Ye5OmihnklfjZyrIB+mUx5X/gMAc4LhdZaBoK62QpsQugP9x2w6VJbPvOQDSstq7H30X4lPtyRv4GUIPSVmY+ltSKL4f3ZeWVQwsAQe34+NQze2w7uY03x6VCG5Z/OyZmE29+OdKCh5G1NEmPrWbFskdhsgyCyfd+rWSgMhcwQw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1614952589913188.03596076215058; Fri, 5 Mar 2021 05:56:29 -0800 (PST) Received: from localhost ([::1]:43436 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lIAwN-0000hK-Rs for importer@patchew.org; Fri, 05 Mar 2021 08:56:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:37764) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lIAuz-0007bs-1i for qemu-devel@nongnu.org; Fri, 05 Mar 2021 08:55:01 -0500 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]:55302) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lIAuw-00079I-Gh for qemu-devel@nongnu.org; Fri, 05 Mar 2021 08:55:00 -0500 Received: by mail-wm1-x32c.google.com with SMTP id w7so1515311wmb.5 for ; Fri, 05 Mar 2021 05:54:58 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id m2sm4446648wml.34.2021.03.05.05.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Mar 2021 05:54:54 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 496E51FF8F; Fri, 5 Mar 2021 13:54:52 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qs3X4Vrdn0QMj8+l/h+TrHu30GN7onIaZQm+5Oo555k=; b=jWWRCWeikH/1ic62WGfEJYHM+DuvzknFxwhVseBkLuusYU+eEMHt+kbDQ+vZoXG1hG E480h4jv12fQN59oCGAF73lhDlxTMIJaXhtbXn49+cfwzPYrmraAFWHyW9L5VoJTbnxU otz7In9ozoO59KoBLWuZt0b+CdjAup7gfQWOrZRgjYrfbna66gYjTyIC5JWi/cJR95wB 622C65aTMddClNfTku9igojnCks6U54F43TxxYs5IWKTfZyZWn9lFVYlFo+ZPqaYH3GK pF7W+Inv1nJtJdjI+9ABrsTP/8GVXfPLkt+8/nVsPQyVnK3sBBA4NY30kOfkB+XZ29bx lv4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qs3X4Vrdn0QMj8+l/h+TrHu30GN7onIaZQm+5Oo555k=; b=lvgvzhG1ouEafjA8sOar9qGPzHgVT0YRtIGTcAa7nMhqJ2sQQhJKV2D4+zf8SFljL/ evJWd6+BXbBLk6QK9NvOrDBSPPD8Ffw0IN+U78xnInBfjl1B+whJZPVal/bxAmgKN0o6 dv4/ZpC/Vs2jHqwNU0Dd1pnalVpP+H2Kbh50or6imgViT6jvl+xdoNzHkhglXYIoNeBj D2VeZfxE8BVmlJcNeiNzm0FUjfr7Nc1tqq9fcgypuulRa7SF4xII2vwVdrdyYR+hshjk EUOoGXwY7dvWYZNbexVApLzMbVpAsupekqcfkPDewbHAqnpfaU48q2oyIXN+m4KSVd3H Jzow== X-Gm-Message-State: AOAM533UMpTbCRRHWj5cZTN88rlQ5MwQSUpBPFomsIrqtUWbvLcHVxBD NNOHW+U105t9uEztl3j0N3ZO4A== X-Google-Smtp-Source: ABdhPJy8hC7aVLOobsPev3Mc+dPtTbxQE6TpV+hR2c9f8+9uALyAyR/Z3n2Lb9SWiRxXZQH70VJ6ug== X-Received: by 2002:a1c:730f:: with SMTP id d15mr8912110wmb.135.1614952496967; Fri, 05 Mar 2021 05:54:56 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v1 3/3] semihosting/arg-compat: fix up handling of SYS_HEAPINFO Date: Fri, 5 Mar 2021 13:54:51 +0000 Message-Id: <20210305135451.15427-4-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210305135451.15427-1-alex.bennee@linaro.org> References: <20210305135451.15427-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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::32c; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x32c.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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bug 1915925 <1915925@bugs.launchpad.net>, Keith Packard , "open list:ARM TCG CPUs" , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Peter Maydell Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) I'm not sure this every worked properly and it's certainly not exercised by check-tcg or Peter's semihosting tests. Hoist it into it's own helper function and attempt to validate the results in the linux-user semihosting test at the least. Bug: https://bugs.launchpad.net/bugs/1915925 Cc: Bug 1915925 <1915925@bugs.launchpad.net> Cc: Keith Packard Signed-off-by: Alex Benn=C3=A9e --- tests/tcg/arm/semicall.h | 1 + semihosting/arm-compat-semi.c | 129 +++++++++++++++++++--------------- tests/tcg/arm/semihosting.c | 34 ++++++++- 3 files changed, 107 insertions(+), 57 deletions(-) diff --git a/tests/tcg/arm/semicall.h b/tests/tcg/arm/semicall.h index d4f6818192..676a542be5 100644 --- a/tests/tcg/arm/semicall.h +++ b/tests/tcg/arm/semicall.h @@ -9,6 +9,7 @@ =20 #define SYS_WRITE0 0x04 #define SYS_READC 0x07 +#define SYS_HEAPINFO 0x16 #define SYS_REPORTEXC 0x18 =20 uintptr_t __semi_call(uintptr_t type, uintptr_t arg0) diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index 94950b6c56..a8fdbceb5f 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -822,6 +822,78 @@ static const GuestFDFunctions guestfd_fns[] =3D { put_user_utl(val, args + (n) * sizeof(target_ulong)) #endif =20 +/* + * SYS_HEAPINFO is a little weird: "On entry, the PARAMETER REGISTER + * contains the address of a pointer to a four-field data block" which + * we then fill in. The PARAMETER REGISTER is unchanged. + */ + +struct HeapInfo { + target_ulong heap_base; + target_ulong heap_limit; + target_ulong stack_base; + target_ulong stack_limit; +}; + +static bool do_heapinfo(CPUState *cs, target_long arg0) +{ + target_ulong limit; + struct HeapInfo info =3D {}; +#ifdef CONFIG_USER_ONLY + TaskState *ts =3D cs->opaque; +#else + target_ulong rambase =3D common_semi_rambase(cs); +#endif + +#ifdef CONFIG_USER_ONLY + /* + * 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; + /* Try a big heap, and reduce the size if that fails. */ + for (;;) { + ret =3D do_brk(limit); + if (ret >=3D limit) { + break; + } + limit =3D (ts->heap_base >> 1) + (limit >> 1); + } + ts->heap_limit =3D limit; + } + + info.heap_base =3D ts->heap_base; + info.heap_limit =3D ts->heap_limit; + info.stack_base =3D ts->stack_base; + info.stack_limit =3D 0; /* Stack limit. */ + + if (copy_to_user(arg0, &info, sizeof(info))) { + errno =3D EFAULT; + return set_swi_errno(cs, -1); + } +#else + limit =3D current_machine->ram_size; + /* TODO: Make this use the limit of the loaded application. */ + info.heap_base =3D rambase + limit / 2; + info.heap_limit =3D rambase + limit; + info.stack_base =3D rambase + limit; /* Stack base */ + info.stack_limit =3D rambase; /* Stack limit. */ + + if (cpu_memory_rw_debug(cs, arg0, &info, sizeof(info), true)) { + errno =3D EFAULT; + return set_swi_errno(cs, -1); + } + +#endif + + return 0; +} + + /* * Do a semihosting call. * @@ -1184,63 +1256,8 @@ target_ulong do_common_semihosting(CPUState *cs) } case TARGET_SYS_HEAPINFO: { - target_ulong retvals[4]; - target_ulong limit; - int i; -#ifdef CONFIG_USER_ONLY - TaskState *ts =3D cs->opaque; -#else - target_ulong rambase =3D common_semi_rambase(cs); -#endif - GET_ARG(0); - -#ifdef CONFIG_USER_ONLY - /* - * 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; - /* Try a big heap, and reduce the size if that fails. */ - for (;;) { - ret =3D do_brk(limit); - if (ret >=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[2] =3D ts->stack_base; - retvals[3] =3D 0; /* Stack limit. */ -#else - limit =3D current_machine->ram_size; - /* TODO: Make this use the limit of the loaded application. */ - retvals[0] =3D rambase + limit / 2; - retvals[1] =3D rambase + limit; - retvals[2] =3D rambase + limit; /* Stack base */ - retvals[3] =3D rambase; /* Stack limit. */ -#endif - - for (i =3D 0; i < ARRAY_SIZE(retvals); i++) { - bool fail; - - fail =3D SET_ARG(i, retvals[i]); - - if (fail) { - /* Couldn't write back to argument block */ - errno =3D EFAULT; - return set_swi_errno(cs, -1); - } - } - return 0; + return do_heapinfo(cs, arg0); } case TARGET_SYS_EXIT: case TARGET_SYS_EXIT_EXTENDED: diff --git a/tests/tcg/arm/semihosting.c b/tests/tcg/arm/semihosting.c index 33faac9916..fd5780ec3c 100644 --- a/tests/tcg/arm/semihosting.c +++ b/tests/tcg/arm/semihosting.c @@ -7,7 +7,13 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ =20 +#define _GNU_SOURCE /* asprintf is a GNU extension */ + #include +#include +#include +#include +#include #include "semicall.h" =20 int main(int argc, char *argv[argc]) @@ -18,8 +24,34 @@ int main(int argc, char *argv[argc]) uintptr_t exit_block[2] =3D {0x20026, 0}; uintptr_t exit_code =3D (uintptr_t) &exit_block; #endif + struct { + void *heap_base; + void *heap_limit; + void *stack_base; + void *stack_limit; + } info; + void *ptr_to_info =3D (void *) &info; + char *heap_info, *stack_info; + void *brk =3D sbrk(0); + + __semi_call(SYS_WRITE0, (uintptr_t) "Hello World\n"); + + memset(&info, 0, sizeof(info)); + __semi_call(SYS_HEAPINFO, (uintptr_t) &ptr_to_info); + + asprintf(&heap_info, "heap: %p -> %p\n", info.heap_base, info.heap_lim= it); + __semi_call(SYS_WRITE0, (uintptr_t) heap_info); + if (info.heap_base !=3D brk) { + sprintf(heap_info, "heap mismatch: %p\n", brk); + __semi_call(SYS_WRITE0, (uintptr_t) heap_info); + return -1; + } + + asprintf(&stack_info, "stack: %p -> %p\n", info.stack_base, info.stack= _limit); + __semi_call(SYS_WRITE0, (uintptr_t) stack_info); + free(heap_info); + free(stack_info); =20 - __semi_call(SYS_WRITE0, (uintptr_t) "Hello World"); __semi_call(SYS_REPORTEXC, exit_code); /* if we get here we failed */ return -1; --=20 2.20.1