From patchwork Sat May 21 00:03:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 574902 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1f0e:0:0:0:0 with SMTP id hs14csp2661921mab; Fri, 20 May 2022 17:11:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJydlW3vMoLsLygPzAFyM9bX5Ans4EUB0s5y/fok2Q2KVLSifjdJzyKPaubLnq63K6hgrotL X-Received: by 2002:a05:622a:178a:b0:2f9:2257:69e3 with SMTP id s10-20020a05622a178a00b002f9225769e3mr2897868qtk.568.1653091898168; Fri, 20 May 2022 17:11:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653091898; cv=none; d=google.com; s=arc-20160816; b=eZ8sa+AX58Xc3JW7zOwqygkRuzbphX/v/H8puGle8NxglhwSumTu04t7ZOY2j3QvpX 4T8JC4UcajYSGRl0ZQ9cFzFGQAknODxLoavoZjkzQYnKRVF8Y6Om1qvNV6vh+1pbokuO 3hOBaMWLQlgIpFjJEvrvQ3l7TmhID2iL7LDj1H/nLVC0qUOZQhrV+Vh9d0BhMVCyB4Zc On0DfzHWGuo84l3zyC40rUicLAPJzvATomZ4JKu7uJti3UGUqumYUCF6je4nrYGx22cU A4F0O5n+P/B2GHW+Spk8s6PURA22TrzJq24yOvz6wBIWuayGvdJ4S0v50CivJAKtxU00 OuGg== 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=tgVRADqR9B5grnNVu8mA4b/Up+ANot4pk44Zo7iuCRQ=; b=JY03SYf45yF25r8SGrzjK4JsiLiChuVJJrpRa6nk0RvhwL2ipViwuivXFkfioR10/l O2Ir1L/GEZgeWFcSDSqQ2xUYUXJDk44TY6hbCuaEn2c06rPQRz77xEysDgzWdBSiC/Q3 GOHxAdw5c6ogkviV1pJtqmoV3wjPgDCvOqcMKd8ypjbmDvZBDKNU/DPv3vxT7k7glrfH oA6sn8cZYFSQuFc4V+2EaFfWQnlJhPIzN4F8xpLjU7QPacZ6zfuOAygIANvoI1bpqUsn VQJDNlkuAXhAD90dDgpwzgHYlm3eib8m8aYHzk01c/BuFxeiNQ/KLk2fYxQnZllq/x0S quUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TV6pjn+8; 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 i13-20020a05620a248d00b0069f7490d35esi441004qkn.116.2022.05.20.17.11.38 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 20 May 2022 17:11:38 -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=TV6pjn+8; 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]:37286 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nsCiX-00087x-OV for patch@linaro.org; Fri, 20 May 2022 20:11:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59492) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nsCbw-0008Bj-GT for qemu-devel@nongnu.org; Fri, 20 May 2022 20:04:56 -0400 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]:46592) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nsCbg-0003z2-RD for qemu-devel@nongnu.org; Fri, 20 May 2022 20:04:46 -0400 Received: by mail-pg1-x529.google.com with SMTP id j21so8768982pga.13 for ; Fri, 20 May 2022 17:04:32 -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=tgVRADqR9B5grnNVu8mA4b/Up+ANot4pk44Zo7iuCRQ=; b=TV6pjn+81ArueSqW6Xzf2F845lAzmHMJaHhlZTBRl1LxzOwBwys4Qx9Li4zX36Pk08 RrfN0S+i2bMtF4nAwq/pyA2gqlDWS12Sv/uJ4UQSM4guuvKI0qhrkP74fMPbxrzVhkr5 z9vh2DjiXtRRTYxDHc4uYgL7ud5Ywu9omQEJdiK1IEIm2Mxyr2vryD4HU5xI3JpcvaMd g1meOcQt7xx/Bo178J5OI3Tn2L16vIqzuTGTVkqGAdSm5cQUkPzSjQYqRfF9s7LSYk0g zdn1NRqkFTrZrdBgaIW5DmR1YuKMBBvILfb4Hax+opH5PXXIWuLllJnh/AX6MYMLYd1L /IUQ== 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=tgVRADqR9B5grnNVu8mA4b/Up+ANot4pk44Zo7iuCRQ=; b=YlmgCxTFLeSqoqRuGth5TrlH0BbwcOEyueGtMbB4lpIrFInECYAuUfgmyrL08cDT+z +xYnJ9TWN3COwI6z3ZoYZ7p68xDERcV/gfVk9D7+gnB0TLBkpYMaZnBMWz5UjMURGbZU 2WsULXwIkH+fMdGMRDYMrHj1hxojvzOp06CmiONHhDrHqyqosyL+N0shwqd7G22uYTgq 8WM2KI5YeYnn+KQFfMy7TvaIVR8wH1q6rz2NpxB2Xv1WTGPBC8lT1MDwtfwkMZSWmYYp 0g1TgwKg5Li8AlYij5I4DQcOG+o4T23wkbobGsmxmM0oX5/Gyj/Zs8u3Ue9ij19/qam1 88XA== X-Gm-Message-State: AOAM5302uh0z6dIM84IS31ZuwxAKH4CTq7MK4Bn7jQUpZSRjU7mWCx4n k/YK0Up5RcF/79SPGoKyKWeuhupZTBBiug== X-Received: by 2002:aa7:92d2:0:b0:518:4929:e4cc with SMTP id k18-20020aa792d2000000b005184929e4ccmr9053431pfa.37.1653091471768; Fri, 20 May 2022 17:04:31 -0700 (PDT) Received: from stoup.. ([71.212.142.129]) by smtp.gmail.com with ESMTPSA id h8-20020a654808000000b003f9d1c020cbsm306369pgs.51.2022.05.20.17.04.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 May 2022 17:04:31 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, alex.bennee@linaro.org Subject: [PATCH v3 26/49] semihosting: Split out semihost_sys_write Date: Fri, 20 May 2022 17:03:37 -0700 Message-Id: <20220521000400.454525-27-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::529; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x529.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_WRITE to a reusable function. This handles all GuestFD. This removes the last use of common_semi_syscall_len. Note that gdb_do_syscall %x reads target_ulong, not int. Signed-off-by: Richard Henderson --- include/semihosting/syscalls.h | 6 ++++ semihosting/arm-compat-semi.c | 52 +------------------------------- semihosting/syscalls.c | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 51 deletions(-) diff --git a/include/semihosting/syscalls.h b/include/semihosting/syscalls.h index 20da8138b0..2464467579 100644 --- a/include/semihosting/syscalls.h +++ b/include/semihosting/syscalls.h @@ -33,4 +33,10 @@ void semihost_sys_read(CPUState *cs, gdb_syscall_complete_cb complete, void semihost_sys_read_gf(CPUState *cs, gdb_syscall_complete_cb complete, GuestFD *gf, target_ulong buf, target_ulong len); +void semihost_sys_write(CPUState *cs, gdb_syscall_complete_cb complete, + int fd, target_ulong buf, target_ulong len); + +void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, target_ulong buf, target_ulong len); + #endif /* SEMIHOSTING_SYSCALLS_H */ diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c index 5e4101c6a7..1238b81e0a 100644 --- a/semihosting/arm-compat-semi.c +++ b/semihosting/arm-compat-semi.c @@ -215,8 +215,6 @@ static inline uint32_t get_swi_errno(CPUState *cs) #endif } -static target_ulong common_semi_syscall_len; - static void common_semi_cb(CPUState *cs, target_ulong ret, target_ulong err) { if (err) { @@ -230,9 +228,6 @@ static void common_semi_cb(CPUState *cs, target_ulong ret, target_ulong err) /* Fixup syscalls that use nonstardard return conventions. */ target_ulong reg0 = common_semi_arg(cs, 0); switch (reg0) { - case TARGET_SYS_WRITE: - ret = common_semi_syscall_len - ret; - break; case TARGET_SYS_SEEK: ret = 0; break; @@ -294,30 +289,10 @@ 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_writefn(CPUState *cs, GuestFD *gf, - target_ulong buf, uint32_t len); typedef void sys_isattyfn(CPUState *cs, GuestFD *gf); typedef void sys_seekfn(CPUState *cs, GuestFD *gf, target_ulong offset); typedef void sys_flenfn(CPUState *cs, GuestFD *gf); -static void host_writefn(CPUState *cs, GuestFD *gf, - target_ulong buf, uint32_t len) -{ - CPUArchState *env = cs->env_ptr; - uint32_t ret = 0; - char *s = lock_user(VERIFY_READ, buf, len, 1); - (void) env; /* Used in arm softmmu lock_user implicitly */ - if (s) { - ret = write(gf->hostfd, s, len); - unlock_user(s, buf, 0); - if (ret == (uint32_t)-1) { - ret = 0; - } - } - /* Return bytes not written, on error as well. */ - common_semi_cb(cs, len - ret, 0); -} - static void host_isattyfn(CPUState *cs, GuestFD *gf) { common_semi_cb(cs, isatty(gf->hostfd), 0); @@ -340,13 +315,6 @@ static void host_flenfn(CPUState *cs, GuestFD *gf) } } -static void gdb_writefn(CPUState *cs, GuestFD *gf, - target_ulong buf, uint32_t len) -{ - common_semi_syscall_len = len; - gdb_do_syscall(common_semi_cb, "write,%x,%x,%x", gf->hostfd, buf, len); -} - static void gdb_isattyfn(CPUState *cs, GuestFD *gf) { gdb_do_syscall(common_semi_cb, "isatty,%x", gf->hostfd); @@ -380,13 +348,6 @@ static const uint8_t featurefile_data[] = { SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */ }; -static void staticfile_writefn(CPUState *cs, GuestFD *gf, - target_ulong buf, uint32_t len) -{ - /* This fd can never be open for writing */ - common_semi_cb(cs, -1, EBADF); -} - static void staticfile_isattyfn(CPUState *cs, GuestFD *gf) { common_semi_cb(cs, 0, 0); @@ -404,7 +365,6 @@ static void staticfile_flenfn(CPUState *cs, GuestFD *gf) } typedef struct GuestFDFunctions { - sys_writefn *writefn; sys_isattyfn *isattyfn; sys_seekfn *seekfn; sys_flenfn *flenfn; @@ -412,19 +372,16 @@ typedef struct GuestFDFunctions { static const GuestFDFunctions guestfd_fns[] = { [GuestFDHost] = { - .writefn = host_writefn, .isattyfn = host_isattyfn, .seekfn = host_seekfn, .flenfn = host_flenfn, }, [GuestFDGDB] = { - .writefn = gdb_writefn, .isattyfn = gdb_isattyfn, .seekfn = gdb_seekfn, .flenfn = gdb_flenfn, }, [GuestFDStatic] = { - .writefn = staticfile_writefn, .isattyfn = staticfile_isattyfn, .seekfn = staticfile_seekfn, .flenfn = staticfile_flenfn, @@ -449,7 +406,6 @@ void do_common_semihosting(CPUState *cs) char * s; int nr; uint32_t ret; - uint32_t len; GuestFD *gf; int64_t elapsed; @@ -530,13 +486,7 @@ void do_common_semihosting(CPUState *cs) GET_ARG(0); GET_ARG(1); GET_ARG(2); - len = arg2; - - gf = get_guestfd(arg0); - if (!gf) { - goto do_badf; - } - guestfd_fns[gf->type].writefn(cs, gf, arg1, len); + semihost_sys_write(cs, common_semi_rw_cb, arg0, arg1, arg2); break; case TARGET_SYS_READ: diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c index 7c18f77898..db4561b798 100644 --- a/semihosting/syscalls.c +++ b/semihosting/syscalls.c @@ -137,6 +137,13 @@ static void gdb_read(CPUState *cs, gdb_syscall_complete_cb complete, (target_ulong)gf->hostfd, buf, len); } +static void gdb_write(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, target_ulong buf, target_ulong len) +{ + gdb_do_syscall(complete, "write,%x,%x,%x", + (target_ulong)gf->hostfd, buf, len); +} + /* * Host semihosting syscall implementations. */ @@ -223,6 +230,22 @@ static void host_read(CPUState *cs, gdb_syscall_complete_cb complete, } } +static void host_write(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, target_ulong buf, target_ulong len) +{ + CPUArchState *env G_GNUC_UNUSED = cs->env_ptr; + void *ptr = lock_user(VERIFY_READ, buf, len, 1); + ssize_t ret; + + if (!ptr) { + complete(cs, -1, GDB_EFAULT); + return; + } + ret = write(gf->hostfd, ptr, len); + complete(cs, ret, ret == -1 ? errno_for_gdb() : 0); + unlock_user(ptr, buf, 0); +} + /* * Static file semihosting syscall implementations. */ @@ -316,3 +339,34 @@ void semihost_sys_read(CPUState *cs, gdb_syscall_complete_cb complete, complete(cs, -1, GDB_EBADF); } } + +void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete, + GuestFD *gf, target_ulong buf, target_ulong len) +{ + switch (gf->type) { + case GuestFDGDB: + gdb_write(cs, complete, gf, buf, len); + break; + case GuestFDHost: + host_write(cs, complete, gf, buf, len); + break; + case GuestFDStatic: + /* Static files are never open for writing: EBADF. */ + complete(cs, -1, GDB_EBADF); + break; + default: + g_assert_not_reached(); + } +} + +void semihost_sys_write(CPUState *cs, gdb_syscall_complete_cb complete, + int fd, target_ulong buf, target_ulong len) +{ + GuestFD *gf = get_guestfd(fd); + + if (gf) { + semihost_sys_write_gf(cs, complete, gf, buf, len); + } else { + complete(cs, -1, GDB_EBADF); + } +}