From patchwork Tue Jun 28 04:53:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 585556 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5036:0:0:0:0 with SMTP id e22csp537873mab; Tue, 28 Jun 2022 01:12:43 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vSwW7e5aFp1IBvmnE5y8b1b9r48RRuWRLYYtiPCI9l5O/49hL3H3rzlYzRTO7Wf7inT2aT X-Received: by 2002:ac8:7d8a:0:b0:31b:7da9:d513 with SMTP id c10-20020ac87d8a000000b0031b7da9d513mr4312507qtd.500.1656403963484; Tue, 28 Jun 2022 01:12:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656403963; cv=none; d=google.com; s=arc-20160816; b=FubBX6ZoUwOAAkQGqkEKSOrqCvS1Cib1VvOfbudoucnJv/CY/e5GHRfPzI3MM15OND NTEiiIS5n5epXgNQgB12J6D3eTkazpsQHSM6redY6GytPYxsdJNYXZ8JzXAybwtIP3Fb 7Sugttcg0LFGvJOLJX7drvc8+0dqGOPxgjFwBWHWueqpajfuZhwAuU/U5M19kxDniSB1 flVFRIw3q4W9v8oUuRBnkWz5rsF3wJUTsvsshqBBD5rNUGK1c/q6R7FKVWnRonDpZnTx +S18vUbd6Qqa4YZNzOH6da4OpiK/8ENiUIr4KX/7f6bo9Cx5iROVEv9wAcjiShdQVF+u 9ioQ== 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=WmyDAD2u3RMCBNdl3aDHlvaK8YHnvUt0O1mqd+xCUGE=; b=qTS3hUbGGLGqal4iGIfigREPUAmNLfIvMyHKiCuVC+vzJtKHZxx3Ju/wpKouAcybH/ i6vgnsa1TXzZdOesgnRGKsTUw37FlocIj1kI+49uDCBwBSbGzUFenEtln0RbHpPh593v ju70Cm0sn51q0528vfG/qOs/9sK/c2uWYtTooIp/59mX2acbJWf4SvKgiLV4wyaD6rEn U24NuC7IEa3o8BsgAuPlZqjg7w+nVBeFYqferg5qsdey7vcUlCS9WWT4wJ4OX+yYYcXx pQ/TBoS7qSgqTbWNeLBrFfFDbVY8+Gm072XkTvzZSm38jrvQ7Hg15WlrJxzjguQFjoRp 0Qug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yPzNIlLR; 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 jm10-20020ad45eca000000b00470463cbee7si7167818qvb.429.2022.06.28.01.12.43 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 28 Jun 2022 01:12:43 -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=yPzNIlLR; 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]:49432 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o66Kx-0005io-2I for patch@linaro.org; Tue, 28 Jun 2022 04:12:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44148) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o63JW-00020U-1p for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:59:05 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]:53956) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o63JS-0000W5-NE for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:59:00 -0400 Received: by mail-pj1-x1032.google.com with SMTP id d14so11436914pjs.3 for ; Mon, 27 Jun 2022 21:58:58 -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=WmyDAD2u3RMCBNdl3aDHlvaK8YHnvUt0O1mqd+xCUGE=; b=yPzNIlLRl02tY5GNzHcbg5Ftr7Gjp5ATtWtnUDHZPiKklVbnNPhT7rJxV+gHfo3Xrz f1ZcnpJNXy4fwBm8RE77G4i2NscTtm0TpHKOI76D2wseGoKuF3Ly7PXKst+2euCL15wv oCIWyzHMwQXxporJ3VB+xzV6Fr2ETRFQxjOEqKX67PmRdLkXOdWrw9AXpo2YLVmhlIrk XtEbKfAvGnt2OvAbNYJRJ0zCiGXgphc/p3hgr12G907WdCYhzdib/ry5HxpSBBj6rxLo 26YHihH0+32s9iG+VM08Y3MdafQwZuiXDU25yYvWDM1xehITMk7rLvSmwjxHpN1m8xTm LK1A== 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=WmyDAD2u3RMCBNdl3aDHlvaK8YHnvUt0O1mqd+xCUGE=; b=BeIydrsXLtk1WYmX+/BjbdjpJBE2NL9IYhm7S2+D5XazBmq1AyDLWYaJ+5s2KMe1oz BCUhwKDwjVesKopPNHgsF+udcYJyajdpGMx8wVt78ONVAaHBaOY2lNF7k52nT8zQDzcf 49CBGhF3WakDNujzmcU/2LSbGEOe35+UI2gVvl1FMrJl77ms06Of1dimCcFLL/yvVoOW VjaC9rab6WAz7pzonb7EBDNl+ZAHXYa0Bm3akbkOcI4v7vC/lilWcZnzV9/NVXacies2 k2l4aWJgzdLO5d2oV9PcszR6C4hNvLEar/mWivw3l8PqysWOIhOz67gWChpbHFLiwvkT dzvw== X-Gm-Message-State: AJIora+mKedVK5rY7URXi3RqwGpJqiH+BtGbBxfyH/4233LrSNgWUqnm Gz5FW83g0UrMfUSrsqh1izMM6fh+TBrk4Q== X-Received: by 2002:a17:90b:3584:b0:1ee:fa46:3986 with SMTP id mm4-20020a17090b358400b001eefa463986mr3367219pjb.227.1656392337342; Mon, 27 Jun 2022 21:58:57 -0700 (PDT) Received: from stoup.. ([122.255.60.245]) by smtp.gmail.com with ESMTPSA id y7-20020aa78f27000000b00522c0a75c39sm8259686pfr.196.2022.06.27.21.58.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 21:58:56 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Luc Michel Subject: [PULL 43/60] semihosting: Expand qemu_semihosting_console_inc to read Date: Tue, 28 Jun 2022 10:23:46 +0530 Message-Id: <20220628045403.508716-44-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::1032; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1032.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" Allow more than one character to be read at one time. Will be used by m68k and nios2 semihosting for stdio. Reviewed-by: Luc Michel Signed-off-by: Richard Henderson --- include/semihosting/console.h | 12 +++++++----- linux-user/semihost.c | 10 ++++++---- semihosting/arm-compat-semi.c | 11 +++++++++-- semihosting/console.c | 16 ++++++++++++---- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/include/semihosting/console.h b/include/semihosting/console.h index 27f8e9ae2e..39dbf1b062 100644 --- a/include/semihosting/console.h +++ b/include/semihosting/console.h @@ -38,19 +38,21 @@ int qemu_semihosting_console_outs(CPUArchState *env, target_ulong s); void qemu_semihosting_console_outc(CPUArchState *env, target_ulong c); /** - * qemu_semihosting_console_inc: + * qemu_semihosting_console_read: * @cs: CPUState + * @buf: host buffer + * @len: buffer size * - * Receive single character from debug console. As this call may block - * if no data is available we suspend the CPU and will re-execute the + * Receive at least one character from debug console. As this call may + * block if no data is available we suspend the CPU and will re-execute the * instruction when data is there. Therefore two conditions must be met: * * - CPUState is synchronized before calling this function * - pc is only updated once the character is successfully returned * - * Returns: character read OR cpu_loop_exit! + * Returns: number of characters read, OR cpu_loop_exit! */ -target_ulong qemu_semihosting_console_inc(CPUState *cs); +int qemu_semihosting_console_read(CPUState *cs, void *buf, int len); /** * qemu_semihosting_log_out: diff --git a/linux-user/semihost.c b/linux-user/semihost.c index f14c6ae21d..2029fb674c 100644 --- a/linux-user/semihost.c +++ b/linux-user/semihost.c @@ -56,21 +56,23 @@ void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr) * program is expecting more normal behaviour. This is slow but * nothing using semihosting console reading is expecting to be fast. */ -target_ulong qemu_semihosting_console_inc(CPUState *cs) +int qemu_semihosting_console_read(CPUState *cs, void *buf, int len) { - uint8_t c; + int ret; struct termios old_tio, new_tio; /* Disable line-buffering and echo */ tcgetattr(STDIN_FILENO, &old_tio); new_tio = old_tio; new_tio.c_lflag &= (~ICANON & ~ECHO); + new_tio.c_cc[VMIN] = 1; + new_tio.c_cc[VTIME] = 0; tcsetattr(STDIN_FILENO, TCSANOW, &new_tio); - c = getchar(); + ret = fread(buf, 1, len, stdin); /* restore config */ tcsetattr(STDIN_FILENO, TCSANOW, &old_tio); - return (target_ulong) c; + return ret; } diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index 40f3730778..fdb143ace8 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -428,8 +428,15 @@ void do_common_semihosting(CPUState *cs) break; case TARGET_SYS_READC: - ret = qemu_semihosting_console_inc(cs); - common_semi_set_ret(cs, ret); + { + uint8_t ch; + int ret = qemu_semihosting_console_read(cs, &ch, 1); + if (ret == 1) { + common_semi_cb(cs, ch, 0); + } else { + common_semi_cb(cs, -1, EIO); + } + } break; case TARGET_SYS_ISERROR: diff --git a/semihosting/console.c b/semihosting/console.c index 17ece6bdca..e5ac3f20ba 100644 --- a/semihosting/console.c +++ b/semihosting/console.c @@ -144,12 +144,14 @@ static void console_read(void *opaque, const uint8_t *buf, int size) c->sleeping_cpus = NULL; } -target_ulong qemu_semihosting_console_inc(CPUState *cs) +int qemu_semihosting_console_read(CPUState *cs, void *buf, int len) { - uint8_t ch; SemihostingConsole *c = &console; + int ret = 0; g_assert(qemu_mutex_iothread_locked()); + + /* Block if the fifo is completely empty. */ if (fifo8_is_empty(&c->fifo)) { c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, cs); cs->halted = 1; @@ -157,8 +159,14 @@ target_ulong qemu_semihosting_console_inc(CPUState *cs) cpu_loop_exit(cs); /* never returns */ } - ch = fifo8_pop(&c->fifo); - return (target_ulong) ch; + + /* Read until buffer full or fifo exhausted. */ + do { + *(char *)(buf + ret) = fifo8_pop(&c->fifo); + ret++; + } while (ret < len && !fifo8_is_empty(&c->fifo)); + + return ret; } void qemu_semihosting_console_init(void)