From patchwork Tue Jun 28 04:53:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 585553 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5036:0:0:0:0 with SMTP id e22csp527050mab; Tue, 28 Jun 2022 00:56:52 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vtq9k4t8XW4TFl82GS6oKUK9YPrn4So+bgTm1gLjRQAdbSna28+KrX2FMSutPTGXXmp5Y1 X-Received: by 2002:a0c:e888:0:b0:470:4b73:fbb with SMTP id b8-20020a0ce888000000b004704b730fbbmr1244680qvo.128.1656403012367; Tue, 28 Jun 2022 00:56:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656403012; cv=none; d=google.com; s=arc-20160816; b=e9pU5FY/DwG+h+XcuzlbhP+glyQtYiIbYVKUbO3MpVZoFz8Po57VmNHd0YxiWelATF Yq0M9PcxbRvaeulRH20tcHEyO5/RRJ3MmUQxwh47ZD9PEKwc9J+RO1oGvAAxP84CupU6 nG/9sGqyfSQnZI7K2+ALGzglnohxdb3JXu1vRoUxpEwUd7CxlRD1kOhxknjznH/SvcBD TtXPeXdgRueq4D6oVtEkEa0p87D5SVzIvjZQQ5RL7G5SsQmoIPaejrqj4CDt0EXHXw7W dB7vVywdNG2Ou9RAHfFVibBlHLodomWrfxdQZCrQtASIuxFDfyWrN5PTOwaWs8+6Ok1+ GXUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=N4HA6P3LhSfjOwoWfaJXDw0oNNYju2wXfDZ+QMa3+ug=; b=rfkYX3ns20wgDkklpVCwzciUgE+7iskd18qX5za26T6aG+QL/M1ORzGFXy27fMG8Tp R6cGkiUdQceLWrrtlhjg60EOQ3WkuDGvrUPfVsUYcgCpUD2iCezX7IgR+lI+X6ABWvt3 At9pedkEd89EOHyUnKcTCCY9jcFE8pdqEbFH88cCwbYr1AHTXTBDc54ZhcxYEbotobkx vgIHE96uS33GndUrvqG6IFRN4Ff2DQ13Oz0U+t7ctKfn6mH6SQl52OfxRosNxujK/tA2 MszSPCtYiiX+M4SAQvQIEMpx5uGEIh+FfLqBxvKnKVxBGkGUVNLP1TN5DNWgY1Y253mA fA3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cRuIinTN; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id a17-20020a05620a439100b006a7794a2fe8si7654081qkp.499.2022.06.28.00.56.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 28 Jun 2022 00:56:52 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cRuIinTN; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:48528 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o665Y-0002B3-Pk for patch@linaro.org; Tue, 28 Jun 2022 03:56:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43694) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o63G9-0003bi-Jk for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:55:33 -0400 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]:45990) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o63G7-0000FM-Ct for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:55:32 -0400 Received: by mail-pf1-x42a.google.com with SMTP id 128so10916483pfv.12 for ; Mon, 27 Jun 2022 21:55:30 -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=N4HA6P3LhSfjOwoWfaJXDw0oNNYju2wXfDZ+QMa3+ug=; b=cRuIinTNBgorQM6jGfSD1MdnylEjYdekm+RlbjoCDJWnLRCadZmrPC8xSOiVgS8g5Z ysnFeHLDxhsxqTNVVShvRvfVNb2nTIUTUikN03d07CsJ4W3BMXHSThNiDjzfqd4bBRuB mh+ZAAynGORRIHyF8lTcDtBIsrv9XLA+yChlMWlZlwpb673j+mIttMH1lA/Dl0I0Cmpz tLes+ExYT/Y7r875aKUoAFSx6XFSwwXxBAUVvwR5eB5UAzj81WVM2S9UrPaqtIb9T5kt pq4f8UVvzhB3TKuuV+VbP/Hl7o+GJMHvCxFwODi1u2TBJOPkzJqW9T8JEhb1twB5+qHD 5uwg== 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=N4HA6P3LhSfjOwoWfaJXDw0oNNYju2wXfDZ+QMa3+ug=; b=Z5a40b+hIzzbJVwtR7K9w9xEIFvCZqBRhTdX9O7CN2BlJAqj/P6D3c78rWjCDALbVI ixYxdAApVPCliXt6evnuvOJ2YTUTAVBUIQvE80I9sbDYJbAqEtxeYOUn0Gid71wRAkx6 jm8MQAwxswGxzLpUvWyLaySQwfyXpNq234qSlRlwtinBjpqts9QzTfSswvhursfufJBQ lRB9zv9DQPrXsfs5DqX6ZfChNVaCWrVxAb93xZNZCk2mhgqHb5hpVIKF1AnQOD5gFzpl 9AmNBFmvxABx4pOD9BPcXM79zsgDoR/QboqcuNuABBV0+p82Bop2yopYsFXZbLp9fBYr IHtA== X-Gm-Message-State: AJIora9HqSkjrQSh/mtdn37stMGjz5S935U6qBh4lM30T5dd+ae9vqxH qnd4bRwkvhsX/2DqsNQISATvOd/wOmAbmQ== X-Received: by 2002:a63:7c3:0:b0:40d:5dc5:f9f0 with SMTP id 186-20020a6307c3000000b0040d5dc5f9f0mr15529137pgh.524.1656392129985; Mon, 27 Jun 2022 21:55:29 -0700 (PDT) Received: from stoup.. ([122.255.60.245]) by smtp.gmail.com with ESMTPSA id w8-20020a1709026f0800b00168c523032fsm8073735plk.269.2022.06.27.21.55.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 21:55:29 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Luc Michel Subject: [PULL 33/60] semihosting: Split out semihost_sys_isatty Date: Tue, 28 Jun 2022 10:23:36 +0530 Message-Id: <20220628045403.508716-34-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 Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42a.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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Split out the non-ARM specific portions of SYS_ISTTY to a reusable function. This handles all GuestFD. Add a common_semi_istty_cb helper to translate the Posix error return, 0+ENOTTY, to the Arm semihosting not-a-file success result. Reviewed-by: Luc Michel Signed-off-by: Richard Henderson --- include/semihosting/syscalls.h | 3 +++ semihosting/arm-compat-semi.c | 40 ++++++++++++---------------------- semihosting/syscalls.c | 36 ++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/include/semihosting/syscalls.h b/include/semihosting/syscalls.h index 841a93d25b..c60ebafb85 100644 --- a/include/semihosting/syscalls.h +++ b/include/semihosting/syscalls.h @@ -42,4 +42,7 @@ void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete, void semihost_sys_lseek(CPUState *cs, gdb_syscall_complete_cb complete, int fd, int64_t off, int gdb_whence); +void semihost_sys_isatty(CPUState *cs, gdb_syscall_complete_cb complete, + int fd); + #endif /* SEMIHOSTING_SYSCALLS_H */ diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index a117d180bc..3cdc2b6efc 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -247,6 +247,19 @@ static void common_semi_rw_cb(CPUState *cs, target_ulong ret, target_ulong err) common_semi_set_ret(cs, arg2 - ret); } +/* + * Convert from Posix ret+errno to Arm SYS_ISTTY return values. + * With gdbstub, err is only ever set for protocol errors to EIO. + */ +static void common_semi_istty_cb(CPUState *cs, target_ulong ret, + target_ulong err) +{ + if (err) { + ret = (err == ENOTTY ? 0 : -1); + } + common_semi_cb(cs, ret, err); +} + /* * SYS_SEEK returns 0 on success, not the resulting offset. */ @@ -291,14 +304,8 @@ common_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err) * do the work and return the required return value to the guest * via common_semi_cb. */ -typedef void sys_isattyfn(CPUState *cs, GuestFD *gf); typedef void sys_flenfn(CPUState *cs, GuestFD *gf); -static void host_isattyfn(CPUState *cs, GuestFD *gf) -{ - common_semi_cb(cs, isatty(gf->hostfd), 0); -} - static void host_flenfn(CPUState *cs, GuestFD *gf) { struct stat buf; @@ -310,11 +317,6 @@ static void host_flenfn(CPUState *cs, GuestFD *gf) } } -static void gdb_isattyfn(CPUState *cs, GuestFD *gf) -{ - gdb_do_syscall(common_semi_cb, "isatty,%x", gf->hostfd); -} - static void gdb_flenfn(CPUState *cs, GuestFD *gf) { gdb_do_syscall(common_semi_flen_cb, "fstat,%x,%x", @@ -338,32 +340,23 @@ static const uint8_t featurefile_data[] = { SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */ }; -static void staticfile_isattyfn(CPUState *cs, GuestFD *gf) -{ - common_semi_cb(cs, 0, 0); -} - static void staticfile_flenfn(CPUState *cs, GuestFD *gf) { common_semi_cb(cs, gf->staticfile.len, 0); } typedef struct GuestFDFunctions { - sys_isattyfn *isattyfn; sys_flenfn *flenfn; } GuestFDFunctions; static const GuestFDFunctions guestfd_fns[] = { [GuestFDHost] = { - .isattyfn = host_isattyfn, .flenfn = host_flenfn, }, [GuestFDGDB] = { - .isattyfn = gdb_isattyfn, .flenfn = gdb_flenfn, }, [GuestFDStatic] = { - .isattyfn = staticfile_isattyfn, .flenfn = staticfile_flenfn, }, }; @@ -489,12 +482,7 @@ void do_common_semihosting(CPUState *cs) case TARGET_SYS_ISTTY: GET_ARG(0); - - gf = get_guestfd(arg0); - if (!gf) { - goto do_badf; - } - guestfd_fns[gf->type].isattyfn(cs, gf); + semihost_sys_isatty(cs, common_semi_istty_cb, arg0); break; case TARGET_SYS_SEEK: diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c index 9e3eb464b5..1f1baf7e2d 100644 --- a/semihosting/syscalls.c +++ b/semihosting/syscalls.c @@ -121,6 +121,12 @@ static void gdb_lseek(CPUState *cs, gdb_syscall_complete_cb complete, (target_ulong)gf->hostfd, off, (target_ulong)gdb_whence); } +static void gdb_isatty(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf) +{ + gdb_do_syscall(complete, "isatty,%x", (target_ulong)gf->hostfd); +} + /* * Host semihosting syscall implementations. */ @@ -246,6 +252,13 @@ static void host_lseek(CPUState *cs, gdb_syscall_complete_cb complete, complete(cs, ret, err); } +static void host_isatty(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf) +{ + int ret = isatty(gf->hostfd); + complete(cs, ret, ret ? 0 : errno); +} + /* * Static file semihosting syscall implementations. */ @@ -437,3 +450,26 @@ void semihost_sys_lseek(CPUState *cs, gdb_syscall_complete_cb complete, g_assert_not_reached(); } } + +void semihost_sys_isatty(CPUState *cs, gdb_syscall_complete_cb complete, int fd) +{ + GuestFD *gf = get_guestfd(fd); + + if (!gf) { + complete(cs, 0, EBADF); + return; + } + switch (gf->type) { + case GuestFDGDB: + gdb_isatty(cs, complete, gf); + break; + case GuestFDHost: + host_isatty(cs, complete, gf); + break; + case GuestFDStatic: + complete(cs, 0, ENOTTY); + break; + default: + g_assert_not_reached(); + } +}