All of lore.kernel.org
 help / color / mirror / Atom feed
From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: "Alex Bennée" <alex.bennee@linaro.org>,
	"Warner Losh" <imp@bsdimp.com>,
	richard.henderson@linaro.org,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	kevans@freebsd.org, f4bug@amsat.org,
	"Thomas Huth" <thuth@redhat.com>,
	"Kyle Evans" <kevans@FreeBSD.org>,
	"Juergen Lock" <nox@jelal.kn-bremen.de>,
	"Stacey Son" <sson@FreeBSD.org>
Subject: [PATCH v2 10/12] bsd-user: do_freebsd_sysctl helper for sysctl(2)
Date: Mon, 13 Feb 2023 17:27:55 -0700	[thread overview]
Message-ID: <20230214002757.99240-11-imp@bsdimp.com> (raw)
In-Reply-To: <20230214002757.99240-1-imp@bsdimp.com>

From: Kyle Evans <kevans@FreeBSD.org>

Implement the wrapper function for sysctl(2). This puts the oid
arguments into a standard form and calls the common
do_freebsd_sysctl_oid.

Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Co-Authored-by: Juergen Lock <nox@jelal.kn-bremen.de>
Signed-off-by: Juergen Lock <nox@jelal.kn-bremen.de>
Co-Authored-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Reviewed-by: Warner Losh <imp@bsdimp.com>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/os-sys.c     | 57 ++++++++++++++++++++++++++++++++++-
 bsd-user/freebsd/os-syscall.c |  4 +++
 bsd-user/qemu.h               |  2 ++
 3 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/bsd-user/freebsd/os-sys.c b/bsd-user/freebsd/os-sys.c
index cbaa70958b9..e70f8f21c52 100644
--- a/bsd-user/freebsd/os-sys.c
+++ b/bsd-user/freebsd/os-sys.c
@@ -258,7 +258,7 @@ static inline void sysctl_oidfmt(uint32_t *holdp)
     holdp[0] = tswap32(holdp[0]);
 }
 
-static abi_long G_GNUC_UNUSED do_freebsd_sysctl_oid(CPUArchState *env, int32_t *snamep,
+static abi_long do_freebsd_sysctl_oid(CPUArchState *env, int32_t *snamep,
         int32_t namelen, void *holdp, size_t *holdlenp, void *hnewp,
         size_t newlen)
 {
@@ -481,6 +481,61 @@ out:
     return ret;
 }
 
+abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
+        abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
+{
+    abi_long ret = -TARGET_EFAULT;
+    void *hnamep, *holdp = NULL, *hnewp = NULL;
+    size_t holdlen;
+    abi_ulong oldlen = 0;
+    int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
+
+    /* oldlenp is read/write, pre-check here for write */
+    if (oldlenp) {
+        if (!access_ok(VERIFY_WRITE, oldlenp, sizeof(abi_ulong)) ||
+            get_user_ual(oldlen, oldlenp)) {
+            goto out;
+        }
+    }
+    hnamep = lock_user(VERIFY_READ, namep, namelen, 1);
+    if (hnamep == NULL) {
+        goto out;
+    }
+    if (newp) {
+        hnewp = lock_user(VERIFY_READ, newp, newlen, 1);
+        if (hnewp == NULL) {
+            goto out;
+        }
+    }
+    if (oldp) {
+        holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0);
+        if (holdp == NULL) {
+            goto out;
+        }
+    }
+    holdlen = oldlen;
+    for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++, q++) {
+        *q = tswap32(*p);
+    }
+
+    ret = do_freebsd_sysctl_oid(env, snamep, namelen, holdp, &holdlen, hnewp,
+        newlen);
+
+    /*
+     * writeability pre-checked above. __sysctl(2) returns ENOMEM and updates
+     * oldlenp for the proper size to use.
+     */
+    if (oldlenp && (ret == 0 || ret == -TARGET_ENOMEM)) {
+        put_user_ual(holdlen, oldlenp);
+    }
+    unlock_user(hnamep, namep, 0);
+    unlock_user(holdp, oldp, holdlen);
+    unlock_user(holdp, oldp, ret == 0 ? holdlen : 0);
+out:
+    g_free(snamep);
+    return ret;
+}
+
 /* sysarch() is architecture dependent. */
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2)
 {
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index e00997a818c..20ab3d4d9a1 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -494,6 +494,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         /*
          * sys{ctl, arch, call}
          */
+    case TARGET_FREEBSD_NR___sysctl: /* sysctl(3) */
+        ret = do_freebsd_sysctl(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
+        break;
+
     case TARGET_FREEBSD_NR_sysarch: /* sysarch(2) */
         ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
         break;
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 0ceecfb6dfa..c7248cfde6f 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -252,6 +252,8 @@ bool is_error(abi_long ret);
 int host_to_target_errno(int err);
 
 /* os-sys.c */
+abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
+        abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
 
 /* user access */
-- 
2.39.1



  parent reply	other threads:[~2023-02-14  0:29 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-14  0:27 [PATCH v2 00/12] 2023 Q1 bsd-user upstreaming: bugfixes and sysctl Warner Losh
2023-02-14  0:27 ` [PATCH v2 01/12] bsd-user: Don't truncate the return value from freebsd_syscall Warner Losh
2023-02-14  0:27 ` [PATCH v2 02/12] build: Don't specify -no-pie for --static user-mode programs Warner Losh
2023-02-14  0:27 ` [PATCH v2 03/12] bsd-user: Add sysarch syscall Warner Losh
2023-02-14  0:27 ` [PATCH v2 04/12] bsd-user: various helper routines for sysctl Warner Losh
2023-02-14 20:52   ` Richard Henderson
2023-02-14 21:31     ` Warner Losh
2023-02-14 21:39       ` Richard Henderson
2023-02-14  0:27 ` [PATCH v2 05/12] bsd-user: Helper routines oidfmt Warner Losh
2023-02-14 20:53   ` Richard Henderson
2023-02-14  0:27 ` [PATCH v2 06/12] bsd-user: Helper routines h2t_old_sysctl Warner Losh
2023-02-14 21:16   ` Richard Henderson
2023-02-15  5:58     ` Warner Losh
2023-02-14  0:27 ` [PATCH v2 07/12] bsd-user: sysctl helper funtions: sysctl_name2oid and sysctl_oidfmt Warner Losh
2023-02-14 21:18   ` Richard Henderson
2023-02-14  0:27 ` [PATCH v2 08/12] bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants Warner Losh
2023-02-14 21:21   ` Richard Henderson
2023-02-14  0:27 ` [PATCH v2 09/12] bsd-user: Start translation of arch-specific sysctls Warner Losh
2023-02-14 21:36   ` Richard Henderson
2023-02-16 22:57     ` Warner Losh
2023-02-14  0:27 ` Warner Losh [this message]
2023-02-14 21:45   ` [PATCH v2 10/12] bsd-user: do_freebsd_sysctl helper for sysctl(2) Richard Henderson
2023-02-14  0:27 ` [PATCH v2 11/12] bsd-user: implement sysctlbyname(2) Warner Losh
2023-02-14 21:48   ` Richard Henderson
2023-02-14  0:27 ` [PATCH v2 12/12] bsd-user: Add -strict Warner Losh
2023-02-14  6:57   ` Philippe Mathieu-Daudé
2023-02-14 21:49   ` Richard Henderson
2023-02-16 22:37     ` Warner Losh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230214002757.99240-11-imp@bsdimp.com \
    --to=imp@bsdimp.com \
    --cc=alex.bennee@linaro.org \
    --cc=f4bug@amsat.org \
    --cc=kevans@freebsd.org \
    --cc=nox@jelal.kn-bremen.de \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=sson@FreeBSD.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.