From patchwork Tue Jun 7 20:14:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 1640079 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bsdimp-com.20210112.gappssmtp.com header.i=@bsdimp-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=MteJizlR; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) 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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4LHhSy6q9pz9sFx for ; Wed, 8 Jun 2022 06:16:18 +1000 (AEST) Received: from localhost ([::1]:43658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nyfcc-00063d-Ho for incoming@patchwork.ozlabs.org; Tue, 07 Jun 2022 16:16:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57550) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nyfb3-000629-KK for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:37 -0400 Received: from mail-il1-x12f.google.com ([2607:f8b0:4864:20::12f]:43784) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nyfb1-0004Mf-69 for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:37 -0400 Received: by mail-il1-x12f.google.com with SMTP id h7so7488882ila.10 for ; Tue, 07 Jun 2022 13:14:34 -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=e8kQ9fst/2AyCf9uYc1Ho1DFIpPsnSwh6RIMuhRg9SA=; b=MteJizlRJT+Z1gwJyqTAo/WoAYDmlqrZx7gPHYxNHOdn69VZYRTN8L+GYDdQA3XLbu RzCx4Mi999Z40KlwFvE9vb2FDmkelfRg+t8CsPRASMJ8sAs2GXxnsXryVGjSJhAa0tpR fkutCZHipJrysqauCwtGXepkv/GYQm7ia//4bPy5xME4XRWlC3jmW2fZF8N9HVwvmYdd GCulivYhnhx8oCFaPFnthhfVovCazyqjFBMyTbrWBSfeSbnoTA1M4UvoRzMGwUAZpAa1 sXQqnS5JmkwKVluLN25xdICNRZxZ+Cd1ogbqAgt5fmtogFhaiClXZmRS6aRDCqg3Wood TpUw== 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=e8kQ9fst/2AyCf9uYc1Ho1DFIpPsnSwh6RIMuhRg9SA=; b=rh168xGl1ILAtL+RUZkAw1+XDSz2ubk0VgllzqJRVxlK/bb6Qz8zfP5pGuzsU7XJTZ m7PUept3v+e5XcR96uU9hc3jCQh+XtAUfD/C7oAaeS5hq2eS6olYpTb5DtWaU2AX+DZb wq67SFrserJNhtHJtOAf79Ih11P+r4iHqpqGndgjakWVE75K7JHXcxWstXKOe1oPdlxL nxR3VzNdd/AV7Pr4wMvRxHZQ/ICYgnWBnMeg6sn9t3xa4K5WqcEho8T4TjscnH9iIOTa w7mZfrmP6qqgFt7YMeDMuCrvUVXGYlvEeqpRaVeQnhDMixobYWIMrPxZtB8dXSUAAlg1 wzlQ== X-Gm-Message-State: AOAM532rCrudTSftEjBdVkWAznNgpGhSbu4Y+BlpkkJ57Ugy6NQO+0ID 0fu2zit9TK0Jf33E+g7NS42HokjFxnJi/w== X-Google-Smtp-Source: ABdhPJwpYrtnngSlIUk1CsY7bLUjNfZhYI+eH2ULwP2BS7H4OsakUOAFbKr77Ae7urxGipd4ahAgRw== X-Received: by 2002:a05:6e02:1e0b:b0:2d1:b5e8:38a0 with SMTP id g11-20020a056e021e0b00b002d1b5e838a0mr17848148ila.296.1654632873605; Tue, 07 Jun 2022 13:14:33 -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 cp5-20020a056638480500b0032ea3ba170dsm7053024jab.86.2022.06.07.13.14.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 13:14:33 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: arrowd@freebsd.org, def@freebsd.org, jrtc27@FreeBSD.org, Warner Losh , Kyle Evans Subject: [PATCH 1/6] bsd-user/freebsd/os-syscall.c: lock_iovec Date: Tue, 7 Jun 2022 14:14:35 -0600 Message-Id: <20220607201440.41464-2-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220607201440.41464-1-imp@bsdimp.com> References: <20220607201440.41464-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::12f; envelope-from=imp@bsdimp.com; helo=mail-il1-x12f.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+incoming=patchwork.ozlabs.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 | 113 ++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index d272478e7b0..c41ef0eda40 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -73,6 +73,119 @@ 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); + abi_long len = tswapal(target_vec[i].iov_len); + if (len < 0) { + /* + * Can't really happen: we'll fail to lock if any elements have a + * length < 0. Better to fail-safe though. + */ + break; + } + 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; + bool bad_address = false; + + if (count == 0) { + errno = 0; + return NULL; + } + if (count < 0 || count > IOV_MAX) { + errno = EINVAL; + return NULL; + } + + vec = g_try_new(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; + } + + /* + * ??? If host page size > target page size, this will result in a value + * larger than what we can actually support. + * ??? Should we just assert something for new 16k page size on aarch64? + */ + max_len = 0x7fffffff & TARGET_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 || bad_address) { + /* Zero length pointer is ignored. */ + len = 0; + 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 { + bad_address = true; + len = 0; + } + } + if (len > max_len - total_len) { + len = max_len - total_len; + } + } + vec[i].iov_len = len; + total_len += len; + } + + 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 Tue Jun 7 20:14:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 1640081 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bsdimp-com.20210112.gappssmtp.com header.i=@bsdimp-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=Rwk0ujVR; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) 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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4LHhSy6nKMz9sFr for ; Wed, 8 Jun 2022 06:16:18 +1000 (AEST) Received: from localhost ([::1]:43648 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nyfcb-00062u-Po for incoming@patchwork.ozlabs.org; Tue, 07 Jun 2022 16:16:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57548) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nyfb3-000628-JD for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:37 -0400 Received: from mail-il1-x12d.google.com ([2607:f8b0:4864:20::12d]:45851) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nyfb1-0004Mx-Rz for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:37 -0400 Received: by mail-il1-x12d.google.com with SMTP id a15so15047707ilq.12 for ; Tue, 07 Jun 2022 13:14:35 -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=TFDLahUFH1FoRiJBTiATu0Ur7LkkxBUFWyrxyo8x3g0=; b=Rwk0ujVRI91rfza2erBM4XXiYBETULKl1sTz8EAUwHiZpv9mrqSpamDZrxcKFvzDcT QJEXUw7xYkcIvm8F7RuYCvAjkQfSHzYLvnjinRfrXrezlrl2bsROZmT0jCEMoooV0uIz p2euX/PsfN9TtexGQ/dBFhNMeUTf47numunmpckfAAyM7Q+xw56ylqK82gZYp2m7vJa/ 3DG5YtkUCtl6FWFfkNEA6oAquei4Hm6d+Y/FIEtvhxinNsgolWaRMY0S7QNO6BAMJ5l2 QmHuu+tBZDt3f3cJPQVB03Ikfg/LLdU+vbyfpb5awiEPpwaZUzTfRUefP86KrUo8QUCJ j1iw== 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=TFDLahUFH1FoRiJBTiATu0Ur7LkkxBUFWyrxyo8x3g0=; b=06vTRt7DPJUTUSwtcXAahMIN1UGMV6ItoreFnvvD7gbKF2p3qV71BW5fI4to9T8kqC RYrWy7rANkVg2sjqgNpertlanDjljRGk3RxKj/lnBynceaacgpNaX6XP61f+pMVkTDzk 0UCUNMVOxtQNFPT2xB/N9cgtK7F3haUm38Te2AS1fpJeh+fZ7zzkCWJY4CucKMZB6Es1 DSEj8j+XxdwaxL07+QueUB5hTfFOhFjrpb5CKL4u13mfCYuNKsSiYC+6YW0NtN4AQjfW kF6D8Yf268FXTaa0FufuLhpt8l5FPJ9kxWVvSYZDSI5R+taoYpBbLdKCYCptWRAPp5Ie vrew== X-Gm-Message-State: AOAM531BZAwmbS7QYRIUOgzCpg8KYN/KC2tBv1YXiTmMIK/MIqwvhzKR wtdR/nzSO4uQ5OXIu7ZR8RfBKgCDg8mOyQ== X-Google-Smtp-Source: ABdhPJyBTO6DE/2PidUqjS80xlJR/LCPBbaWfsmMWjOq2nmQ7SE0zl3zE2k0yjxxpUDHFLzavw1kFw== X-Received: by 2002:a05:6e02:b26:b0:2d1:ef19:8f90 with SMTP id e6-20020a056e020b2600b002d1ef198f90mr17837704ilu.180.1654632874568; Tue, 07 Jun 2022 13:14:34 -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 cp5-20020a056638480500b0032ea3ba170dsm7053024jab.86.2022.06.07.13.14.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 13:14:34 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: arrowd@freebsd.org, def@freebsd.org, jrtc27@FreeBSD.org, Warner Losh , Kyle Evans Subject: [PATCH 2/6] bsd-user/freebsd/os-syscall.c: unlock_iovec Date: Tue, 7 Jun 2022 14:14:36 -0600 Message-Id: <20220607201440.41464-3-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220607201440.41464-1-imp@bsdimp.com> References: <20220607201440.41464-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::12d; envelope-from=imp@bsdimp.com; helo=mail-il1-x12d.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+incoming=patchwork.ozlabs.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 c41ef0eda40..510307f29d9 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -186,6 +186,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 Tue Jun 7 20:14:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 1640082 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bsdimp-com.20210112.gappssmtp.com header.i=@bsdimp-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=SCAmuQu6; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) 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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4LHhSz2hgpz9sGJ for ; Wed, 8 Jun 2022 06:16:19 +1000 (AEST) Received: from localhost ([::1]:43748 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nyfcf-00067y-4F for incoming@patchwork.ozlabs.org; Tue, 07 Jun 2022 16:16:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57576) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nyfb4-00063c-SF for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:38 -0400 Received: from mail-il1-x129.google.com ([2607:f8b0:4864:20::129]:42883) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nyfb3-0004N8-1r for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:38 -0400 Received: by mail-il1-x129.google.com with SMTP id p1so15065114ilj.9 for ; Tue, 07 Jun 2022 13:14:36 -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=9VcXAAhXPRGN/o3HNZi64xOmTmhdRIXzGdmeZM9kuwk=; b=SCAmuQu6CHhpeQmEENfapPCOyjA1qLXyDcY23LjJAN2y/3FcOgXuUUOZBknEPHt/bf 5/9/KqJlvWWoEwm5MDtESy/X5hbc0COB5cKr/+599Og+WvESnK0FeeWWmM37JasswcmC bl5Bm7puVn9Q0glXhTfLJR8OHe1VbPGF8iwigBQPoj61y31WXTQyJ9GOwBJdOOEgPlH8 f0AWDvaL1QO2vQMxdDl4lV5Wunlnht5Tk4VVQsPiqa+4402pb2Rb6QPJljA8OAoFF2lK o29i1nqjAD51QOEBnG7QAUw0GyevvJ0BnSx6egliz27v3bdInEp9p30sMM2qvyL7aDWW MKlA== 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=9VcXAAhXPRGN/o3HNZi64xOmTmhdRIXzGdmeZM9kuwk=; b=CtI+E8rS2s24b9y++LuAgUKx5psV9tUz9GwsiDi2IRsslDX3o1ZJOIk6eF0ad1JTAW kfwRh1Mvw6GxfZYOQTHUAymYu36e1tJ2cv2ARV9biRoJqr29OycF/V+FXvFfgTY0qFmm 9toE0Na6HCrw+XV/gdsYAKWtiFs/o5qEXHJipA7XrUrNk3O2Q7eso7i49ReAIX3ILHLp umhs3Un7fMJ/t3FiXPW/oAcvaWv4GwsGq+N1ot9pTlzNr+MlNrrHcPhl+q3hTokNIdGx 8S51UXFtKi4b7CTWw62XtQGMRPlKuMuo55ydvIflrgSonOx2z52C+ShTSzKnAe1ZT9Rz EYdw== X-Gm-Message-State: AOAM533gUACtBpvKi/syVtPaLGXvbXIBchG5sKmCAv4Q+nvD9xfWZyC0 YQ5wcxHSIYoVb9ESjk6krX58hSKAR+wGTw== X-Google-Smtp-Source: ABdhPJw0Z7HlktlqX4qb9W28xW199wGG3Jk/yTL4Eu3e7mPkGgVMHmu2ogSPHdqXGP/pQaMT/rC6Qg== X-Received: by 2002:a05:6e02:8ae:b0:2c7:90a5:90b8 with SMTP id a14-20020a056e0208ae00b002c790a590b8mr18452104ilt.19.1654632875546; Tue, 07 Jun 2022 13:14:35 -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 cp5-20020a056638480500b0032ea3ba170dsm7053024jab.86.2022.06.07.13.14.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 13:14:34 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: arrowd@freebsd.org, def@freebsd.org, jrtc27@FreeBSD.org, Warner Losh , Kyle Evans Subject: [PATCH 3/6] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate Date: Tue, 7 Jun 2022 14:14:37 -0600 Message-Id: <20220607201440.41464-4-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220607201440.41464-1-imp@bsdimp.com> References: <20220607201440.41464-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::129; envelope-from=imp@bsdimp.com; helo=mail-il1-x129.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+incoming=patchwork.ozlabs.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 Reviewed-by: Richard Henderson --- bsd-user/freebsd/os-syscall.c | 50 ++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 510307f29d9..334c573739b 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -201,16 +201,58 @@ 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: + gemu_log("qemu: 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; + +#ifdef DEBUG + gemu_log("freebsd syscall %d\n", num); +#endif + 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); +#ifdef DEBUG + gemu_log(" = %ld\n", ret); +#endif + 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 Tue Jun 7 20:14:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 1640084 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bsdimp-com.20210112.gappssmtp.com header.i=@bsdimp-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=XShwYsSe; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) 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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4LHhY83YZwz9sFr for ; Wed, 8 Jun 2022 06:19:56 +1000 (AEST) Received: from localhost ([::1]:52330 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nyfgA-0004X4-IZ for incoming@patchwork.ozlabs.org; Tue, 07 Jun 2022 16:19:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57586) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nyfb5-00065Q-BX for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:39 -0400 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]:41977) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nyfb3-0004NK-PP for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:39 -0400 Received: by mail-il1-x133.google.com with SMTP id r3so15082942ilt.8 for ; Tue, 07 Jun 2022 13:14:37 -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=lKoewrYHVDI+U+A6mVZHxcPghd5cIO/uD93HJK3h92s=; b=XShwYsSexfwcA11iyKO+PN6IFXNulrbezZYQhzRR7rYD8KVd8L6wtn+WG0MNtfhcSx rsPB3b6dimlzRuA0Wu1Kg+8STp/DV4Rfp5kF6c/nNgIhuaDz1e2iS1K3NYpQtHxH4Dab uyWbK6hwhw1QqeHZE83G1+SLtVb/kOptKh4FG3m533t2ziWxa3xoa7FeQoo4g0DIexfb 6Tu3sSEVkU9E7mkqf/T4LOjmpmTtBAIHyTRsMh3kenX4RxIruvz9aqjJX1n12Ln5loxN 9XqW+paCXL435eAmYpuTdt6HuLhInU02KZyrNk1umthV/wqX4mpYNv19jqyK2BoSu4O/ axBQ== 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=lKoewrYHVDI+U+A6mVZHxcPghd5cIO/uD93HJK3h92s=; b=EvErC6ZR9aH5QlAFaRNvPSJO2yAgBFYg1Y8OPb69W8e0vh9bv8QUIcSJG2GsThiVLR Kzmup3voGQRK4+nD9pZPkeQTVeIwxur0KRSAxe8lt7exqZnK8ceQt6mIg5lCerVf1mGU cMacxcB0deuqwaXQUs3C3oQuXRbZyqxI1iuwSIEziVS+B3cZQeVvzv9619GPzqTLescM IawKCIjYq16Pujlk9C3hriDDGLfDTQJxOWvBprgKXMUAKnzLK/OGX5lEbGNKdAEa+inq Al/2hVI5EDaHdtEqWL7Q0uHrDlEkD42rKRo3qnPLQpcX35Imnj8F3fw7Frj3X+2R5BmL nhhg== X-Gm-Message-State: AOAM530BLvvNKF3jpCoEvqGu2P3yV7UME5RbbtVkryBI4ssjN/rWWbqP 6G7Oam+G/GzvSKx6l5ajLsSTAPsFhKTJ3w== X-Google-Smtp-Source: ABdhPJwHv75Ye9JbZ3UUf9alf/7X51oMqI39uTzcUuMBAU2X7wTGk28hXExQPwevDjJfrE/O8bxQzQ== X-Received: by 2002:a05:6e02:1523:b0:2d3:cb16:2d03 with SMTP id i3-20020a056e02152300b002d3cb162d03mr17354397ilu.198.1654632876471; Tue, 07 Jun 2022 13:14:36 -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 cp5-20020a056638480500b0032ea3ba170dsm7053024jab.86.2022.06.07.13.14.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 13:14:35 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: arrowd@freebsd.org, def@freebsd.org, jrtc27@FreeBSD.org, Warner Losh , Kyle Evans , Stacey Son , Kyle Evans , Richard Henderson Subject: [PATCH 4/6] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv Date: Tue, 7 Jun 2022 14:14:38 -0600 Message-Id: <20220607201440.41464-5-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220607201440.41464-1-imp@bsdimp.com> References: <20220607201440.41464-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::133; envelope-from=imp@bsdimp.com; helo=mail-il1-x133.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+incoming=patchwork.ozlabs.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 Reviewed-by: Richard Henderson --- bsd-user/bsd-file.h | 79 +++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 24 +++++++++++ 2 files changed, 103 insertions(+) diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h index a6bff3b8c26..ed305439e1a 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 inline 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 inline 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 inline 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 inline 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 334c573739b..79fb2cb69f8 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) { } @@ -211,6 +219,22 @@ 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; + default: gemu_log("qemu: unsupported syscall: %d\n", num); ret = -TARGET_ENOSYS; From patchwork Tue Jun 7 20:14:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 1640083 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bsdimp-com.20210112.gappssmtp.com header.i=@bsdimp-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=22PpXw7e; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) 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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4LHhXw5vQvz9sFr for ; Wed, 8 Jun 2022 06:19:44 +1000 (AEST) Received: from localhost ([::1]:51964 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nyffy-0004HD-16 for incoming@patchwork.ozlabs.org; Tue, 07 Jun 2022 16:19:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57592) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nyfb6-00067b-0b for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:40 -0400 Received: from mail-il1-x12f.google.com ([2607:f8b0:4864:20::12f]:43784) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nyfb4-0004Mf-9r for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:39 -0400 Received: by mail-il1-x12f.google.com with SMTP id h7so7488882ila.10 for ; Tue, 07 Jun 2022 13:14:37 -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=pQMqF1aMpPYbY9PbXj81qvpIN85mbCQFeDcpWQUX9OA=; b=22PpXw7ecOIwcQCuMhEuLcHhytJ6S9MqqfpSXcr1oRbb7koJM6WkKC/2g5zas/MqkE PQEst0o7LKkdkb8CBeH486kVxZZWavE7qBsy1pKgSnoWQe0uL9xZ8SHfSniw9Ue20ubN GV23XC07WI0BvhlqcCi8yN6WK4uORfznWIqmC0sRFIYYU5KaAFkpMoP8kn54qhMzJxpV syu4GEdY83K3MZGM2MlUulamW0wOvld/gxJVBJwNVTpPGI4fNh4whmJaGLNQGlz7t0BI U9AyoJh9L4GPHM+plZrNn4/tAeIOlHgmNVFsOq6/J6Ku9B3Wh0rhrt03G1F3HMXyH2bZ 3H5w== 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=pQMqF1aMpPYbY9PbXj81qvpIN85mbCQFeDcpWQUX9OA=; b=YVhVXfoVDENGRX3IOhUuyZkI25GIqNWAJQ3jwvMCb7BaA6JCZhNwAgfwG78VlJlyth wcwNJwj9KGuhamO/ukf7F1/AEOf7ZViZGzbRJfmmstRTyuWTXtZ/zPrEomp7NIq31hId lCuCSZdIKtteka1HmwOiX8DHdW8vXF6LT+i5kswWMv2PkQ+u7vxm8rMVAcF9t/PSBAbA +Oj3IXBpd+JCUCm2MgkC9DYRm5tFiPLDPLE05RXhdcNxzDQLqhAMkwNYRRKa7IiBv/Mh fOYt2mT1wILZQbl0kvtEWwtuYZUDZSiX6ROtASb3uEnjgbxBAZdjcsuCh8BDHL0RghZ4 MEXQ== X-Gm-Message-State: AOAM531CoswsX9c9mk73UXFiwNMcPqPB2ufQQ9vLXWIJS43SN8BgXbZv WISCdmS7TSmzR9jmpoHBEbcfGzSMPsWkMw== X-Google-Smtp-Source: ABdhPJx4YK7EUcQkBoheC0VnSlUPnyWoZ/CDBo48OrwnRVDeBNwr8q1LP4qWCW0ur7J4mM0dqOzrhQ== X-Received: by 2002:a05:6e02:168f:b0:2d3:c51d:7f69 with SMTP id f15-20020a056e02168f00b002d3c51d7f69mr17531284ila.64.1654632877420; Tue, 07 Jun 2022 13:14:37 -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 cp5-20020a056638480500b0032ea3ba170dsm7053024jab.86.2022.06.07.13.14.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 13:14:37 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: arrowd@freebsd.org, def@freebsd.org, jrtc27@FreeBSD.org, Warner Losh , Kyle Evans , Stacey Son , Kyle Evans , Richard Henderson Subject: [PATCH 5/6] bsd-user/bsd-file.h: Meat of the write system calls Date: Tue, 7 Jun 2022 14:14:39 -0600 Message-Id: <20220607201440.41464-6-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220607201440.41464-1-imp@bsdimp.com> References: <20220607201440.41464-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::12f; envelope-from=imp@bsdimp.com; helo=mail-il1-x12f.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+incoming=patchwork.ozlabs.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 Reviewed-by: Richard Henderson --- bsd-user/bsd-file.h | 85 +++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 23 ++++++++++ 2 files changed, 108 insertions(+) diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h index ed305439e1a..9c3dcb9ef3f 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 inline abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3) { @@ -106,4 +111,84 @@ static inline abi_long do_bsd_preadv(void *cpu_env, abi_long arg1, return ret; } +/* write(2) */ +static inline 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 inline 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 inline 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 inline 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 79fb2cb69f8..4c7c32daa56 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) { } @@ -235,6 +242,22 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, ret = do_bsd_readv(arg1, arg2, arg3); break; + 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: gemu_log("qemu: unsupported syscall: %d\n", num); ret = -TARGET_ENOSYS; From patchwork Tue Jun 7 20:14:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 1640085 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bsdimp-com.20210112.gappssmtp.com header.i=@bsdimp-com.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=do4D4MJm; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) 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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4LHhcg4wpRz9sFr for ; Wed, 8 Jun 2022 06:22:58 +1000 (AEST) Received: from localhost ([::1]:58424 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nyfj4-0000Kw-FT for incoming@patchwork.ozlabs.org; Tue, 07 Jun 2022 16:22:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57628) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nyfbG-0006RO-Km for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:50 -0400 Received: from mail-io1-xd32.google.com ([2607:f8b0:4864:20::d32]:36761) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nyfb5-0004Nq-NM for qemu-devel@nongnu.org; Tue, 07 Jun 2022 16:14:49 -0400 Received: by mail-io1-xd32.google.com with SMTP id s26so6819165ioa.3 for ; Tue, 07 Jun 2022 13:14:39 -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=/5GRLhuryvhFCNL7Z2U0IxtJCZm43srKeThQEapvKBc=; b=do4D4MJmsE6smWqbstnNw9gRaM0xV1/0pkXMyQKgl9qbA99krXLeQcCI+iCj4DnzIj ljuvNbE1/RKySseGCphMNXgHAawYaVNGRbXchh64/+TOwjkHh6Yv7FXk9/0ZZkTz3sAo yyipHxzSb4+73MmJMRaqFdLgCzljEJJc14l/acyxyxu5HeHEX7UBja+qQ5M77hZlFHTM jSX0/uNY+0Jm56JzKNj44HhvcsCAm3XEQnKgHoqPV3eQ4Q+DVqfM/UgfVJBntWHKSHMz 6+PspOgDA7DAyLEXy4NRDsom8+xZAohAVTOS+T15IWxEkUDkZUNxWgOxtizWPfUJvFn4 81RQ== 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=/5GRLhuryvhFCNL7Z2U0IxtJCZm43srKeThQEapvKBc=; b=4b0lUukgBEmX/8JzY78mbYmksn1Gbs43r78baHGCIiodyzBSkNHno+sgtv+gAltoZp H1FGupRvXZt3fUksowadoGIvLu07B90QF7BsPtyLSjUdnbqDbPJXI9252P0DVM60LwvY IU+LRZsEhQHuI8VkWsfFWPfIskuFc2oanfHtby7UbP+7TlHJ5XPKOEZCJ9+8ZiF7UCg/ O541dABQdLiSC3cJkgv4CRoFANtXAqpwYRIoz69DenyD9gpwB1IYzroYldzsAfYZDGGG hEcFoMKJv9fJv9tU4ovkrqyp0t7xoyg6P4y8/BQUzcfLet+Tu4xFC/VPc5z/9fZgee+K bgfA== X-Gm-Message-State: AOAM532/sLo7yMIZm6nX/sZJ/pkZjRTFx5+lVV/NHo3S0B1FdkRvxzfF +/aiNbjl9o6QiRAIj/tJEYWSp111DRnFXg== X-Google-Smtp-Source: ABdhPJw5F62FLv8VA0HO0JVzElui1peHZUyOEP3XEQ03wviW4wNkelc2iB5LmolzYYUljgn4o23x9g== X-Received: by 2002:a02:2124:0:b0:32d:beca:e5ab with SMTP id e36-20020a022124000000b0032dbecae5abmr16793683jaa.119.1654632878351; Tue, 07 Jun 2022 13:14:38 -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 cp5-20020a056638480500b0032ea3ba170dsm7053024jab.86.2022.06.07.13.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 13:14:37 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: arrowd@freebsd.org, def@freebsd.org, jrtc27@FreeBSD.org, Warner Losh , Kyle Evans , Stacey Son , Kyle Evans , Richard Henderson Subject: [PATCH 6/6] bsd-user/freebsd/os-syscall.c: Implement exit Date: Tue, 7 Jun 2022 14:14:40 -0600 Message-Id: <20220607201440.41464-7-imp@bsdimp.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20220607201440.41464-1-imp@bsdimp.com> References: <20220607201440.41464-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::d32; envelope-from=imp@bsdimp.com; helo=mail-io1-xd32.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+incoming=patchwork.ozlabs.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 4c7c32daa56..2daba0e623c 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); @@ -226,6 +227,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.