From nobody Sat May 18 09:22:57 2024 Delivered-To: importer@patchew.org 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; 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=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1578543251; cv=none; d=zohomail.com; s=zohoarc; b=ieef76FTFZrVsEEo2gQXk+2E+kPSdywcdYx+xKFQfRqq1PU8WwqUGpn66661fGn0oUBqLJDVahwpjqhfAL+yeQOxJbcEz3s4wyIamiRLoHnjqZm4DgGpUTHMXJRoZKvFoc/BDohzH1CFSc4nzwVJLb+XIpl4jPiAnNThdcR8BlY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1578543251; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=BGrV9MYFYy/4lN3+lwdtOSdl3h0m0Dt37S+FF37WHQc=; b=HZPuB916PjlSXem/9lhsGsv5cTLh6/00aivURdWhWPVtTBNtvz8AWOA+qyemBuskzD8M+UGV6OYkgFDmne17BNSyHHC8Rp/Na84yiRRDy0rPwaenHNbeXCNwHk7pLJXCJfXDshu0pHojRn1D232qpknmPqg/BgCYA7YbwPs7T+k= 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 1578543251089212.0288390394153; Wed, 8 Jan 2020 20:14:11 -0800 (PST) Received: from localhost ([::1]:55012 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipPCy-0006tY-PT for importer@patchew.org; Wed, 08 Jan 2020 23:14:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:33297) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ipPCF-0006Lf-8U for qemu-devel@nongnu.org; Wed, 08 Jan 2020 23:13:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ipPCD-0002Ze-TL for qemu-devel@nongnu.org; Wed, 08 Jan 2020 23:13:23 -0500 Received: from conuserg-08.nifty.com ([210.131.2.75]:62838) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ipPC8-0002Hs-Fh; Wed, 08 Jan 2020 23:13:17 -0500 Received: from localhost.localdomain (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-08.nifty.com with ESMTP id 0094CUwT026552; Thu, 9 Jan 2020 13:12:31 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-08.nifty.com 0094CUwT026552 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1578543151; bh=BGrV9MYFYy/4lN3+lwdtOSdl3h0m0Dt37S+FF37WHQc=; h=From:To:Cc:Subject:Date:From; b=d/T0X+ZAfspEWoIVTg6beDtFsXcpo+8TfB2VkYDfVX2fDsjd0JLe106hkZ5zWkD+w IbMy5mC2H5icC68IkVKkkOYtG5BHN0x4RDr04zeM4cRbM4x3exNGenvNHRp/0/Hedo OamRG1eZ5De043p7uTnTt3/Hau25+r4DykuhViQV1UCWDrX/WhV1oO0v+6Lwc8fa09 DaT+Weq8Rj5RIQU9LWuytgEkytPOorJlh38SaODizyH/IBRioIwa+Jd5Np1BGLLDIj rAgRCfNbx/Vlx6lZKsGw+sLx5zzyVel2vrIMHbdr8oU/DPKp61s5y2OZSflbl+EQE9 swYZzQpIuDNig== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: qemu-arm@nongnu.org, Peter Maydell Subject: [PATCH] target/arm/arm-semi: fix SYS_OPEN to return nonzero filehandle Date: Thu, 9 Jan 2020 13:12:28 +0900 Message-Id: <20200109041228.10131-1-masahiroy@kernel.org> X-Mailer: git-send-email 2.17.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 210.131.2.75 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: Masahiro Yamada , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" According to the specification "Semihosting for AArch32 and Aarch64", the SYS_OPEN operation should return: - A nonzero handle if the call is successful - -1 if the call is not successful So, it should never return 0. Prior to commit 35e9a0a8ce4b ("target/arm/arm-semi: Make semihosting code hand out its own file descriptors"), the guest fd matched to the host fd. It returned a nonzero handle on success since the fd 0 is already used for stdin. Now that the guest fd is the index of guestfd_array, it starts from 0. I noticed this issue particularly because Trusted Firmware-A built with PLAT=3Dqemu is no longer working. Its io_semihosting driver only handles a positive return value as a valid filehandle. Basically, there are two ways to fix this: - Use (guestfd - 1) as the index of guestfs_arrary. We need to insert increment/decrement to convert the guestfd and the array index back and forth. - Keep using guestfd as the index of guestfs_array. The first entry of guestfs_array is left unused. I thought the latter is simpler. We end up with wasting a small piece of memory for the unused first entry of guestfd_array, but this is probably not a big deal. Fixes: 35e9a0a8ce4b ("target/arm/arm-semi: Make semihosting code hand out i= ts own file descriptors") Signed-off-by: Masahiro Yamada Reviewed-by: Richard Henderson --- target/arm/arm-semi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/target/arm/arm-semi.c b/target/arm/arm-semi.c index 6f7b6d801bf9..4275dfc34591 100644 --- a/target/arm/arm-semi.c +++ b/target/arm/arm-semi.c @@ -144,7 +144,8 @@ static int alloc_guestfd(void) guestfd_array =3D g_array_new(FALSE, TRUE, sizeof(GuestFD)); } =20 - for (i =3D 0; i < guestfd_array->len; i++) { + /* 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); =20 if (gf->type =3D=3D GuestFDUnused) { @@ -168,7 +169,7 @@ static GuestFD *do_get_guestfd(int guestfd) return NULL; } =20 - if (guestfd < 0 || guestfd >=3D guestfd_array->len) { + if (guestfd <=3D 0 || guestfd >=3D guestfd_array->len) { return NULL; } =20 --=20 2.17.1