From patchwork Fri May 10 19:10:50 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: 163945 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:142:0:0:0:0 with SMTP id j2csp2769753ilr; Fri, 10 May 2019 12:12:28 -0700 (PDT) X-Google-Smtp-Source: APXvYqzRc7h5hZpKGPJTN1V6ptjlEMCdibjA2A789sm9j2bOnPdSzUGlaBrFcAExq/RMDypDOIY3 X-Received: by 2002:a50:ae84:: with SMTP id e4mr13433461edd.33.1557515548721; Fri, 10 May 2019 12:12:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557515548; cv=none; d=google.com; s=arc-20160816; b=C+Y14ObQoUSm0CHybqhAlrEvO32C5BRiXDzqsXj9gwwAGGcGHtvj2VHpeFluAvBxqG 0wZRInwVhEDVXyCO8Q+YSr2YobbjB8QUWUZmcWbWUl+Dyb1ka2f7vg2D6gvHNkQaIfNA D+Vm5fYleWu4USJZq+DmwwDpf6rw36ex95ls/xNe4D+2RNYyw7DsQ65/NH80nJ4jI6I7 WZWdDTtoYBBBwWqwnjC3T7yj5nDlpoLm7XGGwqzPitCSJPLneeKWxjN6RQpTHOVbFqed +e51/A2KkLOPUtpjyPGtTsMhhTXqkj2JA3R/uabk6SkLnZfZcakAjuq4NWd8ps7q+IIH 2LXA== 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:message-id:date:to:from :dkim-signature; bh=d6TLWS7RtIcv4B4DpCpsrRR4kh8Z/F3NenDWVNK0XWU=; b=y3bl18/XOTAqMr+ZXiGN6Csa/Y6O4SYtjbfk73vc7cTivUwUlDJaahaI2COceAGN0E vzQK94NVcQDPy2g2pzcHnioi8ZyxS/de1xO3T6BTqohwmicF9+OE1vcscimxA0veLk6J lRl/2WaUUWKEuGQKk9xPHGV/cHbN+A0/MM7lT4cLufoR/wnEOEGKWvtDsKdtvHqmLm0G ixNUTH/TXshDPYMjCHrEDI2+KtKitwzjxbEEqSsnxycuyy0GA/GGVH3iHmTs1NhCRZaD HnSU22xE+NIV69L78qcj8sGDYMrLjmXQrWMI9iBYsoz0pqpbhpDssY8HFs1hbYGnFTh9 rdCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=HVPRj+E7; 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=fail (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 p11si3679909ejr.36.2019.05.10.12.12.28 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 10 May 2019 12:12:28 -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=fail header.i=@linaro.org header.s=google header.b=HVPRj+E7; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:48803 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hPAwV-00071i-B7 for patch@linaro.org; Fri, 10 May 2019 15:12:27 -0400 Received: from eggs.gnu.org ([209.51.188.92]:54804) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hPAvI-000667-Dk for qemu-devel@nongnu.org; Fri, 10 May 2019 15:11:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hPAvE-0002pt-QO for qemu-devel@nongnu.org; Fri, 10 May 2019 15:11:10 -0400 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:41581) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hPAvB-0002kT-6C for qemu-devel@nongnu.org; Fri, 10 May 2019 15:11:06 -0400 Received: by mail-wr1-x444.google.com with SMTP id d12so9003650wrm.8 for ; Fri, 10 May 2019 12:11:01 -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:mime-version :content-transfer-encoding; bh=d6TLWS7RtIcv4B4DpCpsrRR4kh8Z/F3NenDWVNK0XWU=; b=HVPRj+E7aXRWoEiBO16sTm2O4zv1rnbhXlpyRBiRwr2uKa7p/gtf5l5eCsRwaaMJlC K5y0eaQTQE5Q2WAcSSI38EB6g1gweYRoZyE4wVvCWk6D5+EqnMMuNqzxrJsQ/EyyMjUI MxcPq/P0abfKde/SohPiGBdHgL4kepNV8ZqoNtVA4QiL+KbHVVaPLCCvP867grMouKKF IE2SV/QD4LiifnNfeOLI79H0fNW0pfSFymDaKPJkXqJk7YTLSG8yXjxzSA+PG/z7t7Ik Gu3PQHQZrG9K4PvdtbXVd/pmfR7Y2Sc3QULYQ5LY+PM6ugc2/HMhDI8mzdbmKQ1lGcuc G6BA== 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:mime-version :content-transfer-encoding; bh=d6TLWS7RtIcv4B4DpCpsrRR4kh8Z/F3NenDWVNK0XWU=; b=IpfxayUqkfR1OyGUA6HyM0d6H8L7fAM5s7QO1CeDI8PhV+uwOtBLFGNw8vmVeYwlxR fRy2NAE5E8V4DQY3rl31tnt9zAVbJQy7yu6lIRVP85DS41enFhGYT3vWe3XSnUXnb+m2 9M6ubpc756vIw9zJk4wF+SAUbraJamkBMUp7cgPi+jS9Hl/IJEEdzTOTqgtb38biNxvq VF2ww3ls7bsLipkmuRf8aSwsEEu03zXNsWvvwcBcNHABG+lNFQaZxCjhVZziePB+tXxO Lolm2Jh2hzj75h0pFYnlaHZ7tfGB6iyq3mDSmDs/zaUCE3SIYqSSTlhasyiMbVqXHbwk Xaeg== X-Gm-Message-State: APjAAAWojUSJYZLx61Pobkz3JL4xTNwLEtB5eh/rRcQ7MuQrlk0Br+aY knYk6LhWVI5RVl4EHgaj3bYkEA== X-Received: by 2002:a5d:6249:: with SMTP id m9mr8646952wrv.255.1557515459139; Fri, 10 May 2019 12:10:59 -0700 (PDT) Received: from zen.linaroharston ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id n4sm11300720wmb.22.2019.05.10.12.10.57 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 10 May 2019 12:10:57 -0700 (PDT) Received: from zen.linaroharston. (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 35D331FF87; Fri, 10 May 2019 20:10:57 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-arm@nongnu.org Date: Fri, 10 May 2019 20:10:50 +0100 Message-Id: <20190510191051.5973-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 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::444 Subject: [Qemu-devel] [RFC PATCH] target/arm: semihosting docs, formatting and return clean-ups X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This is a clean-up of the semihosting calls after reading ver 2.0 of the specification. There are a number of small fixes that seemed too insignificant to split into smaller patches: - add reference to the ARM semihosting spec - add some additional commentary on return values - fixup block comments as per standard - audit return values, return 0xdeadbeef for corrupted values - fix up leaks from early returns with lock_user_string - return bytes not written/read instead of -1 - add LOG_UNIMP for missing functionality This is very much a Friday patch. It might be worth splitting up if coming back for a more concerted clean-up series for semihosting as the asynchronous gdb calls probably need more attention. Signed-off-by: Alex Bennée --- target/arm/arm-semi.c | 180 +++++++++++++++++++++++++----------------- 1 file changed, 109 insertions(+), 71 deletions(-) -- 2.20.1 diff --git a/target/arm/arm-semi.c b/target/arm/arm-semi.c index 4c326fdc2fb..8deaed2807c 100644 --- a/target/arm/arm-semi.c +++ b/target/arm/arm-semi.c @@ -2,6 +2,7 @@ * Arm "Angel" semihosting syscalls * * Copyright (c) 2005, 2007 CodeSourcery. + * Copyright (c) 2019 Linaro * Written by Paul Brook. * * This program is free software; you can redistribute it and/or modify @@ -15,13 +16,19 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, see . + * along with this program; if not, see + * . + * + * ARM Semihosting is documented in: + * Semihosting for AArch32 and AArch64 Release 2.0 + * https://static.docs.arm.com/100863/0200/semihosting.pdf */ #include "qemu/osdep.h" #include "cpu.h" #include "exec/semihost.h" +#include "exec/log.h" #ifdef CONFIG_USER_ONLY #include "qemu.h" @@ -241,13 +248,18 @@ static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb, put_user_u64(val, args + (n) * 8) : \ put_user_u32(val, args + (n) * 4)) +/* + * Do a semihosting call. Returns the "RETURN REGISTER" which is + * documented as corrupted for some calls. In this case we use the + * venerable 0xdeadbeef. + */ target_ulong do_arm_semihosting(CPUARMState *env) { ARMCPU *cpu = arm_env_get_cpu(env); CPUState *cs = CPU(cpu); target_ulong args; target_ulong arg0, arg1, arg2, arg3; - char * s; + char *s; int nr; uint32_t ret; uint32_t len; @@ -273,9 +285,9 @@ target_ulong do_arm_semihosting(CPUARMState *env) GET_ARG(2); s = lock_user_string(arg0); if (!s) { - /* FIXME - should this error code be -TARGET_EFAULT ? */ return (uint32_t)-1; } + /* check for invalid open mode */ if (arg1 >= 12) { unlock_user(s, arg0, 0); return (uint32_t)-1; @@ -287,7 +299,7 @@ target_ulong do_arm_semihosting(CPUARMState *env) } if (use_gdb_syscalls()) { ret = arm_gdb_syscall(cpu, arm_semi_cb, "open,%s,%x,1a4", arg0, - (int)arg2+1, gdb_open_modeflags[arg1]); + (int) arg2 + 1, gdb_open_modeflags[arg1]); } else { ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644)); } @@ -301,48 +313,51 @@ target_ulong do_arm_semihosting(CPUARMState *env) return set_swi_errno(ts, close(arg0)); } case TARGET_SYS_WRITEC: - { - char c; - - if (get_user_u8(c, args)) - /* FIXME - should this error code be -TARGET_EFAULT ? */ - return (uint32_t)-1; - /* Write to debug console. stderr is near enough. */ - if (use_gdb_syscalls()) { + { + char c; + if (!get_user_u8(c, args)) { + /* Write to debug console. stderr is near enough. */ + if (use_gdb_syscalls()) { return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,1", args); - } else { + } else { #ifdef CONFIG_SOFTMMU - Chardev *chardev = semihosting_get_chardev(); - if (chardev) { - return qemu_chr_write_all(chardev, (uint8_t *) &c, 1); - } else + Chardev *chardev = semihosting_get_chardev(); + if (chardev) { + return qemu_chr_write_all(chardev, (uint8_t *) &c, 1); + } #endif - { - return write(STDERR_FILENO, &c, 1); - } - } + return write(STDERR_FILENO, &c, 1); + } + } else { + /* return register is corrupted */ + return 0xdeadbeef; } + } case TARGET_SYS_WRITE0: - if (!(s = lock_user_string(args))) - /* FIXME - should this error code be -TARGET_EFAULT ? */ - return (uint32_t)-1; - len = strlen(s); - if (use_gdb_syscalls()) { - return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,%x", - args, len); - } else { + { + s = lock_user_string(args); + if (s) { + len = strlen(s); + if (use_gdb_syscalls()) { + arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,%x", + args, len); + } else { #ifdef CONFIG_SOFTMMU - Chardev *chardev = semihosting_get_chardev(); - if (chardev) { - ret = qemu_chr_write_all(chardev, (uint8_t *) s, len); - } else + Chardev *chardev = semihosting_get_chardev(); + if (chardev) { + qemu_chr_write_all(chardev, (uint8_t *) s, len); + } else #endif - { - ret = write(STDERR_FILENO, s, len); + { + /* result ignored */ + ret = write(STDERR_FILENO, s, len); + } } } unlock_user(s, args, 0); - return ret; + /* return register is corrupted */ + return 0xdeadbeef; + } case TARGET_SYS_WRITE: GET_ARG(0); GET_ARG(1); @@ -355,13 +370,15 @@ target_ulong do_arm_semihosting(CPUARMState *env) } else { s = lock_user(VERIFY_READ, arg1, len, 1); if (!s) { - /* FIXME - should this error code be -TARGET_EFAULT ? */ - return (uint32_t)-1; + /* Return bytes not written on error */ + return len; } ret = set_swi_errno(ts, write(arg0, s, len)); unlock_user(s, arg1, 0); - if (ret == (uint32_t)-1) - return -1; + if (ret == (uint32_t)-1) { + ret = 0; + } + /* Return bytes not written */ return len - ret; } case TARGET_SYS_READ: @@ -376,26 +393,33 @@ target_ulong do_arm_semihosting(CPUARMState *env) } else { s = lock_user(VERIFY_WRITE, arg1, len, 0); if (!s) { - /* FIXME - should this error code be -TARGET_EFAULT ? */ - return (uint32_t)-1; + /* return bytes not read */ + return len; } do { ret = set_swi_errno(ts, read(arg0, s, len)); } while (ret == -1 && errno == EINTR); unlock_user(s, arg1, len); - if (ret == (uint32_t)-1) - return -1; + if (ret == (uint32_t)-1) { + ret = 0; + } + /* Return bytes not read */ return len - ret; } case TARGET_SYS_READC: - /* XXX: Read from debug console. Not implemented. */ + qemu_log_mask(LOG_UNIMP, "%s: TARGET_SYS_READC not implemented", + __func__); return 0; case TARGET_SYS_ISTTY: GET_ARG(0); if (use_gdb_syscalls()) { return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", arg0); } else { - return isatty(arg0); + if (isatty(arg0)) { + return 1; + } else { + return (errno == EBADF ? -1 : 0); + } } case TARGET_SYS_SEEK: GET_ARG(0); @@ -405,8 +429,9 @@ target_ulong do_arm_semihosting(CPUARMState *env) arg0, arg1); } else { ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET)); - if (ret == (uint32_t)-1) - return -1; + if (ret == (uint32_t)-1) { + return -1; + } return 0; } case TARGET_SYS_FLEN: @@ -417,28 +442,30 @@ target_ulong do_arm_semihosting(CPUARMState *env) } else { struct stat buf; ret = set_swi_errno(ts, fstat(arg0, &buf)); - if (ret == (uint32_t)-1) + if (ret == (uint32_t)-1) { return -1; + } return buf.st_size; } case TARGET_SYS_TMPNAM: - /* XXX: Not implemented. */ + qemu_log_mask(LOG_UNIMP, "%s: TARGET_SYS_TMPNAM not implemented", + __func__); return -1; case TARGET_SYS_REMOVE: GET_ARG(0); GET_ARG(1); if (use_gdb_syscalls()) { ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s", - arg0, (int)arg1+1); + arg0, (int)arg1 + 1); } else { s = lock_user_string(arg0); if (!s) { - /* FIXME - should this error code be -TARGET_EFAULT ? */ - return (uint32_t)-1; + return -EIO; } - ret = set_swi_errno(ts, remove(s)); + ret = set_swi_errno(ts, remove(s)); unlock_user(s, arg0, 0); } + /* 0 on success or host-specific error code */ return ret; case TARGET_SYS_RENAME: GET_ARG(0); @@ -447,20 +474,23 @@ target_ulong do_arm_semihosting(CPUARMState *env) GET_ARG(3); if (use_gdb_syscalls()) { return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s", - arg0, (int)arg1+1, arg2, (int)arg3+1); + arg0, (int)arg1 + 1, arg2, (int)arg3 + 1); } else { char *s2; s = lock_user_string(arg0); s2 = lock_user_string(arg2); - if (!s || !s2) - /* FIXME - should this error code be -TARGET_EFAULT ? */ - ret = (uint32_t)-1; - else + if (s && s2) { ret = set_swi_errno(ts, rename(s, s2)); - if (s2) + } else { + ret = -EIO; + } + if (s2) { unlock_user(s2, arg2, 0); - if (s) + } + if (s) { unlock_user(s, arg0, 0); + } + /* 0 on success or host-specific error code */ return ret; } case TARGET_SYS_CLOCK: @@ -472,7 +502,7 @@ target_ulong do_arm_semihosting(CPUARMState *env) GET_ARG(1); if (use_gdb_syscalls()) { return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s", - arg0, (int)arg1+1); + arg0, (int)arg1 + 1); } else { s = lock_user_string(arg0); if (!s) { @@ -527,14 +557,16 @@ target_ulong do_arm_semihosting(CPUARMState *env) output_size = ts->info->arg_end - ts->info->arg_start; if (!output_size) { - /* We special-case the "empty command line" case (argc==0). - Just provide the terminating 0. */ + /* + * We special-case the "empty command line" case (argc==0). + * Just provide the terminating 0. + */ output_size = 1; } #endif if (output_size > input_size) { - /* Not enough space to store command-line arguments. */ + /* Not enough space to store command-line arguments. */ return -1; } @@ -588,8 +620,10 @@ target_ulong do_arm_semihosting(CPUARMState *env) GET_ARG(0); #ifdef CONFIG_USER_ONLY - /* Some C libraries assume the heap immediately follows .bss, so - allocate it using sbrk. */ + /* + * Some C libraries assume the heap immediately follows .bss, so + * allocate it using sbrk. + */ if (!ts->heap_limit) { abi_ulong ret; @@ -637,7 +671,8 @@ target_ulong do_arm_semihosting(CPUARMState *env) } case TARGET_SYS_EXIT: if (is_a64(env)) { - /* The A64 version of this call takes a parameter block, + /* + * The A64 version of this call takes a parameter block, * so the application-exit type can return a subcode which * is the exit status code from the application. */ @@ -650,14 +685,17 @@ target_ulong do_arm_semihosting(CPUARMState *env) ret = 1; } } else { - /* ARM specifies only Stopped_ApplicationExit as normal - * exit, everything else is considered an error */ + /* + * ARM specifies only Stopped_ApplicationExit as normal + * exit, everything else is considered an error + */ ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1; } gdb_exit(env, ret); exit(ret); case TARGET_SYS_SYNCCACHE: - /* Clean the D-cache and invalidate the I-cache for the specified + /* + * Clean the D-cache and invalidate the I-cache for the specified * virtual address range. This is a nop for us since we don't * implement caches. This is only present on A64. */