From nobody Tue Feb 10 10:19:09 2026 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1656400780220519.5830941242448; Tue, 28 Jun 2022 00:19:40 -0700 (PDT) Received: from localhost ([::1]:59002 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o65VZ-00087k-WE for importer@patchew.org; Tue, 28 Jun 2022 03:19:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43150) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o63F9-0002Ye-Ul for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:54:31 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]:37550) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o63F7-0008JM-29 for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:54:31 -0400 Received: by mail-pl1-x636.google.com with SMTP id k14so10038759plh.4 for ; Mon, 27 Jun 2022 21:54:28 -0700 (PDT) Received: from stoup.. ([122.255.60.245]) by smtp.gmail.com with ESMTPSA id w8-20020a1709026f0800b00168c523032fsm8073735plk.269.2022.06.27.21.54.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 21:54:27 -0700 (PDT) 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=ZpNeXR+riLFDBFgPlcWD2eatfpq6x/LtbRlobcViT8U=; b=eIyynwEYf+zAYlBqq3YD3ntHYjLFB3MwZtlSnuIXz0oKUT+/Qn+27dpEoQ28yiQGc8 FXh5a4zDXfLw/tsBeBDDfKEdU5Kmx2IRYjzQ9WuZewMB9P7OpKU4Okxkr4Xkxccq8Qy8 +F/tiWdTee5a2bZnKIFE182nd1IRIqRKhdrfmRT3kQ/AIzsE+wbgLn1D1+dpPeSLi0pj K4c83hMaw2z/E7ZoD+jyyUIE1zJ1QYxUtNNFSFcxNjBgOJeUlnABgRqRh1ntagSoO/t2 s2wE6xhBCSv/bbORUOBFfPWQoecsxPphrb/ZXfdL6njNY01nhmTtIhfiRq22F9X1MrwD hKaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZpNeXR+riLFDBFgPlcWD2eatfpq6x/LtbRlobcViT8U=; b=34XLkyhS78Gw/A6Sk/m4purWRBCMJQZrURKH5cHR3lbLTm7HZzSZRd42BwUQ1ZJpBL SkHkSqS7K75k8o90o2btCSqbSRzeuGqBY8jT53a9SPWHOhul/kYmFmxONqbdJOTija1u O9L9r1vjWHrGwjfvQAkyB07f4rU82UACwPttAsYdEhc9Kz0YVk1jKzhtJHJNjj4vW+pd wJEwbBq3/heVSNy+plcfw9oRoO934wX5Hyv5ljGenvicZXS+mjbVHop0yIWaHdttQosY +FDXemDCxTY9EFPxHW0o7FbD7lwgEgiIbw/a6MhB/WGMSOZrqxlc3oGvEa4o+U1VqVtx HdGw== X-Gm-Message-State: AJIora8AOK8CDv1RG8OLmuJF34aGxx9h64Lz15W5pkD2pYA483VuDm+K 9BMzt9OGAFkODk0xW4s9+czucyz5JooUUA== X-Google-Smtp-Source: AGRyM1v1EAOZRXHjDjrBkPdFwivFl8DJs7HIR9MthNZc7GldTpc28I6nudLOus6043ad9MBcZSD36Q== X-Received: by 2002:a17:903:249:b0:168:ecca:43e with SMTP id j9-20020a170903024900b00168ecca043emr1783181plh.14.1656392067554; Mon, 27 Jun 2022 21:54:27 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Peter Maydell Subject: [PULL 08/60] semihosting: Split out guestfd.c Date: Tue, 28 Jun 2022 10:23:11 +0530 Message-Id: <20220628045403.508716-9-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220628045403.508716-1-richard.henderson@linaro.org> References: <20220628045403.508716-1-richard.henderson@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=2607:f8b0:4864:20::636; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x636.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, T_SCC_BODY_TEXT_LINE=-0.01 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" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1656400781281100001 In arm-compat-semi.c, we have more advanced treatment of guest file descriptors than we do in other implementations. Split out GuestFD and related functions to a new file so that they can be shared. Reviewed-by: Alex Benn=C3=A9e Reviewed-by: Peter Maydell Signed-off-by: Richard Henderson --- configs/targets/aarch64-linux-user.mak | 1 + configs/targets/aarch64_be-linux-user.mak | 1 + configs/targets/arm-linux-user.mak | 1 + configs/targets/armeb-linux-user.mak | 1 + configs/targets/riscv32-linux-user.mak | 1 + configs/targets/riscv64-linux-user.mak | 1 + include/semihosting/guestfd.h | 83 +++++++++++ semihosting/arm-compat-semi.c | 164 +++------------------- semihosting/guestfd.c | 118 ++++++++++++++++ semihosting/meson.build | 4 + 10 files changed, 233 insertions(+), 142 deletions(-) create mode 100644 include/semihosting/guestfd.h create mode 100644 semihosting/guestfd.c diff --git a/configs/targets/aarch64-linux-user.mak b/configs/targets/aarch= 64-linux-user.mak index d0c603c54e..db552f1839 100644 --- a/configs/targets/aarch64-linux-user.mak +++ b/configs/targets/aarch64-linux-user.mak @@ -2,4 +2,5 @@ TARGET_ARCH=3Daarch64 TARGET_BASE_ARCH=3Darm TARGET_XML_FILES=3D gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml TARGET_HAS_BFLT=3Dy +CONFIG_SEMIHOSTING=3Dy CONFIG_ARM_COMPATIBLE_SEMIHOSTING=3Dy diff --git a/configs/targets/aarch64_be-linux-user.mak b/configs/targets/aa= rch64_be-linux-user.mak index 7794424745..dc78044fb1 100644 --- a/configs/targets/aarch64_be-linux-user.mak +++ b/configs/targets/aarch64_be-linux-user.mak @@ -3,4 +3,5 @@ TARGET_BASE_ARCH=3Darm TARGET_BIG_ENDIAN=3Dy TARGET_XML_FILES=3D gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml TARGET_HAS_BFLT=3Dy +CONFIG_SEMIHOSTING=3Dy CONFIG_ARM_COMPATIBLE_SEMIHOSTING=3Dy diff --git a/configs/targets/arm-linux-user.mak b/configs/targets/arm-linux= -user.mak index 3e10d6b15d..7f5d65794c 100644 --- a/configs/targets/arm-linux-user.mak +++ b/configs/targets/arm-linux-user.mak @@ -3,4 +3,5 @@ TARGET_SYSTBL_ABI=3Dcommon,oabi TARGET_SYSTBL=3Dsyscall.tbl TARGET_XML_FILES=3D gdb-xml/arm-core.xml gdb-xml/arm-vfp.xml gdb-xml/arm-v= fp3.xml gdb-xml/arm-vfp-sysregs.xml gdb-xml/arm-neon.xml gdb-xml/arm-m-prof= ile.xml gdb-xml/arm-m-profile-mve.xml TARGET_HAS_BFLT=3Dy +CONFIG_SEMIHOSTING=3Dy CONFIG_ARM_COMPATIBLE_SEMIHOSTING=3Dy diff --git a/configs/targets/armeb-linux-user.mak b/configs/targets/armeb-l= inux-user.mak index a249cc2e29..943d0d87bf 100644 --- a/configs/targets/armeb-linux-user.mak +++ b/configs/targets/armeb-linux-user.mak @@ -4,4 +4,5 @@ TARGET_SYSTBL=3Dsyscall.tbl TARGET_BIG_ENDIAN=3Dy TARGET_XML_FILES=3D gdb-xml/arm-core.xml gdb-xml/arm-vfp.xml gdb-xml/arm-v= fp3.xml gdb-xml/arm-vfp-sysregs.xml gdb-xml/arm-neon.xml gdb-xml/arm-m-prof= ile.xml gdb-xml/arm-m-profile-mve.xml TARGET_HAS_BFLT=3Dy +CONFIG_SEMIHOSTING=3Dy CONFIG_ARM_COMPATIBLE_SEMIHOSTING=3Dy diff --git a/configs/targets/riscv32-linux-user.mak b/configs/targets/riscv= 32-linux-user.mak index bd2f1fd497..9761618e67 100644 --- a/configs/targets/riscv32-linux-user.mak +++ b/configs/targets/riscv32-linux-user.mak @@ -2,4 +2,5 @@ TARGET_ARCH=3Driscv32 TARGET_BASE_ARCH=3Driscv TARGET_ABI_DIR=3Driscv TARGET_XML_FILES=3D gdb-xml/riscv-32bit-cpu.xml gdb-xml/riscv-32bit-fpu.xm= l gdb-xml/riscv-64bit-fpu.xml gdb-xml/riscv-32bit-virtual.xml +CONFIG_SEMIHOSTING=3Dy CONFIG_ARM_COMPATIBLE_SEMIHOSTING=3Dy diff --git a/configs/targets/riscv64-linux-user.mak b/configs/targets/riscv= 64-linux-user.mak index 4aca7662ce..cfd1fd382f 100644 --- a/configs/targets/riscv64-linux-user.mak +++ b/configs/targets/riscv64-linux-user.mak @@ -2,4 +2,5 @@ TARGET_ARCH=3Driscv64 TARGET_BASE_ARCH=3Driscv TARGET_ABI_DIR=3Driscv TARGET_XML_FILES=3D gdb-xml/riscv-64bit-cpu.xml gdb-xml/riscv-32bit-fpu.xm= l gdb-xml/riscv-64bit-fpu.xml gdb-xml/riscv-64bit-virtual.xml +CONFIG_SEMIHOSTING=3Dy CONFIG_ARM_COMPATIBLE_SEMIHOSTING=3Dy diff --git a/include/semihosting/guestfd.h b/include/semihosting/guestfd.h new file mode 100644 index 0000000000..ef268abe85 --- /dev/null +++ b/include/semihosting/guestfd.h @@ -0,0 +1,83 @@ +/* + * Hosted file support for semihosting syscalls. + * + * Copyright (c) 2005, 2007 CodeSourcery. + * Copyright (c) 2019 Linaro + * Copyright =C2=A9 2020 by Keith Packard + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef SEMIHOSTING_GUESTFD_H +#define SEMIHOSTING_GUESTFD_H + +typedef enum GuestFDType { + GuestFDUnused =3D 0, + GuestFDHost =3D 1, + GuestFDGDB =3D 2, + GuestFDStatic =3D 3, +} GuestFDType; + +/* + * Guest file descriptors are integer indexes into an array of + * these structures (we will dynamically resize as necessary). + */ +typedef struct GuestFD { + GuestFDType type; + union { + int hostfd; + struct { + const uint8_t *data; + size_t len; + size_t off; + } staticfile; + }; +} GuestFD; + +/** + * alloc_guestfd: + * + * Allocate an unused GuestFD index. The associated guestfd index + * will still be GuestFDUnused until it is initialized. + */ +int alloc_guestfd(void); + +/** + * dealloc_guestfd: + * @guestfd: GuestFD index + * + * Deallocate a GuestFD index. The associated GuestFD structure + * will be recycled for a subsequent allocation. + */ +void dealloc_guestfd(int guestfd); + +/** + * get_guestfd: + * @guestfd: GuestFD index + * + * Return the GuestFD structure associated with an initialized @guestfd, + * or NULL if it has not been allocated, or hasn't been initialized. + */ +GuestFD *get_guestfd(int guestfd); + +/** + * associate_guestfd: + * @guestfd: GuestFD index + * @hostfd: host file descriptor + * + * Initialize the GuestFD for @guestfd to GuestFDHost using @hostfd. + */ +void associate_guestfd(int guestfd, int hostfd); + +/** + * staticfile_guestfd: + * @guestfd: GuestFD index + * @data: data to be read + * @len: length of @data + * + * Initialize the GuestFD for @guestfd to GuestFDStatic. + * The @len bytes at @data will be returned to the guest on reads. + */ +void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len); + +#endif /* SEMIHOSTING_GUESTFD_H */ diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index 1033e751ef..2fa7f23d8b 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -32,12 +32,13 @@ */ =20 #include "qemu/osdep.h" - #include "semihosting/semihost.h" #include "semihosting/console.h" #include "semihosting/common-semi.h" +#include "semihosting/guestfd.h" #include "qemu/timer.h" #include "exec/gdbstub.h" + #ifdef CONFIG_USER_ONLY #include "qemu.h" =20 @@ -123,27 +124,6 @@ static int open_modeflags[12] =3D { O_RDWR | O_CREAT | O_APPEND | O_BINARY }; =20 -typedef enum GuestFDType { - GuestFDUnused =3D 0, - GuestFDHost =3D 1, - GuestFDGDB =3D 2, - GuestFDFeatureFile =3D 3, -} GuestFDType; - -/* - * Guest file descriptors are integer indexes into an array of - * these structures (we will dynamically resize as necessary). - */ -typedef struct GuestFD { - GuestFDType type; - union { - int hostfd; - target_ulong featurefile_offset; - }; -} GuestFD; - -static GArray *guestfd_array; - #ifndef CONFIG_USER_ONLY =20 /** @@ -268,98 +248,6 @@ common_semi_sys_exit_extended(CPUState *cs, int nr) =20 #endif =20 -/* - * Allocate a new guest file descriptor and return it; if we - * couldn't allocate a new fd then return -1. - * This is a fairly simplistic implementation because we don't - * expect that most semihosting guest programs will make very - * heavy use of opening and closing fds. - */ -static int alloc_guestfd(void) -{ - guint i; - - if (!guestfd_array) { - /* New entries zero-initialized, i.e. type GuestFDUnused */ - guestfd_array =3D g_array_new(FALSE, TRUE, sizeof(GuestFD)); - } - - /* SYS_OPEN should return nonzero handle on success. Start guestfd fro= m 1 */ - for (i =3D 1; i < guestfd_array->len; i++) { - GuestFD *gf =3D &g_array_index(guestfd_array, GuestFD, i); - - if (gf->type =3D=3D GuestFDUnused) { - return i; - } - } - - /* All elements already in use: expand the array */ - g_array_set_size(guestfd_array, i + 1); - return i; -} - -/* - * Look up the guestfd in the data structure; return NULL - * for out of bounds, but don't check whether the slot is unused. - * This is used internally by the other guestfd functions. - */ -static GuestFD *do_get_guestfd(int guestfd) -{ - if (!guestfd_array) { - return NULL; - } - - if (guestfd <=3D 0 || guestfd >=3D guestfd_array->len) { - return NULL; - } - - return &g_array_index(guestfd_array, GuestFD, guestfd); -} - -/* - * Associate the specified guest fd (which must have been - * allocated via alloc_fd() and not previously used) with - * the specified host/gdb fd. - */ -static void associate_guestfd(int guestfd, int hostfd) -{ - GuestFD *gf =3D do_get_guestfd(guestfd); - - assert(gf); - gf->type =3D use_gdb_syscalls() ? GuestFDGDB : GuestFDHost; - gf->hostfd =3D hostfd; -} - -/* - * Deallocate the specified guest file descriptor. This doesn't - * close the host fd, it merely undoes the work of alloc_fd(). - */ -static void dealloc_guestfd(int guestfd) -{ - GuestFD *gf =3D do_get_guestfd(guestfd); - - assert(gf); - gf->type =3D GuestFDUnused; -} - -/* - * Given a guest file descriptor, get the associated struct. - * If the fd is not valid, return NULL. This is the function - * used by the various semihosting calls to validate a handle - * from the guest. - * Note: calling alloc_guestfd() or dealloc_guestfd() will - * invalidate any GuestFD* obtained by calling this function. - */ -static GuestFD *get_guestfd(int guestfd) -{ - GuestFD *gf =3D do_get_guestfd(guestfd); - - if (!gf || gf->type =3D=3D GuestFDUnused) { - return NULL; - } - return gf; -} - /* * The semihosting API has no concept of its errno being thread-safe, * as the API design predates SMP CPUs and was intended as a simple @@ -665,22 +553,13 @@ static const uint8_t featurefile_data[] =3D { SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */ }; =20 -static void init_featurefile_guestfd(int guestfd) -{ - GuestFD *gf =3D do_get_guestfd(guestfd); - - assert(gf); - gf->type =3D GuestFDFeatureFile; - gf->featurefile_offset =3D 0; -} - -static uint32_t featurefile_closefn(CPUState *cs, GuestFD *gf) +static uint32_t staticfile_closefn(CPUState *cs, GuestFD *gf) { /* Nothing to do */ return 0; } =20 -static uint32_t featurefile_writefn(CPUState *cs, GuestFD *gf, +static uint32_t staticfile_writefn(CPUState *cs, GuestFD *gf, target_ulong buf, uint32_t len) { /* This fd can never be open for writing */ @@ -689,7 +568,7 @@ static uint32_t featurefile_writefn(CPUState *cs, Guest= FD *gf, return set_swi_errno(cs, -1); } =20 -static uint32_t featurefile_readfn(CPUState *cs, GuestFD *gf, +static uint32_t staticfile_readfn(CPUState *cs, GuestFD *gf, target_ulong buf, uint32_t len) { CPUArchState *env =3D cs->env_ptr; @@ -703,11 +582,11 @@ static uint32_t featurefile_readfn(CPUState *cs, Gues= tFD *gf, } =20 for (i =3D 0; i < len; i++) { - if (gf->featurefile_offset >=3D sizeof(featurefile_data)) { + if (gf->staticfile.off >=3D gf->staticfile.len) { break; } - s[i] =3D featurefile_data[gf->featurefile_offset]; - gf->featurefile_offset++; + s[i] =3D gf->staticfile.data[gf->staticfile.off]; + gf->staticfile.off++; } =20 unlock_user(s, buf, len); @@ -716,21 +595,21 @@ static uint32_t featurefile_readfn(CPUState *cs, Gues= tFD *gf, return len - i; } =20 -static uint32_t featurefile_isattyfn(CPUState *cs, GuestFD *gf) +static uint32_t staticfile_isattyfn(CPUState *cs, GuestFD *gf) { return 0; } =20 -static uint32_t featurefile_seekfn(CPUState *cs, GuestFD *gf, +static uint32_t staticfile_seekfn(CPUState *cs, GuestFD *gf, target_ulong offset) { - gf->featurefile_offset =3D offset; + gf->staticfile.off =3D offset; return 0; } =20 -static uint32_t featurefile_flenfn(CPUState *cs, GuestFD *gf) +static uint32_t staticfile_flenfn(CPUState *cs, GuestFD *gf) { - return sizeof(featurefile_data); + return gf->staticfile.len; } =20 typedef struct GuestFDFunctions { @@ -759,13 +638,13 @@ static const GuestFDFunctions guestfd_fns[] =3D { .seekfn =3D gdb_seekfn, .flenfn =3D gdb_flenfn, }, - [GuestFDFeatureFile] =3D { - .closefn =3D featurefile_closefn, - .writefn =3D featurefile_writefn, - .readfn =3D featurefile_readfn, - .isattyfn =3D featurefile_isattyfn, - .seekfn =3D featurefile_seekfn, - .flenfn =3D featurefile_flenfn, + [GuestFDStatic] =3D { + .closefn =3D staticfile_closefn, + .writefn =3D staticfile_writefn, + .readfn =3D staticfile_readfn, + .isattyfn =3D staticfile_isattyfn, + .seekfn =3D staticfile_seekfn, + .flenfn =3D staticfile_flenfn, }, }; =20 @@ -886,7 +765,8 @@ target_ulong do_common_semihosting(CPUState *cs) errno =3D EACCES; return set_swi_errno(cs, -1); } - init_featurefile_guestfd(guestfd); + staticfile_guestfd(guestfd, featurefile_data, + sizeof(featurefile_data)); return guestfd; } =20 diff --git a/semihosting/guestfd.c b/semihosting/guestfd.c new file mode 100644 index 0000000000..b6405f5663 --- /dev/null +++ b/semihosting/guestfd.c @@ -0,0 +1,118 @@ +/* + * Hosted file support for semihosting syscalls. + * + * Copyright (c) 2005, 2007 CodeSourcery. + * Copyright (c) 2019 Linaro + * Copyright =C2=A9 2020 by Keith Packard + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "exec/gdbstub.h" +#include "semihosting/guestfd.h" + +static GArray *guestfd_array; + +/* + * Allocate a new guest file descriptor and return it; if we + * couldn't allocate a new fd then return -1. + * This is a fairly simplistic implementation because we don't + * expect that most semihosting guest programs will make very + * heavy use of opening and closing fds. + */ +int alloc_guestfd(void) +{ + guint i; + + if (!guestfd_array) { + /* New entries zero-initialized, i.e. type GuestFDUnused */ + guestfd_array =3D g_array_new(FALSE, TRUE, sizeof(GuestFD)); + } + + /* SYS_OPEN should return nonzero handle on success. Start guestfd fro= m 1 */ + for (i =3D 1; i < guestfd_array->len; i++) { + GuestFD *gf =3D &g_array_index(guestfd_array, GuestFD, i); + + if (gf->type =3D=3D GuestFDUnused) { + return i; + } + } + + /* All elements already in use: expand the array */ + g_array_set_size(guestfd_array, i + 1); + return i; +} + +/* + * Look up the guestfd in the data structure; return NULL + * for out of bounds, but don't check whether the slot is unused. + * This is used internally by the other guestfd functions. + */ +static GuestFD *do_get_guestfd(int guestfd) +{ + if (!guestfd_array) { + return NULL; + } + + if (guestfd <=3D 0 || guestfd >=3D guestfd_array->len) { + return NULL; + } + + return &g_array_index(guestfd_array, GuestFD, guestfd); +} + +/* + * Given a guest file descriptor, get the associated struct. + * If the fd is not valid, return NULL. This is the function + * used by the various semihosting calls to validate a handle + * from the guest. + * Note: calling alloc_guestfd() or dealloc_guestfd() will + * invalidate any GuestFD* obtained by calling this function. + */ +GuestFD *get_guestfd(int guestfd) +{ + GuestFD *gf =3D do_get_guestfd(guestfd); + + if (!gf || gf->type =3D=3D GuestFDUnused) { + return NULL; + } + return gf; +} + +/* + * Associate the specified guest fd (which must have been + * allocated via alloc_fd() and not previously used) with + * the specified host/gdb fd. + */ +void associate_guestfd(int guestfd, int hostfd) +{ + GuestFD *gf =3D do_get_guestfd(guestfd); + + assert(gf); + gf->type =3D use_gdb_syscalls() ? GuestFDGDB : GuestFDHost; + gf->hostfd =3D hostfd; +} + +void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len) +{ + GuestFD *gf =3D do_get_guestfd(guestfd); + + assert(gf); + gf->type =3D GuestFDStatic; + gf->staticfile.data =3D data; + gf->staticfile.len =3D len; + gf->staticfile.off =3D 0; +} + +/* + * Deallocate the specified guest file descriptor. This doesn't + * close the host fd, it merely undoes the work of alloc_fd(). + */ +void dealloc_guestfd(int guestfd) +{ + GuestFD *gf =3D do_get_guestfd(guestfd); + + assert(gf); + gf->type =3D GuestFDUnused; +} diff --git a/semihosting/meson.build b/semihosting/meson.build index 10b3b99921..d2c1c37bfd 100644 --- a/semihosting/meson.build +++ b/semihosting/meson.build @@ -1,3 +1,7 @@ +specific_ss.add(when: 'CONFIG_SEMIHOSTING', if_true: files( + 'guestfd.c', +)) + specific_ss.add(when: ['CONFIG_SEMIHOSTING', 'CONFIG_SOFTMMU'], if_true: f= iles( 'config.c', 'console.c', --=20 2.34.1