From patchwork Fri Jun 7 09:05:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 166110 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp420999ili; Fri, 7 Jun 2019 02:56:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqyb0migFTauVRoEYt9N8ru2xEhJE3C10HGTJeyqwlc7+t2dsNEBiAytO5U4LiRgmsWP+ENA X-Received: by 2002:a37:e30b:: with SMTP id y11mr24381345qki.100.1559901398082; Fri, 07 Jun 2019 02:56:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559901398; cv=none; d=google.com; s=arc-20160816; b=RCbcDSRfWJPFRS34OMr/22DGWVxcjNeNRZYuqJpIvxzSi26qIQwbuvC2XMsre5135B /b7Efjk5Txy6CEbFyt9i2ezdz5PxjNi/V8fVTHV5tpnb3fHUnYJEkZZJ59vo9Gf5KGnH QYl6RTriKOWxBuSdx/4DoOgbRu9c5CnqeJfAZ5jN+2W7ySWINNwtt1XIRLmcjOfwAKA9 GOqotUffOhluhwM0YC0ttsJ5BRqVCatWoe3jacN3Xr+B30iq0YEoMkXHYNduaZa+ReY7 vDHZv6GY8BRBtYU8OrRaA2gUaIDMC6MO9fSPeLvmA7XSTq3jZ24hwBZWv2FF9I3Qufiw k6gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=R7hQv/k6BU4/ZnGK1cbG5kk+E/CvNF37llrzUvfKDoc=; b=ACaWJujAZ8SuyxUmCUuJRPpwKPkponK1U3OaOhDc9mM6vXLuaSVH08D5GebgclKY+V 3h97guq41Any7liPVbYvv1lm0p4/ZAYWZqOOeYMWVgV8thwhTdXgn36yp2BLJPTn2awX 2MYLXL/DWcqAFWlg43NTMcMu81aHzamWCPDxyLZQVKE5j4lu3vb6iur04u80ZQ3PQk7a 5MVkh8YzPX0GiQSIsSXqNz9cBjVAu0wKjA0VjYBimsd/lVJy2OrLDDkGCAYeCrBwbIHM Sh6JGDUUmK+ehUP3aUoBeAlS0kTxIRWnIefmc2BpaeDyKvLpB6nAGLSgr/Ww5gK/fxW9 lu/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=XbG7yfs0; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.47 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.47]) by mx.google.com with ESMTPS id c10si299501qte.165.2019.06.07.02.56.37 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 07 Jun 2019 02:56:38 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.47 as permitted sender) client-ip=209.51.188.47; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=XbG7yfs0; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.47 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:47692 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hZBbx-0004hO-Ld for patch@linaro.org; Fri, 07 Jun 2019 05:56:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42286) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hZAvV-0002HY-Pn for qemu-devel@nongnu.org; Fri, 07 Jun 2019 05:12:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hZAvQ-0004zz-WD for qemu-devel@nongnu.org; Fri, 07 Jun 2019 05:12:45 -0400 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]:43837) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hZAvQ-0004lF-Lt for qemu-devel@nongnu.org; Fri, 07 Jun 2019 05:12:40 -0400 Received: by mail-wr1-x442.google.com with SMTP id r18so1351564wrm.10 for ; Fri, 07 Jun 2019 02:12:34 -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=R7hQv/k6BU4/ZnGK1cbG5kk+E/CvNF37llrzUvfKDoc=; b=XbG7yfs0OkAkZF/+fRCikzBlFe1VRQRbKYo/xU+YesBY5mjZ+F5kS/AhoAKAQEUHr7 Sk9WfUbnXZpw0nYSruYTH+gtHuQs/3AwRxakncQY17Wat2GGtry6DFJnY9wfa55JlyDb BdGi2E4TQICaEH/4mdzjoiRMLRXuEb7ZmLolAJhKIfzuSWEowkY/rRoq0VfmeSiblyP3 jS3jVvzQfTEqybNstzARnTAwkUjaqwAN67nmlsV7LVt0lOBfwIuKSBS/wSWIzYEs6zCd G+HNU0xjhwGP8ucLHAWiTuWfTrryGP6PAqOl1dxaQwMuOgsg6FIYo7WJzP2nj4kzjKUh aEXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=R7hQv/k6BU4/ZnGK1cbG5kk+E/CvNF37llrzUvfKDoc=; b=bL1etqZMgE58MwjV9X4IQP+3UpuHVpwBAiOKAqUmbBzgwTQY2T4+k/dJRwwiIAHJIR Z5/sP8KJMAp+tGkfsHccRaRMhbiH2hlY7lqkV9XTk4cl7Imq9zu1O6R+3e5y+6Osgzai Dp3jdNnZ3SqUd5wUQ2AVyjMoCDPJUQqoW9WDP9bjH5YJUGCUv/RgfUhKyK5ET8KFYj8Y nfmCjUMJIfiFFms+FK6jYMIVnYaBoLwhbx/LKbwt2GkUdO7ZghjWc2oWT6xhpki+HgU7 djQ3lbUuIF5gcPwACqaL7PUV1meQ4IWMs+4OP38l1BbdxbFZ27ORuwc8hEewmirplXG/ wenw== X-Gm-Message-State: APjAAAW0isrZUUmast92Ur4hn39ilRZBBSULbV1r4d0rcX4zLT36kLii 6+EkusbTS/YX5vruKqJtFfTXbw== X-Received: by 2002:a5d:5283:: with SMTP id c3mr3506792wrv.268.1559898753629; Fri, 07 Jun 2019 02:12:33 -0700 (PDT) Received: from zen.linaroharston ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id b2sm1567719wrp.72.2019.06.07.02.12.28 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 07 Jun 2019 02:12:30 -0700 (PDT) Received: from zen.linaroharston. (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 1EC991FFAF; Fri, 7 Jun 2019 10:05:54 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Date: Fri, 7 Jun 2019 10:05:25 +0100 Message-Id: <20190607090552.12434-27-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190607090552.12434-1-alex.bennee@linaro.org> References: <20190607090552.12434-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::442 Subject: [Qemu-devel] [PULL 26/52] semihosting: split console_out into string and char versions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Riku Voipio , =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-devel@nongnu.org, "open list:ARM" , Laurent Vivier Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This is ostensibly to avoid the weirdness of len looking like it might come from a guest and sometimes being used. While we are at it fix up the error checking for the arm-linux-user implementation of the API which got flagged up by Coverity (CID 1401700). Signed-off-by: Alex Bennée -- 2.20.1 diff --git a/hw/semihosting/console.c b/hw/semihosting/console.c index 466ea6dade..4a5758972d 100644 --- a/hw/semihosting/console.c +++ b/hw/semihosting/console.c @@ -36,26 +36,24 @@ int qemu_semihosting_log_out(const char *s, int len) /* * A re-implementation of lock_user_string that we can use locally * instead of relying on softmmu-semi. Hopefully we can deprecate that - * in time. We either copy len bytes if specified or until we find a NULL. + * in time. Copy string until we find a 0 or address error. */ -static GString *copy_user_string(CPUArchState *env, target_ulong addr, int len) +static GString *copy_user_string(CPUArchState *env, target_ulong addr) { CPUState *cpu = ENV_GET_CPU(env); - GString *s = g_string_sized_new(len ? len : 128); + GString *s = g_string_sized_new(128); uint8_t c; - bool done; do { if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0) == 0) { s = g_string_append_c(s, c); - done = len ? s->len == len : c == 0; } else { qemu_log_mask(LOG_GUEST_ERROR, "%s: passed inaccessible address " TARGET_FMT_lx, __func__, addr); - done = true; + break; } - } while (!done); + } while (c!=0); return s; } @@ -68,9 +66,9 @@ static void semihosting_cb(CPUState *cs, target_ulong ret, target_ulong err) } } -int qemu_semihosting_console_out(CPUArchState *env, target_ulong addr, int len) +int qemu_semihosting_console_outs(CPUArchState *env, target_ulong addr) { - GString *s = copy_user_string(env, addr, len); + GString *s = copy_user_string(env, addr); int out = s->len; if (use_gdb_syscalls()) { @@ -82,3 +80,21 @@ int qemu_semihosting_console_out(CPUArchState *env, target_ulong addr, int len) g_string_free(s, true); return out; } + +void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr) +{ + CPUState *cpu = ENV_GET_CPU(env); + uint8_t c; + + if (cpu_memory_rw_debug(cpu, addr, &c, 1, 0) == 0) { + if (use_gdb_syscalls()) { + gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, 1); + } else { + qemu_semihosting_log_out((const char *) &c, 1); + } + } else { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: passed inaccessible address " TARGET_FMT_lx, + __func__, addr); + } +} diff --git a/include/hw/semihosting/console.h b/include/hw/semihosting/console.h index 30e66ae20a..3a4fba7590 100644 --- a/include/hw/semihosting/console.h +++ b/include/hw/semihosting/console.h @@ -10,17 +10,30 @@ #define _SEMIHOST_CONSOLE_H_ /** - * qemu_semihosting_console_out: + * qemu_semihosting_console_outs: * @env: CPUArchState - * @s: host address of guest string - * @len: length of string or 0 (string is null terminated) + * @s: host address of null terminated guest string * - * Send a guest string to the debug console. This may be the remote - * gdb session if a softmmu guest is currently being debugged. + * Send a null terminated guest string to the debug console. This may + * be the remote gdb session if a softmmu guest is currently being + * debugged. * * Returns: number of bytes written. */ -int qemu_semihosting_console_out(CPUArchState *env, target_ulong s, int len); +int qemu_semihosting_console_outs(CPUArchState *env, target_ulong s); + +/** + * qemu_semihosting_console_outc: + * @env: CPUArchState + * @s: host address of null terminated guest string + * + * Send single character from guest memory to the debug console. This + * may be the remote gdb session if a softmmu guest is currently being + * debugged. + * + * Returns: nothing + */ +void qemu_semihosting_console_outc(CPUArchState *env, target_ulong c); /** * qemu_semihosting_log_out: diff --git a/linux-user/arm/semihost.c b/linux-user/arm/semihost.c index 9554102a85..a16b525eec 100644 --- a/linux-user/arm/semihost.c +++ b/linux-user/arm/semihost.c @@ -15,10 +15,35 @@ #include "hw/semihosting/console.h" #include "qemu.h" -int qemu_semihosting_console_out(CPUArchState *env, target_ulong addr, int len) +int qemu_semihosting_console_outs(CPUArchState *env, target_ulong addr) { - void *s = lock_user_string(addr); - len = write(STDERR_FILENO, s, len ? len : strlen(s)); + int len = target_strlen(addr); + void *s; + if (len < 0){ + qemu_log_mask(LOG_GUEST_ERROR, + "%s: passed inaccessible address " TARGET_FMT_lx, + __func__, addr); + return 0; + } + s = lock_user(VERIFY_READ, addr, (long)(len + 1), 1); + g_assert(s); /* target_strlen has already verified this will work */ + len = write(STDERR_FILENO, s, len); unlock_user(s, addr, 0); return len; } + +void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr) +{ + char c; + + if (get_user_u8(c, addr)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: passed inaccessible address " TARGET_FMT_lx, + __func__, addr); + } else { + if (write(STDERR_FILENO, &c, 1) != 1) { + qemu_log_mask(LOG_UNIMP, "%s: unexpected write to stdout failure", + __func__); + } + } +} diff --git a/target/arm/arm-semi.c b/target/arm/arm-semi.c index 53e807ab72..8844da8da3 100644 --- a/target/arm/arm-semi.c +++ b/target/arm/arm-semi.c @@ -315,10 +315,10 @@ target_ulong do_arm_semihosting(CPUARMState *env) return set_swi_errno(ts, close(arg0)); } case TARGET_SYS_WRITEC: - qemu_semihosting_console_out(env, args, 1); + qemu_semihosting_console_outc(env, args); return 0xdeadbeef; case TARGET_SYS_WRITE0: - return qemu_semihosting_console_out(env, args, 0); + return qemu_semihosting_console_outs(env, args); case TARGET_SYS_WRITE: GET_ARG(0); GET_ARG(1);