From patchwork Wed Jun 8 21:17:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 12874674 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 616F9C433EF for ; Wed, 8 Jun 2022 21:21:52 +0000 (UTC) Received: from localhost ([::1]:51000 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nz37f-0001jK-Dq for qemu-devel@archiver.kernel.org; Wed, 08 Jun 2022 17:21:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57818) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nz335-00017X-9i for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:07 -0400 Received: from mail-io1-xd2b.google.com ([2607:f8b0:4864:20::d2b]:47079) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nz333-00069G-9U for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:07 -0400 Received: by mail-io1-xd2b.google.com with SMTP id s23so20262001iog.13 for ; Wed, 08 Jun 2022 14:17:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NouXyIwW4GTIatJl2QuUmC9T72VYbmlkZ6IdIsUMHhQ=; b=Y76wZh4byU++SVWXdQiLoekQGiBjl6j5VdpKiQcwN/S2428afc3HR81NXKSxfVi016 y3/9oHVekpyuvYIjIroqhx+5qZbr7XxFZCyuxvPH6AoYUWcH2W92hAUM4rcfbgpNnaM0 yAE0QyiLJhxm0KVPZvfhGMJYygmPgBDjMCY7mpderp67l2UuqJK0FnND9BxU/1B82TCP 7EjQ2FjJhUsicOLQmQWJy6Owl+0VnWZDK4UkIeRTB7VPXXnIUnMI62CsGNamkzhyCABV KgEpCsnWz1uxC3UvyFuGgO4TMKqOqoEoAgMlaTMgwxMkZL3uBVi5lyZcDLh0geW4hwDM yF7w== 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=NouXyIwW4GTIatJl2QuUmC9T72VYbmlkZ6IdIsUMHhQ=; b=rLL6Tg70fbxKfg+/ilQMhw/NsUUQy3mjbTZoSGk+Bg1Ai5D2OOobSqmyKgNZsE6KrD BkLOnd15yj631VbAoQxZvDmO3EvoR99klqsD1ng7D7fRkSRPZjsSoQW2Hmna/LMrqRQ+ YISBJMa/DuozIQExklSmvWMo0+2bltlRJcbWkpUKi0jelCJeEFnMUCnIL2HEeHBjWDmS GNkT/PVEDxNTfpv5pPf1dv+wL2WHgPfTXwjsA2ibIrsKupm/Ghw8bKiLnF6BFz/sTsel ZBKKaOhAvFG0XX1PkqJEExDJp5HErd0Htain+Sz7ckWA2EVKsXAKxPHb69xr6gMXOG4O Unag== X-Gm-Message-State: AOAM532E6cARcx9Kteb/39WBkyaWv8CIG8VYOqDtnchqw/JJjp+4GT1R fPYvv1Sy2EAMKfBnpBeTFQq+VFjNu1FRFQ== X-Google-Smtp-Source: ABdhPJzm+BkMWgn7nnskLsEsOBGQ5yLQgchtBUjYcJqoWigIpDKraWEEIRq/CBo0Jsn2KvsS0AIanQ== X-Received: by 2002:a05:6638:12d2:b0:331:c4f6:ffda with SMTP id v18-20020a05663812d200b00331c4f6ffdamr6996829jas.173.1654723023095; Wed, 08 Jun 2022 14:17:03 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id k29-20020a02661d000000b0032ead96ee5csm8418552jac.165.2022.06.08.14.17.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 14:17:02 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: jrtc27@FreeBSD.org, def@freebsd.org, Warner Losh , arrowd@freebsd.org, Kyle Evans Subject: [PATCH v2 1/6] bsd-user/freebsd/os-syscall.c: lock_iovec Date: Wed, 8 Jun 2022 15:17:06 -0600 Message-Id: <20220608211711.56158-2-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220608211711.56158-1-imp@bsdimp.com> References: <20220608211711.56158-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::d2b; envelope-from=imp@bsdimp.com; helo=mail-io1-xd2b.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" lock_iovec will lock an I/O vec and the memory to which it refers and create a iovec in the host space that refers to it, with full error unwinding. Add helper_iovec_unlock to unlock the partially locked iovec in case there's an error. The code will be used in iovec_unlock when that is committed. Note: memory handling likely could be rewritten to use q_autofree. That will be explored in the future since what we have now works well enough. Signed-off-by: Warner Losh --- bsd-user/freebsd/os-syscall.c | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index d272478e7b0..67851937a8f 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -73,6 +73,108 @@ bool is_error(abi_long ret) return (abi_ulong)ret >= (abi_ulong)(-4096); } +/* + * Unlocks a iovec. Unlike unlock_iovec, it assumes the tvec array itself is + * already locked from target_addr. It will be unlocked as well as all the iovec + * elements. + */ +static void helper_unlock_iovec(struct target_iovec *target_vec, + abi_ulong target_addr, struct iovec *vec, + int count, int copy) +{ + for (int i = 0; i < count; i++) { + abi_ulong base = tswapal(target_vec[i].iov_base); + + if (vec[i].iov_base) { + unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0); + } + } + unlock_user(target_vec, target_addr, 0); +} + +struct iovec *lock_iovec(int type, abi_ulong target_addr, + int count, int copy) +{ + struct target_iovec *target_vec; + struct iovec *vec; + abi_ulong total_len, max_len; + int i; + int err = 0; + + if (count == 0) { + errno = 0; + return NULL; + } + if (count < 0 || count > IOV_MAX) { + errno = EINVAL; + return NULL; + } + + vec = g_try_new0(struct iovec, count); + if (vec == NULL) { + errno = ENOMEM; + return NULL; + } + + target_vec = lock_user(VERIFY_READ, target_addr, + count * sizeof(struct target_iovec), 1); + if (target_vec == NULL) { + err = EFAULT; + goto fail2; + } + + max_len = 0x7fffffff & MIN(TARGET_PAGE_MASK, PAGE_MASK); + total_len = 0; + + for (i = 0; i < count; i++) { + abi_ulong base = tswapal(target_vec[i].iov_base); + abi_long len = tswapal(target_vec[i].iov_len); + + if (len < 0) { + err = EINVAL; + goto fail; + } else if (len == 0) { + /* Zero length pointer is ignored. */ + vec[i].iov_base = 0; + } else { + vec[i].iov_base = lock_user(type, base, len, copy); + /* + * If the first buffer pointer is bad, this is a fault. But + * subsequent bad buffers will result in a partial write; this is + * realized by filling the vector with null pointers and zero + * lengths. + */ + if (!vec[i].iov_base) { + if (i == 0) { + err = EFAULT; + goto fail; + } else { + /* + * Fail all the subsequent addresses, they are already + * zero'd. + */ + goto out; + } + } + if (len > max_len - total_len) { + len = max_len - total_len; + } + } + vec[i].iov_len = len; + total_len += len; + } +out: + unlock_user(target_vec, target_addr, 0); + return vec; + +fail: + helper_unlock_iovec(target_vec, target_addr, vec, i, copy); +fail2: + g_free(vec); + errno = err; + return NULL; +} + /* * do_syscall() should always have a single exit point at the end so that * actions, such as logging of syscall results, can be performed. All errnos From patchwork Wed Jun 8 21:17:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 12874672 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 627B3C43334 for ; Wed, 8 Jun 2022 21:19:24 +0000 (UTC) Received: from localhost ([::1]:43680 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nz35H-0004on-ID for qemu-devel@archiver.kernel.org; Wed, 08 Jun 2022 17:19:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57816) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nz335-00017W-7G for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:07 -0400 Received: from mail-io1-xd2a.google.com ([2607:f8b0:4864:20::d2a]:36856) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nz333-00069Q-DO for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:06 -0400 Received: by mail-io1-xd2a.google.com with SMTP id s26so10390437ioa.3 for ; Wed, 08 Jun 2022 14:17:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NavSdPo8FT9tgL7O1y4GHz2cTUNr6cPjcE/aghh8Xno=; b=IquNK5G5FFh1OEyf/hdIdsolRlAE0fMrY2i1W8EnXHYm6u33Fm+2Y47mvXjbtvrvT8 O8JEj4O5UaQ5PjDecIM9M/uezzUi4xxjGuYQGUPhQtnXd1rM7EBnBgem1SrdFT3ilvJ3 DwZv88Fw3wZa38mo2kWGbPOCd+CyPxbQgK8DVqdYtjzhwn/ORwM0FzQW0/B9jC354ih5 xWd/HDGaPQUY+p6PJsFWkVkWJFy1m5zrSL09w9qOH8jfBko0yEd5rCJAg3dlPyfW9q0F hRfp3su+pdMez+0eCgml+ThyupbvUYKpcyUKPJsyQ128lPFYcUrArDzyNxvgPu/UeHg/ I04g== 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=NavSdPo8FT9tgL7O1y4GHz2cTUNr6cPjcE/aghh8Xno=; b=mHhH3axqkSXjSBxoL9HyzFGQIK9qi+R7Qnwvlr62Xl4/KOolAxa42Mi2YzUWudJ6vP lLsQtmT3VUQUH4uKrHdZB9x+iODRudfH1XhgmQXVvxPmZ97Zz1/w4mSQLzA3qYNZrzqv G4r1MoAs4KmvZ5eJWImzDxD31WS8rNvTiw/RJ8wjzIumvSs6QeQBDleBc426fiSc2YJR Xl/ADqAl3JyfPYkLDNeKcyrIU6zqBMGhRS5MK6ta0tdJLOE8Ld+xLO++R8CVHECOnEzD LCzpnIPC3hJRVI/5K/MLf70CMj9APy0pEbeExX9aJOprwgqHbHbkeYXTq9w1sRf6bp1w euoA== X-Gm-Message-State: AOAM532YMXjRQlPGyM6Plogtb96TMcZ5WTSMSWC6FkTPjqMqoqSvjLJE uJXQtoveyYR47xRhkXj3/NM8jeisdTlW5w== X-Google-Smtp-Source: ABdhPJxQDGfsh69rGVLxuZsfCaDqxrH+7RE/1W7zW6PSD14zbPFCKFMvyDFv0YI/9tapUuWYVI9LuQ== X-Received: by 2002:a05:6638:381c:b0:331:b4c2:1f93 with SMTP id i28-20020a056638381c00b00331b4c21f93mr8619980jav.185.1654723024085; Wed, 08 Jun 2022 14:17:04 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id k29-20020a02661d000000b0032ead96ee5csm8418552jac.165.2022.06.08.14.17.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 14:17:03 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: jrtc27@FreeBSD.org, def@freebsd.org, Warner Losh , arrowd@freebsd.org, Kyle Evans Subject: [PATCH v2 2/6] bsd-user/freebsd/os-syscall.c: unlock_iovec Date: Wed, 8 Jun 2022 15:17:07 -0600 Message-Id: <20220608211711.56158-3-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220608211711.56158-1-imp@bsdimp.com> References: <20220608211711.56158-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::d2a; envelope-from=imp@bsdimp.com; helo=mail-io1-xd2a.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Releases the references to the iovec created by lock_iovec. Signed-off-by: Warner Losh --- bsd-user/freebsd/os-syscall.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 67851937a8f..52093d479d8 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -175,6 +175,20 @@ fail2: return NULL; } +void unlock_iovec(struct iovec *vec, abi_ulong target_addr, + int count, int copy) +{ + struct target_iovec *target_vec; + + target_vec = lock_user(VERIFY_READ, target_addr, + count * sizeof(struct target_iovec), 1); + if (target_vec) { + helper_unlock_iovec(target_vec, target_addr, vec, count, copy); + } + + g_free(vec); +} + /* * do_syscall() should always have a single exit point at the end so that * actions, such as logging of syscall results, can be performed. All errnos From patchwork Wed Jun 8 21:17:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 12874670 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 80790C433EF for ; Wed, 8 Jun 2022 21:19:15 +0000 (UTC) Received: from localhost ([::1]:43556 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nz358-0004iR-5F for qemu-devel@archiver.kernel.org; Wed, 08 Jun 2022 17:19:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57862) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nz336-00017r-IC for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:08 -0400 Received: from mail-il1-x136.google.com ([2607:f8b0:4864:20::136]:42682) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nz334-00069V-Ao for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:08 -0400 Received: by mail-il1-x136.google.com with SMTP id p1so17494176ilj.9 for ; Wed, 08 Jun 2022 14:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FhYZZ966K8Fto0GgZfW1CgR/cEkdwN4ztsC31H6e9R8=; b=64plnoav2psOz1ZS0NCXbeCes2Sl7g8Iz3X1yXnfpa3KeR04EZ6WMkEPcvTfql+bhX aXWnesdDVFmIQmgR6+LBFZDGVcM8O6ZO8+ZEazXWgNIIoaxz2Y+Kq4BJhjJs/qUB52qJ qaQssd3UyN+DeIu2fUzvbrpfcWLrFJU5fngMLdpf6QJEzyBGpl3G+XO41VqZOAU38Kwy MSJui6hXKt9X6fhdmpVnxh7bM3GCMFr/QWkgz88bV3yE3n9JCNHz0qycKTRTWquKPd2m caAEtPYzeCLeyZF0mBAIR4ZQsAFUGWnekBVLfioB+IYZVMNRyS3S+6M2fI2I0qlMb6p7 QzNQ== 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=FhYZZ966K8Fto0GgZfW1CgR/cEkdwN4ztsC31H6e9R8=; b=uLOULMZYUFGMFKRd7Pc7P3JHII6TespkyV3lTNqRC1L+S5ZY3gPWtLXJF7glJJi/+2 ytxhVMcNY1RmLsKPhbOsGTuKuSBvl6iB2kwl+39ij9OeQETP6oAtUuV4xQixa0QQFtVX PglyPLoWVUXisV+01CVxcD3Y40jP94F2wpb6TNzkOzomtuyzm4PawMJE4zhUheBI4pwJ CPVl1R+micYvZZjlinoTWHR7bbKhHrangY2QQUikydcUPa+1HF/szC3+OhZFFLch5SJA +VJFzWi8Mv1QJAlQQpBNLtD1446urOFy4Gb+rv9nFVvPne/gfevEv3IkRhaUUR63Ck2E DASQ== X-Gm-Message-State: AOAM532FWV8HYwkOXcJ3z2dtoBSa4KX0rGFwWQ4XIN3Tru30Qb1LqWfo h7pdY3EMBSIjY3APB9MvsN4MnP8j54iHrQ== X-Google-Smtp-Source: ABdhPJw49mXNqhTLNsQzCc2sCPrUzJYaQRKqdSvHQbV1t2IHzhHSHRjdHzIqp+OPku6UDYq2tISlew== X-Received: by 2002:a05:6e02:1c22:b0:2d1:abab:8806 with SMTP id m2-20020a056e021c2200b002d1abab8806mr19390982ilh.300.1654723024930; Wed, 08 Jun 2022 14:17:04 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id k29-20020a02661d000000b0032ead96ee5csm8418552jac.165.2022.06.08.14.17.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 14:17:04 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: jrtc27@FreeBSD.org, def@freebsd.org, Warner Losh , arrowd@freebsd.org, Kyle Evans Subject: [PATCH v2 3/6] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate Date: Wed, 8 Jun 2022 15:17:08 -0600 Message-Id: <20220608211711.56158-4-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220608211711.56158-1-imp@bsdimp.com> References: <20220608211711.56158-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::136; envelope-from=imp@bsdimp.com; helo=mail-il1-x136.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Add in the tracing and this system call not implemented boilerplate. Do this by moving the guts of do_freebsd_syscall to freebsd_syscall. Put the tracing in the wrapper function. Since freebsd_syscall is a singleton static function, it will almost certainly be inlined. Fix comments that referred to do_syscall since that was renamed some tie ago. Signed-off-by: Warner Losh --- bsd-user/freebsd/os-syscall.c | 44 +++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 52093d479d8..1c4c9983f1a 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -190,16 +190,52 @@ void unlock_iovec(struct iovec *vec, abi_ulong target_addr, } /* - * do_syscall() should always have a single exit point at the end so that - * actions, such as logging of syscall results, can be performed. All errnos - * that do_syscall() returns must be -TARGET_. + * All errnos that freebsd_syscall() returns must be -TARGET_. + */ +static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, + abi_long arg2, abi_long arg3, abi_long arg4, + abi_long arg5, abi_long arg6, abi_long arg7, + abi_long arg8) +{ + abi_long ret; + + switch (num) { + default: + qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); + ret = -TARGET_ENOSYS; + break; + } + + return ret; +} + +/* + * do_freebsd_syscall() should always have a single exit point at the end so + * that actions, such as logging of syscall results, can be performed. This + * as a wrapper around freebsd_syscall() so that actually happens. Since + * that is a singleton, modern compilers will inline it anyway... */ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6, abi_long arg7, abi_long arg8) { - return 0; + CPUState *cpu = env_cpu(cpu_env); + int ret; + + trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); + if (do_strace) { + print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6); + } + + ret = freebsd_syscall(cpu_env, num, arg1, arg2, arg3, arg4, arg5, arg6, + arg7, arg8); + if (do_strace) { + print_freebsd_syscall_ret(num, ret); + } + trace_guest_user_syscall_ret(cpu, num, ret); + + return ret; } void syscall_init(void) From patchwork Wed Jun 8 21:17:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 12874675 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 46BB5C43334 for ; Wed, 8 Jun 2022 21:21:52 +0000 (UTC) Received: from localhost ([::1]:50954 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nz37f-0001hL-B4 for qemu-devel@archiver.kernel.org; Wed, 08 Jun 2022 17:21:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57866) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nz336-00018O-Pu for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:10 -0400 Received: from mail-io1-xd29.google.com ([2607:f8b0:4864:20::d29]:37402) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nz335-00069a-76 for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:08 -0400 Received: by mail-io1-xd29.google.com with SMTP id n11so390791iod.4 for ; Wed, 08 Jun 2022 14:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ymFzIaVTUtQ9q6k+TDbLX45RQahUqiFZ+XgERpQYRk8=; b=UWdsI06Bsz0KPxs51pWDJoJtzAoiD0dqE2gScAmn5ftKv4lxCttzL3bOdt9cehcYGP aXRUBnhfd1uNJL6mdu9e+ngplZdZP2HQm5btBGNgfBNlOcTIKOpTqzDLYNRO2jvQE7Za MjvnGQ2fAtbET7JYEqLME8lOCkQO5K1vKBVTVqqPtDY3B0654dZa+j/RkPjT0VPjX5mn Cil+6wKLnpi3Lq3HOwZBJ/w2hPz3Df3OemIwE/d6vCo9BK8wy1el2ozVVDTCRzJj4UOP pxDFu6Ay7BTPS5ibpwhvJvi2BorgWnFMGBbnbL7/wXDyeoPx+0EC4gWxk5dFR4Iz+72W 5qAw== 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=ymFzIaVTUtQ9q6k+TDbLX45RQahUqiFZ+XgERpQYRk8=; b=XvfrcrwgaTtqcfY6+htCuv++a720BqeJZeFhfLANhJ6hz46b3ySuH6DuSGpC3MnLJr lYDczc4ZK3+1pWgBwlTOaM1RkCNEMd0GWMC1lL3LYQoF8PQOOvlstcHLOgysa52Oy+Nz xF8K5hyBvKxRDgb385eNkTlLde7kA966URBatgtQWALJMIjY/XKF+yv5QVUfz0MILuUr oJSKf+T8MkTP4O2l3I5fWwsyTc9k6Gz4sBngBAdjxcRii0lT5m+smVCV+dUc1bHSbqqm FDP1RvqpqQd/cUHI8xl195Iz6+LCgbT2VELehSw7Dycvh2EOElD/KASqmKewHXgmidsv rkFg== X-Gm-Message-State: AOAM530qUi8YbSfMle0IVHn3NCTaEJbQCgrkfYFGlN7cVJ+utNNioJBJ icHX6713B4Pkplo580o+vqV0xUfjVj/+XQ== X-Google-Smtp-Source: ABdhPJw0V6DxfEhpnO9Ra9ASkR3mwAFg0JyiGdG5PZzzF25IXdLrXLvsSIFAKcCs4aU+01rxYgo2YQ== X-Received: by 2002:a05:6638:1411:b0:331:ac08:5ae3 with SMTP id k17-20020a056638141100b00331ac085ae3mr9806955jad.162.1654723025911; Wed, 08 Jun 2022 14:17:05 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id k29-20020a02661d000000b0032ead96ee5csm8418552jac.165.2022.06.08.14.17.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 14:17:05 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: jrtc27@FreeBSD.org, def@freebsd.org, Warner Losh , arrowd@freebsd.org, Kyle Evans , Stacey Son , Kyle Evans Subject: [PATCH v2 4/6] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv Date: Wed, 8 Jun 2022 15:17:09 -0600 Message-Id: <20220608211711.56158-5-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220608211711.56158-1-imp@bsdimp.com> References: <20220608211711.56158-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::d29; envelope-from=imp@bsdimp.com; helo=mail-io1-xd29.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Implement do_bsd_{read,pread,readv,preadv}. Connect them to the system call table. Signed-off-by: Stacey Son Signed-off-by: Kyle Evans Signed-off-by: Warner Losh --- bsd-user/bsd-file.h | 79 +++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 28 +++++++++++++ 2 files changed, 107 insertions(+) diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h index a6bff3b8c26..839f8c5c55a 100644 --- a/bsd-user/bsd-file.h +++ b/bsd-user/bsd-file.h @@ -27,4 +27,83 @@ extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count, extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count, int copy); +ssize_t safe_read(int fd, void *buf, size_t nbytes); +ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset); +ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt); +ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset); + +/* read(2) */ +static abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3) +{ + abi_long ret; + void *p; + + p = lock_user(VERIFY_WRITE, arg2, arg3, 0); + if (p == NULL) { + return -TARGET_EFAULT; + } + ret = get_errno(safe_read(arg1, p, arg3)); + unlock_user(p, arg2, ret); + + return ret; +} + +/* pread(2) */ +static abi_long do_bsd_pread(void *cpu_env, abi_long arg1, + abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6) +{ + abi_long ret; + void *p; + + p = lock_user(VERIFY_WRITE, arg2, arg3, 0); + if (p == NULL) { + return -TARGET_EFAULT; + } + if (regpairs_aligned(cpu_env) != 0) { + arg4 = arg5; + arg5 = arg6; + } + ret = get_errno(safe_pread(arg1, p, arg3, target_arg64(arg4, arg5))); + unlock_user(p, arg2, ret); + + return ret; +} + +/* readv(2) */ +static abi_long do_bsd_readv(abi_long arg1, abi_long arg2, abi_long arg3) +{ + abi_long ret; + struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0); + + if (vec != NULL) { + ret = get_errno(safe_readv(arg1, vec, arg3)); + unlock_iovec(vec, arg2, arg3, 1); + } else { + ret = -host_to_target_errno(errno); + } + + return ret; +} + +/* preadv(2) */ +static abi_long do_bsd_preadv(void *cpu_env, abi_long arg1, + abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6) +{ + abi_long ret; + struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 1); + + if (vec != NULL) { + if (regpairs_aligned(cpu_env) != 0) { + arg4 = arg5; + arg5 = arg6; + } + ret = get_errno(safe_preadv(arg1, vec, arg3, target_arg64(arg4, arg5))); + unlock_iovec(vec, arg2, arg3, 0); + } else { + ret = -host_to_target_errno(errno); + } + + return ret; +} + #endif /* BSD_FILE_H */ diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 1c4c9983f1a..8ca92f29857 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -42,6 +42,14 @@ #include "bsd-file.h" +/* I/O */ +safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes); +safe_syscall4(ssize_t, pread, int, fd, void *, buf, size_t, nbytes, off_t, + offset); +safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt); +safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt, + off_t, offset); + void target_set_brk(abi_ulong new_brk) { } @@ -200,6 +208,26 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, abi_long ret; switch (num) { + + /* + * File system calls. + */ + case TARGET_FREEBSD_NR_read: /* read(2) */ + ret = do_bsd_read(arg1, arg2, arg3); + break; + + case TARGET_FREEBSD_NR_pread: /* pread(2) */ + ret = do_bsd_pread(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); + break; + + case TARGET_FREEBSD_NR_readv: /* readv(2) */ + ret = do_bsd_readv(arg1, arg2, arg3); + break; + + case TARGET_FREEBSD_NR_preadv: /* preadv(2) */ + ret = do_bsd_preadv(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); + break; + default: qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num); ret = -TARGET_ENOSYS; From patchwork Wed Jun 8 21:17:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 12874676 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D9674C43334 for ; Wed, 8 Jun 2022 21:21:59 +0000 (UTC) Received: from localhost ([::1]:51260 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nz37n-0001uj-1H for qemu-devel@archiver.kernel.org; Wed, 08 Jun 2022 17:21:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57886) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nz337-00018c-Rk for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:10 -0400 Received: from mail-io1-xd33.google.com ([2607:f8b0:4864:20::d33]:41679) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nz336-00069r-1e for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:09 -0400 Received: by mail-io1-xd33.google.com with SMTP id q76so15053969iod.8 for ; Wed, 08 Jun 2022 14:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WxIt8MZdWae+geLCxbzvUsygKk++by+kW7xZVirZzoE=; b=7+LDuxU1qphaZB41wzzvhzWcVwn9q4G48uHBJa6mL+14YCwxiYI+bTLHxgkqhNkAJS rnyFcxe3WpjzK662HEFqw3zgF0WxxIGkOvjFwca25RmUpYEyFRLu1T9ZEXOsFkF+aDe7 R5Jw4UHRS4BVjtjJxUv6/nqGSJaVcFmM7WlDmPckERU1joxUVD9VYZ45NwICuP8VxiJz M2Rg7XGzfmwzpdTiq/nmEszauM/Z31qh5lmpZ0bH+h1GUwvptJxbZ7kqw7t1C8FFk3st kXUlySdHURRGzei3pbdLhF8SVvLSDZVNxiBrnlDSSMbdXtgjS1kAvCxrAXRcFF+lvRuS WqJA== 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=WxIt8MZdWae+geLCxbzvUsygKk++by+kW7xZVirZzoE=; b=eeHqNfJzaS7lZyxCx1rVqHRr8Pv4dUF2Kzk/yo1YpxUgzCLvvaCTsFEgaP9FNP7YQm r5zKpKGbU9S7JApCSJbephaZTx9StX8ke6iZhXSA63qNaq4Knd4z8dE621tOM/KXYrF4 EgZBVNr65FmcosI6eGCSu18NUI4BeHNR/WD4AWkfGD2gVblKqvKtSMC4x0KRffzQQ1yx IkGO75h9HkmMM4EQgvbizMuqr18Q+sT+VWyZaG1sWUT8SeThW2cpGGBQpM8Fk+7K0CZC l5eIrbLzY8J/MMAlLcKBTCVXVAmCVxzX9zo8O0LqY53W5vUFMVBRWBtC7J2zeDm+IIXj dcxA== X-Gm-Message-State: AOAM531ee1jmLORboKQrVxTnMK7KUTmyV7bDhqB9FnLYqH4ZsQr4Pffa 4+Tyhk/bYTDCn5miRpuOeLCNM1Cl17S5xg== X-Google-Smtp-Source: ABdhPJyBlH5TuGMnwP3ALb+8aSOITCsQa1CSWG4+VBhJLV6UNNuXC4OrS1TnvUtKK4010okE9D8XhQ== X-Received: by 2002:a6b:8f90:0:b0:669:3ad9:9bd with SMTP id r138-20020a6b8f90000000b006693ad909bdmr11429970iod.121.1654723026704; Wed, 08 Jun 2022 14:17:06 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id k29-20020a02661d000000b0032ead96ee5csm8418552jac.165.2022.06.08.14.17.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 14:17:06 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: jrtc27@FreeBSD.org, def@freebsd.org, Warner Losh , arrowd@freebsd.org, Kyle Evans , Stacey Son , Kyle Evans Subject: [PATCH v2 5/6] bsd-user/bsd-file.h: Meat of the write system calls Date: Wed, 8 Jun 2022 15:17:10 -0600 Message-Id: <20220608211711.56158-6-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220608211711.56158-1-imp@bsdimp.com> References: <20220608211711.56158-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::d33; envelope-from=imp@bsdimp.com; helo=mail-io1-xd33.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Implement write, writev, pwrite and pwritev and connect them to the system call dispatch routine. Signed-off-by: Stacey Son Signed-off-by: Kyle Evans Signed-off-by: Warner Losh --- bsd-user/bsd-file.h | 84 +++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 22 +++++++++ 2 files changed, 106 insertions(+) diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h index 839f8c5c55a..e9e2c85eb67 100644 --- a/bsd-user/bsd-file.h +++ b/bsd-user/bsd-file.h @@ -32,6 +32,11 @@ ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset); ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt); ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset); +ssize_t safe_write(int fd, void *buf, size_t nbytes); +ssize_t safe_pwrite(int fd, void *buf, size_t nbytes, off_t offset); +ssize_t safe_writev(int fd, const struct iovec *iov, int iovcnt); +ssize_t safe_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset); + /* read(2) */ static abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3) { @@ -106,4 +111,83 @@ static abi_long do_bsd_preadv(void *cpu_env, abi_long arg1, return ret; } +/* write(2) */ +static abi_long do_bsd_write(abi_long arg1, abi_long arg2, abi_long arg3) +{ + abi_long nbytes, ret; + void *p; + + /* nbytes < 0 implies that it was larger than SIZE_MAX. */ + nbytes = arg3; + if (nbytes < 0) { + return -TARGET_EINVAL; + } + p = lock_user(VERIFY_READ, arg2, nbytes, 1); + if (p == NULL) { + return -TARGET_EFAULT; + } + ret = get_errno(safe_write(arg1, p, arg3)); + unlock_user(p, arg2, 0); + + return ret; +} + +/* pwrite(2) */ +static abi_long do_bsd_pwrite(void *cpu_env, abi_long arg1, + abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6) +{ + abi_long ret; + void *p; + + p = lock_user(VERIFY_READ, arg2, arg3, 1); + if (p == NULL) { + return -TARGET_EFAULT; + } + if (regpairs_aligned(cpu_env) != 0) { + arg4 = arg5; + arg5 = arg6; + } + ret = get_errno(safe_pwrite(arg1, p, arg3, target_arg64(arg4, arg5))); + unlock_user(p, arg2, 0); + + return ret; +} + +/* writev(2) */ +static abi_long do_bsd_writev(abi_long arg1, abi_long arg2, abi_long arg3) +{ + abi_long ret; + struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); + + if (vec != NULL) { + ret = get_errno(safe_writev(arg1, vec, arg3)); + unlock_iovec(vec, arg2, arg3, 0); + } else { + ret = -host_to_target_errno(errno); + } + + return ret; +} + +/* pwritev(2) */ +static abi_long do_bsd_pwritev(void *cpu_env, abi_long arg1, + abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6) +{ + abi_long ret; + struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1); + + if (vec != NULL) { + if (regpairs_aligned(cpu_env) != 0) { + arg4 = arg5; + arg5 = arg6; + } + ret = get_errno(safe_pwritev(arg1, vec, arg3, target_arg64(arg4, arg5))); + unlock_iovec(vec, arg2, arg3, 0); + } else { + ret = -host_to_target_errno(errno); + } + + return ret; +} + #endif /* BSD_FILE_H */ diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 8ca92f29857..3a33d54fa4d 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -50,6 +50,13 @@ safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt); safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt, off_t, offset); +safe_syscall3(ssize_t, write, int, fd, void *, buf, size_t, nbytes); +safe_syscall4(ssize_t, pwrite, int, fd, void *, buf, size_t, nbytes, off_t, + offset); +safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt); +safe_syscall4(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt, + off_t, offset); + void target_set_brk(abi_ulong new_brk) { } @@ -226,6 +233,21 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, case TARGET_FREEBSD_NR_preadv: /* preadv(2) */ ret = do_bsd_preadv(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); + + case TARGET_FREEBSD_NR_write: /* write(2) */ + ret = do_bsd_write(arg1, arg2, arg3); + break; + + case TARGET_FREEBSD_NR_pwrite: /* pwrite(2) */ + ret = do_bsd_pwrite(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); + break; + + case TARGET_FREEBSD_NR_writev: /* writev(2) */ + ret = do_bsd_writev(arg1, arg2, arg3); + break; + + case TARGET_FREEBSD_NR_pwritev: /* pwritev(2) */ + ret = do_bsd_pwritev(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6); break; default: From patchwork Wed Jun 8 21:17:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 12874673 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C8E14C43334 for ; Wed, 8 Jun 2022 21:19:33 +0000 (UTC) Received: from localhost ([::1]:43832 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nz35Q-0004xJ-Pd for qemu-devel@archiver.kernel.org; Wed, 08 Jun 2022 17:19:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57888) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nz338-00018h-Eb for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:10 -0400 Received: from mail-io1-xd2c.google.com ([2607:f8b0:4864:20::d2c]:45784) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nz336-0006A1-R7 for qemu-devel@nongnu.org; Wed, 08 Jun 2022 17:17:10 -0400 Received: by mail-io1-xd2c.google.com with SMTP id 19so946298iou.12 for ; Wed, 08 Jun 2022 14:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SGQIfvqGKRBhKV4r5du5kcOf3P++/TK2DI7dqWk8+6U=; b=6Zgr3yCJjLKpsk6F+vgIB5mYX7ctSu36aUOMSAka5j3D8qiO6uDw2f5a0ytRIpkE/K 5BQdY9QhhDJ4ujczzR2LfuYUKsouvfc4r/rntiFpLBHoPDWDappYFxbErMCS1vOjvJ/X 0lymHt5wM6efaYGL8lmeytxex4VI0Cfk3nZtQ2iQcuvsjBuUqVy1smd+s6qW6j0FLDGG KPonD/jgcC/kXFlmfGGmlOHZ4kKldaAML4L0RqCJkoTYyKQtUaKAItNiK5kPHN48nN6C voJnOrRdfxQj7C0AGaK2jfHPTfl6AB+cU5ZVoxWrlNYr3wX6WRqYhaQbKdh9vKbLZo1z i6dA== 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=SGQIfvqGKRBhKV4r5du5kcOf3P++/TK2DI7dqWk8+6U=; b=j/I9M0GtYyKRxvKBL5NAsZFrzelNtMugUEvFAZWl2ta5eB4EuWc1Gj/CyxSkXoMCjB fMys4INIvj+6qLUmVBT8k0SwToOz+4k+azr3E9iwfCTrKfxNYhHJ0UqZYiyX2CjPYFnh x336jDvkCOdZHq7GuCs2/vPP6MxiVSxfbaXNxMi/Ne9qT1QvmpCITnvTo2hnicp5kKHw QV3/eOmKJO8ZgyQcPcPTxuC1NTBN9SCEHOVUFXCD6XG9O8NkSw+YrS8iFVRC5+nwa4/P NrScpSAKgmc4Aj28h8MwqtbeMGDV+nai1o66hHi6xg1Y98NPDS4OcBhOM3xpFlpX2mSC yeCg== X-Gm-Message-State: AOAM532zTkEBx8L3GIm+fzMOxTQWf07+j6Sf7pqig9jTGBlpQsxIXwDA Z5neIqMEbblGdMYmdWNDpiZT9dd66RyLiA== X-Google-Smtp-Source: ABdhPJyBmnVzYWxQjAafVjhZWHFq40hbfuuxrAbebFQ8Hy/xHkhqjPPlrGtcVvv7q1UeuD9urtntXw== X-Received: by 2002:a05:6602:1552:b0:669:5870:884d with SMTP id h18-20020a056602155200b006695870884dmr8186950iow.134.1654723027584; Wed, 08 Jun 2022 14:17:07 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id k29-20020a02661d000000b0032ead96ee5csm8418552jac.165.2022.06.08.14.17.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 14:17:07 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: jrtc27@FreeBSD.org, def@freebsd.org, Warner Losh , arrowd@freebsd.org, Kyle Evans , Stacey Son , Kyle Evans , Richard Henderson Subject: [PATCH v2 6/6] bsd-user/freebsd/os-syscall.c: Implement exit Date: Wed, 8 Jun 2022 15:17:11 -0600 Message-Id: <20220608211711.56158-7-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220608211711.56158-1-imp@bsdimp.com> References: <20220608211711.56158-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::d2c; envelope-from=imp@bsdimp.com; helo=mail-io1-xd2c.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Implement the exit system call. Bring in bsd-proc.h to contain all the process system call implementation and helper routines. Signed-off-by: Stacey Son Signed-off-by: Warner Losh Reviewed-by: Kyle Evans Reviewed-by: Richard Henderson --- bsd-user/bsd-proc.h | 43 +++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 7 ++++++ 2 files changed, 50 insertions(+) create mode 100644 bsd-user/bsd-proc.h diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h new file mode 100644 index 00000000000..8f0b6990d14 --- /dev/null +++ b/bsd-user/bsd-proc.h @@ -0,0 +1,43 @@ +/* + * process related system call shims and definitions + * + * Copyright (c) 2013-2014 Stacey D. Son + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 . + */ + +#ifndef BSD_PROC_H_ +#define BSD_PROC_H_ + +#include +#include +#include +#include +#include + +/* exit(2) */ +static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1) +{ +#ifdef TARGET_GPROF + _mcleanup(); +#endif + gdb_exit(arg1); + qemu_plugin_user_exit(); + /* XXX: should free thread stack and CPU env here */ + _exit(arg1); + + return 0; +} + +#endif /* !BSD_PROC_H_ */ diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 3a33d54fa4d..71aa0d38e03 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -41,6 +41,7 @@ #include "user/syscall-trace.h" #include "bsd-file.h" +#include "bsd-proc.h" /* I/O */ safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes); @@ -215,6 +216,12 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, abi_long ret; switch (num) { + /* + * process system calls + */ + case TARGET_FREEBSD_NR_exit: /* exit(2) */ + ret = do_bsd_exit(cpu_env, arg1); + break; /* * File system calls.