From patchwork Sat May 21 00:04:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 574925 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1f0e:0:0:0:0 with SMTP id hs14csp2672421mab; Fri, 20 May 2022 17:35:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhJX0ym7u49zCdLTtQJu3tO35afmkzGfSFM4x7bQ8ncIp8oTJtNt4hLRF0AAwx8seK2sXg X-Received: by 2002:ac8:5891:0:b0:2f3:ba03:429 with SMTP id t17-20020ac85891000000b002f3ba030429mr9513438qta.13.1653093348982; Fri, 20 May 2022 17:35:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653093348; cv=none; d=google.com; s=arc-20160816; b=oIzBlCBVZQWL6442t5MLW3MzHV1gHSAryq7LPsho+XdB3j1ixciC+Bv0IrTkY6smPV cSK9x6HTWF3plxVZvd7fQ2F14G90CRfhteaAL/BiQpGQK+STFtXoivw7YzMnO0Zlznh4 c3l2eCf1eGm3cIgkNTHq8Sq9PwuBugFoSVTXuAY2EMV02+d5mVO2qOthdb+S/uwDj9qo hm67VlhD9QTZ9i0C6VXLIYmTETVY1yQFKSQ8s8L1zTgkxpBYUfPsRjB25qMaXouz5r6w Po9ENwujVn5FSxAYxe48tweYvaooy3zuoiCHzArWtgK3IZOEkLXGghrXA4XbnlkQ8Rk/ GaxA== 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=yIETyNF8e+iDYubG7UAr54h79EzcA695Ph0Ym5jO9Uo=; b=zuCyHaoVbmeTO/1npYNfvus1qjIoIkX4THw80q8ncLn+n11yF5eASpI4u28vAqUy/j fjNVgZkH8HMOnt/MnfqN0jWSU63RmS7slPF93LxsHHGTnjF9wbtu5re8TW4NkUEPpYuP 2VDBPANKk6U77QGbMeKBqptE76EaA0lVmXSGErCV5Ouli7lacIbly+qCRQ9IQRMNGcQv IhnK5jaWlGASvzHriqjJ/iijiwt8hx1NJ51ZQdeHXE3Zo40vlKXr0cmKPzbJHVPHKwhu wkEWnJ2Oz74LTgWQJX+xk6me3LubzR1IPKh9xLGo47GddGQ1eS9f5d4vBf9x9FgWkPWU tY8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=xsNJcIjx; 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 iu3-20020ad45cc3000000b004564f8f5e9dsi368638qvb.454.2022.05.20.17.35.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 20 May 2022 17:35:48 -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=xsNJcIjx; 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]:36822 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nsD5w-0004ns-JG for patch@linaro.org; Fri, 20 May 2022 20:35:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60076) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsCf2-00039X-Hh for qemu-devel@nongnu.org; Fri, 20 May 2022 20:08:00 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]:35635) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nsCf0-0004aH-9J for qemu-devel@nongnu.org; Fri, 20 May 2022 20:07:59 -0400 Received: by mail-pj1-x1031.google.com with SMTP id nk9-20020a17090b194900b001df2fcdc165so12804958pjb.0 for ; Fri, 20 May 2022 17:07:57 -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=yIETyNF8e+iDYubG7UAr54h79EzcA695Ph0Ym5jO9Uo=; b=xsNJcIjxRM8IcFuxnv1J3ocV9SpPogNjRsRIuIHQuMjH/HMOgstJjMM4Mht65QaMKN keZfxAvAlJTHwxOajGx0nIidNv2Bf1eaH6MKJn3Ffv4akMuMUdezow7/D2j0dQWA/kT4 TPpm3cSGagMDZNYcGXxie/w9xFz02ZBX72HFa2xPoboFBz4IWiav2z2NiSSaJoQM9ZBd TALnmjvFmTRdc5hMxvMaVeYpVtRWtQwSYmW31OYzza9kFl5ueXITNEk8GfP//3AfaFl4 2+ycdqt1BykI9/Tl0TiWART7BgDzOJbRl1G5L3QKefBCdG9XbpfXX97aO74CuJMq4J/2 8Leg== 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=yIETyNF8e+iDYubG7UAr54h79EzcA695Ph0Ym5jO9Uo=; b=ptR5aDSgYxiOFc/dJv21MDC74gN4KaQcv+NRE1XISYdbXTo6uLIGR35755fnfN8zcW 9+HhDIPqaMM1FAqk9h5bhdEtZ63ipyzpqAcgSugXxlCp3LMnDgzxGvrLxePztx1aDdND UnRt7ZqtTHO71+xzY1ytli7BscClaX2XM0d4j0kOu3Iz8uBOZAafkFONz39m+6O1rSt4 yg2KAtgrAuxfPrjvroDBhOY+JhhSXPGEPnvCSGzOi4PlAi058uLvvPgyygrSH/v9N0H/ IB2sbeyWlFvE3F4W6/+EcjIOS4OV37rcUfYcpNm7e9BXKNpq6j+rMV4NHGYf5PisUhHj bTfA== X-Gm-Message-State: AOAM533dE3NSGCJjFI2s1j9XEPd+aEhw5CiSYGohWxan64V5w1CZhxcW BjBcKQAG9+J5pJMnTzD/0nHJ4KApD4lD9Q== X-Received: by 2002:a17:90b:1b4b:b0:1de:d495:8bc3 with SMTP id nv11-20020a17090b1b4b00b001ded4958bc3mr14067976pjb.200.1653091676915; Fri, 20 May 2022 17:07:56 -0700 (PDT) Received: from stoup.. ([71.212.142.129]) by smtp.gmail.com with ESMTPSA id 1-20020a17090a0cc100b001d2bff34228sm2517703pjt.9.2022.05.20.17.07.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 May 2022 17:07:56 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, alex.bennee@linaro.org Subject: [PATCH v3 49/49] semihosting: Create semihost_sys_poll_one Date: Fri, 20 May 2022 17:04:00 -0700 Message-Id: <20220521000400.454525-50-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220521000400.454525-1-richard.henderson@linaro.org> References: <20220521000400.454525-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1031.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" This will be used for implementing the xtensa select_one system call. Choose "poll" over "select" so that we can reuse Glib's g_poll constants and to avoid struct timeval. Signed-off-by: Richard Henderson --- include/semihosting/console.h | 16 ++++++++ include/semihosting/syscalls.h | 3 ++ semihosting/console.c | 19 ++++++++- semihosting/syscalls.c | 70 ++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) diff --git a/include/semihosting/console.h b/include/semihosting/console.h index 20c31d89d4..61b0cb3a94 100644 --- a/include/semihosting/console.h +++ b/include/semihosting/console.h @@ -53,4 +53,20 @@ int qemu_semihosting_console_write(void *buf, int len); */ int qemu_semihosting_log_out(const char *s, int len); +/* + * qemu_semihosting_console_block_until_ready: + * @cs: CPUState + * + * If no data is available we suspend the CPU and will re-execute the + * instruction when data is available. + */ +void qemu_semihosting_console_block_until_ready(CPUState *cs); + +/** + * qemu_semihosting_console_ready: + * + * Return true if characters are available for read; does not block. + */ +bool qemu_semihosting_console_ready(void); + #endif /* SEMIHOST_CONSOLE_H */ diff --git a/include/semihosting/syscalls.h b/include/semihosting/syscalls.h index 347200cb9f..3a5ec229eb 100644 --- a/include/semihosting/syscalls.h +++ b/include/semihosting/syscalls.h @@ -69,4 +69,7 @@ void semihost_sys_system(CPUState *cs, gdb_syscall_complete_cb complete, void semihost_sys_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete, target_ulong tv_addr, target_ulong tz_addr); +void semihost_sys_poll_one(CPUState *cs, gdb_syscall_complete_cb complete, + int fd, GIOCondition cond, int timeout); + #endif /* SEMIHOSTING_SYSCALLS_H */ diff --git a/semihosting/console.c b/semihosting/console.c index c84ab97ab6..cda7cf1905 100644 --- a/semihosting/console.c +++ b/semihosting/console.c @@ -77,10 +77,17 @@ static void console_read(void *opaque, const uint8_t *buf, int size) c->sleeping_cpus = NULL; } -int qemu_semihosting_console_read(CPUState *cs, void *buf, int len) +bool qemu_semihosting_console_ready(void) +{ + SemihostingConsole *c = &console; + + g_assert(qemu_mutex_iothread_locked()); + return !fifo8_is_empty(&c->fifo); +} + +void qemu_semihosting_console_block_until_ready(CPUState *cs) { SemihostingConsole *c = &console; - int ret = 0; g_assert(qemu_mutex_iothread_locked()); @@ -92,6 +99,14 @@ int qemu_semihosting_console_read(CPUState *cs, void *buf, int len) cpu_loop_exit(cs); /* never returns */ } +} + +int qemu_semihosting_console_read(CPUState *cs, void *buf, int len) +{ + SemihostingConsole *c = &console; + int ret = 0; + + qemu_semihosting_console_block_until_ready(cs); /* Read until buffer full or fifo exhausted. */ do { diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c index aa4ee47649..1b87c37437 100644 --- a/semihosting/syscalls.c +++ b/semihosting/syscalls.c @@ -550,6 +550,21 @@ static void host_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete, unlock_user(p, tv_addr, sizeof(struct gdb_timeval)); } +#ifndef CONFIG_USER_ONLY +static void host_poll_one(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, GIOCondition cond, int timeout) +{ + /* + * Since this is only used by xtensa in system mode, and stdio is + * handled through GuestFDConsole, and there are no semihosting + * system calls for sockets and the like, that means this descriptor + * must be a normal file. Normal files never block and are thus + * always ready. + */ + complete(cs, cond & (G_IO_IN | G_IO_OUT), 0); +} +#endif + /* * Static file semihosting syscall implementations. */ @@ -658,6 +673,34 @@ static void console_fstat(CPUState *cs, gdb_syscall_complete_cb complete, complete(cs, ret ? -1 : 0, ret ? -ret : 0); } +#ifndef CONFIG_USER_ONLY +static void console_poll_one(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, GIOCondition cond, int timeout) +{ + /* The semihosting console does not support urgent data or errors. */ + cond &= G_IO_IN | G_IO_OUT; + + /* + * Since qemu_semihosting_console_write never blocks, we can + * consider output always ready -- leave G_IO_OUT alone. + * All that remains is to conditionally signal input ready. + * Since output ready causes an immediate return, only block + * for G_IO_IN alone. + * + * TODO: Implement proper timeout. For now, only support + * indefinite wait or immediate poll. + */ + if (cond == G_IO_IN && timeout < 0) { + qemu_semihosting_console_block_until_ready(cs); + /* We returned -- input must be ready. */ + } else if ((cond & G_IO_IN) && !qemu_semihosting_console_ready()) { + cond &= ~G_IO_IN; + } + + complete(cs, cond, 0); +} +#endif + /* * Syscall entry points. */ @@ -936,3 +979,30 @@ void semihost_sys_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete, host_gettimeofday(cs, complete, tv_addr, tz_addr); } } + +#ifndef CONFIG_USER_ONLY +void semihost_sys_poll_one(CPUState *cs, gdb_syscall_complete_cb complete, + int fd, GIOCondition cond, int timeout) +{ + GuestFD *gf = get_guestfd(fd); + + if (!gf) { + complete(cs, G_IO_NVAL, 1); + return; + } + switch (gf->type) { + case GuestFDGDB: + complete(cs, G_IO_NVAL, 1); + break; + case GuestFDHost: + host_poll_one(cs, complete, gf, cond, timeout); + break; + case GuestFDConsole: + console_poll_one(cs, complete, gf, cond, timeout); + break; + case GuestFDStatic: + default: + g_assert_not_reached(); + } +} +#endif