From patchwork Tue Jun 28 04:53:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 585539 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5036:0:0:0:0 with SMTP id e22csp500912mab; Tue, 28 Jun 2022 00:14:36 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ukesL/vWPppMemVDDVorw20DjuobhA88HGjfkPlYK60tmtS43r4SkLNVfsZ7Wlw6RarfOr X-Received: by 2002:ae9:f106:0:b0:6ae:dca2:73f7 with SMTP id k6-20020ae9f106000000b006aedca273f7mr10450772qkg.266.1656400476507; Tue, 28 Jun 2022 00:14:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656400476; cv=none; d=google.com; s=arc-20160816; b=xjMPymO+OV6Ojvc1WI0yYFi0msSLltTVMN84iKNvVoQ5pAxbJgx5r13jVXNBubumth awPC5snSoYJeCLOKr+SDK6IOvQoOk35a0T5we6RU6lyZ9fR/i75/La/dCx6ZYkd+u8DG /zo4dzmQgypt27y8E6d+KReXfPkdAm+WtMNC/D4BpGMAQXg3YN7edkn9FTuNBYGYADdP 0pXu6mfJ8EMsze/dcfeCEWeLy/yn3QNQndDXCZE8ekCQt/mcjSZ3LRdSBxovA5mPpB1E l+7pMOJ4SXIaYpCsvFPZ0EGLtp4JTuE/D0Km8ljkMbd+xxfW+gSJGGoAdeG/ZGELWNEq ZcuQ== 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=hlSrerE/ONSg0LVKjyJGeSkE9veMTHprjHDwYw72eK4=; b=VgBwrJB089F+L9ne5Fq8PhXEuDAEz8sLGjJcy2R/iGN3NyH5ZODbdV5cM93I09DU6+ JRv6zBHva/Y1y49n/d2O4oBiXJYkuSt9THvTAnOige0Zc4SyG+I17rn8tGtnxCg2k4eb N85wXGSUwuKp1TXTWDyq1lwmeGzrGiNvEyYIoedgv7aI8p0eOWOzdWbM3aGRVnvZqAkg +NO4eZF9a7D9miA281uG1djc15jjHThTrSh3L6tmI7V/ZrPj/O8+UF/ZygPBaiAI/Xi/ qHaunorDLUypPIPsHaDPNSu24kQxJACurydS06vzYuPOdg+oW+L4N1flzU9jmX9DPn3R 0fqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WBu+wYa4; 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 j11-20020a05621419cb00b0046e46e4326dsi7153299qvc.78.2022.06.28.00.14.36 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 28 Jun 2022 00:14:36 -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=WBu+wYa4; 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]:51144 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o65Qh-0002db-Sh for patch@linaro.org; Tue, 28 Jun 2022 03:14:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44192) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o63Jc-00021q-Hs for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:59:09 -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 1o63JZ-0000W5-IW for qemu-devel@nongnu.org; Tue, 28 Jun 2022 00:59:07 -0400 Received: by mail-pj1-x1032.google.com with SMTP id d14so11436914pjs.3 for ; Mon, 27 Jun 2022 21:59:04 -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=hlSrerE/ONSg0LVKjyJGeSkE9veMTHprjHDwYw72eK4=; b=WBu+wYa4ZYQQg+UltowXTuhS+SlEhkRbLgY+DJZmcvY0DcvkkdVlPjvYuPadJvFyCi s/aDbc9eBX71n2NF37BOPyzD7OomMpwOkZNhQzQCkIg8pvTpnc96fUVmWPIOTEmk8E6A xRyBaElITNA4NItBnMfFU6/cVraqLR1h1ATK8Yap0WLTI0lkB/H2WNzIO8pGs826HJPQ /TtDjUFTZaUOHNTDazuGYtWCliOL3GahwgovCEZzRiikTCSfgnDkBHhk/Nuhd0fYHf5C emWT9knXu1TTTj2pDmyaYWdHWQfYz/WoDOfYpSqbdHej7wteKKVbfRw+teE20m23o+St 6qNw== 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=hlSrerE/ONSg0LVKjyJGeSkE9veMTHprjHDwYw72eK4=; b=o88K5Y2QSHu7serpZVyK+8mJORTtULIeEhvoS/nbUIxpN+AZMxyWkhBioW/jMAoocf Q+gOjhKvAjeU5qSBjVVYBDbNfUzGs8bqUMXsx3xVwBoiuUgm9FYdI4toGbAPTd0e5gqU 6m4nACyhnQCoq1gfFO0a2lskU0rQId9DroddHTZvzRZyG+MHNCRNhghrt6Hk6aQ4m+DW vSTqAvAjj/qFLBo0nOSnLXVZ61107Rob2v6CXebpnsDMZ8J/Mpb/LoQyCFWyEm3YgCPM lLLYhuqTUNWy9a/BrQ0b0Riu0JsvfMBTmyqqjLNOKW3+bFlIinFIgRv/EUOS0y2AQJ5e L+hw== X-Gm-Message-State: AJIora9BBidCj4nMBY9SOxIQ3rz9CzRi9fzwC9hSGYt1lBOnUMQuUGza 0h9eI+UPTPedsCxptj0hTs8r+G6AIFG9dQ== X-Received: by 2002:a17:903:328e:b0:16b:8745:bb77 with SMTP id jh14-20020a170903328e00b0016b8745bb77mr3030872plb.70.1656392344208; Mon, 27 Jun 2022 21:59:04 -0700 (PDT) Received: from stoup.. ([122.255.60.245]) by smtp.gmail.com with ESMTPSA id y7-20020aa78f27000000b00522c0a75c39sm8259686pfr.196.2022.06.27.21.59.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jun 2022 21:59:03 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Luc Michel , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 46/60] semihosting: Add GuestFDConsole Date: Tue, 28 Jun 2022 10:23:49 +0530 Message-Id: <20220628045403.508716-47-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" Add a GuestFDType for connecting to the semihosting console. Hook up to read, write, isatty, and fstat syscalls. Note that the arm-specific syscall flen cannot be applied to the console, because the console is not a descriptor exposed to the guest. Reviewed-by: Luc Michel Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- include/semihosting/guestfd.h | 7 ++-- semihosting/syscalls.c | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/include/semihosting/guestfd.h b/include/semihosting/guestfd.h index ef268abe85..a7ea1041ea 100644 --- a/include/semihosting/guestfd.h +++ b/include/semihosting/guestfd.h @@ -13,9 +13,10 @@ typedef enum GuestFDType { GuestFDUnused = 0, - GuestFDHost = 1, - GuestFDGDB = 2, - GuestFDStatic = 3, + GuestFDHost, + GuestFDGDB, + GuestFDStatic, + GuestFDConsole, } GuestFDType; /* diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c index 13a9bdeda6..9e499b1751 100644 --- a/semihosting/syscalls.c +++ b/semihosting/syscalls.c @@ -10,6 +10,7 @@ #include "exec/gdbstub.h" #include "semihosting/guestfd.h" #include "semihosting/syscalls.h" +#include "semihosting/console.h" #ifdef CONFIG_USER_ONLY #include "qemu.h" #else @@ -577,6 +578,56 @@ static void staticfile_flen(CPUState *cs, gdb_syscall_complete_cb complete, complete(cs, gf->staticfile.len, 0); } +/* + * Console semihosting syscall implementations. + */ + +static void console_read(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, target_ulong buf, target_ulong len) +{ + CPUArchState *env G_GNUC_UNUSED = cs->env_ptr; + char *ptr; + int ret; + + ptr = lock_user(VERIFY_WRITE, buf, len, 0); + if (!ptr) { + complete(cs, -1, EFAULT); + return; + } + ret = qemu_semihosting_console_read(cs, ptr, len); + complete(cs, ret, 0); + unlock_user(ptr, buf, ret); +} + +static void console_write(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, target_ulong buf, target_ulong len) +{ + CPUArchState *env G_GNUC_UNUSED = cs->env_ptr; + char *ptr = lock_user(VERIFY_READ, buf, len, 1); + int ret; + + if (!ptr) { + complete(cs, -1, EFAULT); + return; + } + ret = qemu_semihosting_console_write(ptr, len); + complete(cs, ret ? ret : -1, ret ? 0 : EIO); + unlock_user(ptr, buf, ret); +} + +static void console_fstat(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, target_ulong addr) +{ + static const struct stat tty_buf = { + .st_mode = 020666, /* S_IFCHR, ugo+rw */ + .st_rdev = 5, /* makedev(5, 0) -- linux /dev/tty */ + }; + int ret; + + ret = copy_stat_to_user(cs, addr, &tty_buf); + complete(cs, ret ? -1 : 0, ret ? -ret : 0); +} + /* * Syscall entry points. */ @@ -608,6 +659,7 @@ void semihost_sys_close(CPUState *cs, gdb_syscall_complete_cb complete, int fd) host_close(cs, complete, gf); break; case GuestFDStatic: + case GuestFDConsole: complete(cs, 0, 0); break; default: @@ -637,6 +689,9 @@ void semihost_sys_read_gf(CPUState *cs, gdb_syscall_complete_cb complete, case GuestFDStatic: staticfile_read(cs, complete, gf, buf, len); break; + case GuestFDConsole: + console_read(cs, complete, gf, buf, len); + break; default: g_assert_not_reached(); } @@ -672,6 +727,9 @@ void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete, case GuestFDHost: host_write(cs, complete, gf, buf, len); break; + case GuestFDConsole: + console_write(cs, complete, gf, buf, len); + break; case GuestFDStatic: /* Static files are never open for writing: EBADF. */ complete(cs, -1, EBADF); @@ -712,6 +770,9 @@ void semihost_sys_lseek(CPUState *cs, gdb_syscall_complete_cb complete, case GuestFDStatic: staticfile_lseek(cs, complete, gf, off, gdb_whence); break; + case GuestFDConsole: + complete(cs, -1, ESPIPE); + break; default: g_assert_not_reached(); } @@ -735,6 +796,9 @@ void semihost_sys_isatty(CPUState *cs, gdb_syscall_complete_cb complete, int fd) case GuestFDStatic: complete(cs, 0, ENOTTY); break; + case GuestFDConsole: + complete(cs, 1, 0); + break; default: g_assert_not_reached(); } @@ -760,6 +824,7 @@ void semihost_sys_flen(CPUState *cs, gdb_syscall_complete_cb fstat_cb, case GuestFDStatic: staticfile_flen(cs, flen_cb, gf); break; + case GuestFDConsole: default: g_assert_not_reached(); } @@ -781,6 +846,9 @@ void semihost_sys_fstat(CPUState *cs, gdb_syscall_complete_cb complete, case GuestFDHost: host_fstat(cs, complete, gf, addr); break; + case GuestFDConsole: + console_fstat(cs, complete, gf, addr); + break; case GuestFDStatic: default: g_assert_not_reached();