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>,
	"Sean Bruno" <sbruno@FreeBSD.org>,
	"Juergen Lock" <nox@jelal.kn-bremen.de>,
	"Raphael Kubo da Costa" <rakuco@FreeBSD.org>,
	"Stacey Son" <sson@FreeBSD.org>
Subject: [PATCH v2 06/12] bsd-user: Helper routines h2t_old_sysctl
Date: Mon, 13 Feb 2023 17:27:51 -0700	[thread overview]
Message-ID: <20230214002757.99240-7-imp@bsdimp.com> (raw)
In-Reply-To: <20230214002757.99240-1-imp@bsdimp.com>

h2t_old_sysctl does the byte swapping in the data to return it to the
target for the 'well known' types.

Co-Authored-by: Sean Bruno <sbruno@FreeBSD.org>
Signed-off-by: Sean Bruno <sbruno@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: Raphael Kubo da Costa <rakuco@FreeBSD.org>
Signed-off-by: Raphael Kubo da Costa <rakuco@FreeBSD.org>
Co-Authored-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/os-sys.c | 95 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 91 insertions(+), 4 deletions(-)

diff --git a/bsd-user/freebsd/os-sys.c b/bsd-user/freebsd/os-sys.c
index 1df53a3e53b..457e61f5b36 100644
--- a/bsd-user/freebsd/os-sys.c
+++ b/bsd-user/freebsd/os-sys.c
@@ -29,7 +29,7 @@
  * Compare with sys/kern_sysctl.c ctl_size
  * Note: Not all types appear to be used in-tree.
  */
-static const int G_GNUC_UNUSED target_ctl_size[CTLTYPE+1] = {
+static const int target_ctl_size[CTLTYPE+1] = {
 	[CTLTYPE_INT] = sizeof(abi_int),
 	[CTLTYPE_UINT] = sizeof(abi_uint),
 	[CTLTYPE_LONG] = sizeof(abi_long),
@@ -44,7 +44,7 @@ static const int G_GNUC_UNUSED target_ctl_size[CTLTYPE+1] = {
 	[CTLTYPE_U64] = sizeof(uint64_t),
 };
 
-static const int G_GNUC_UNUSED host_ctl_size[CTLTYPE+1] = {
+static const int host_ctl_size[CTLTYPE+1] = {
 	[CTLTYPE_INT] = sizeof(int),
 	[CTLTYPE_UINT] = sizeof(u_int),
 	[CTLTYPE_LONG] = sizeof(long),
@@ -97,7 +97,7 @@ static abi_ulong G_GNUC_UNUSED scale_to_target_pages(uint64_t pages)
 }
 
 #ifdef TARGET_ABI32
-static abi_long G_GNUC_UNUSED h2t_long_sat(long l)
+static abi_long h2t_long_sat(long l)
 {
     if (l > INT32_MAX) {
         l = INT32_MAX;
@@ -107,7 +107,7 @@ static abi_long G_GNUC_UNUSED h2t_long_sat(long l)
     return l;
 }
 
-static abi_ulong G_GNUC_UNUSED h2t_ulong_sat(u_long ul)
+static abi_ulong h2t_ulong_sat(u_long ul)
 {
     if (ul > UINT32_MAX) {
         ul = UINT32_MAX;
@@ -153,6 +153,93 @@ static int G_GNUC_UNUSED oidfmt(int *oid, int len, char *fmt, uint32_t *kind)
     return 0;
 }
 
+/*
+ * Convert the old value from host to target.
+ *
+ * For LONG and ULONG on ABI32, we need to 'down convert' the 8 byte quantities
+ * to 4 bytes. The caller setup a buffer in host memory to get this data from
+ * the kernel and pass it to us. We do the down conversion and adjust the length
+ * so the caller knows what to write as the returned length into the target when
+ * it copies the down converted values into the target.
+ *
+ * For normal integral types, we just need to byte swap. No size changes.
+ *
+ * For strings and node data, there's no conversion needed.
+ *
+ * For opaque data, per sysctl OID converts take care of it.
+ */
+static void G_GNUC_UNUSED h2t_old_sysctl(void *holdp, size_t *holdlen, uint32_t kind)
+{
+    size_t len;
+    int hlen, tlen;
+    uint8_t *hp, *tp;
+
+    /*
+     * Although rare, we can have arrays of sysctl. Both sysctl_old_ddb in
+     * kern_sysctl.c and show_var in sbin/sysctl/sysctl.c have code that loops
+     * this way.  *holdlen has been set by the kernel to the host's length.
+     * Only LONG and ULONG on ABI32 have different sizes: see below.
+     */
+    hp = (uint8_t *)holdp;
+    tp = hp;
+    len = 0;
+    hlen = host_ctl_size[kind & CTLTYPE];
+    tlen = target_ctl_size[kind & CTLTYPE];
+
+    /*
+     * hlen == 0 for CTLTYPE_STRING and CTLTYPE_NODE, which need no conversion
+     * as well as CTLTYPE_OPAQUE, which needs special converters.
+     */
+    if (hlen == 0) {
+        return;
+    }
+
+    while (len < *holdlen) {
+        if (hlen == tlen) {
+            switch (hlen) {
+            case 1:
+                /* Nothing needed: no byteswapping and assigning in place */
+                break;
+            case 2:
+                *(uint16_t *)tp = tswap16(*(uint16_t *)hp);
+                break;
+            case 4:
+                *(uint32_t *)tp = tswap32(*(uint32_t *)hp);
+                break;
+            case 8:
+                *(uint64_t *)tp = tswap64(*(uint64_t *)hp);
+                break;
+            }
+        }
+#ifdef TARGET_ABI32
+        else {
+            /*
+             * Saturating assignment for the only two types that differ between
+             * 32-bit and 64-bit machines. All other integral types have the
+             * same, fixed size and will be converted w/o loss of precision
+             * in the above switch.
+             */
+            switch (kind & CTLTYPE) {
+            case CTLTYPE_LONG:
+                *(abi_long *)tp = tswap32(h2t_long_sat(*(long *)hp));
+                break;
+            case CTLTYPE_ULONG:
+                *(abi_ulong *)tp = tswap32(h2t_ulong_sat(*(u_long *)hp));
+                break;
+            }
+        }
+#endif
+        tp += tlen;
+        hp += hlen;
+        len += hlen;
+    }
+#ifdef TARGET_ABI32
+    if (hlen != tlen) {
+        *holdlen = (*holdlen / hlen) * tlen;
+    }
+#endif
+}
+
 /* sysarch() is architecture dependent. */
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2)
 {
-- 
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 ` Warner Losh [this message]
2023-02-14 21:16   ` [PATCH v2 06/12] bsd-user: Helper routines h2t_old_sysctl 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 ` [PATCH v2 10/12] bsd-user: do_freebsd_sysctl helper for sysctl(2) Warner Losh
2023-02-14 21:45   ` 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-7-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=rakuco@FreeBSD.org \
    --cc=richard.henderson@linaro.org \
    --cc=sbruno@FreeBSD.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.