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
On 2/13/23 14:27, Warner Losh wrote:
> +/*
> + * Convert the old value from host to target.
host vs guest is clearer language; "target" gets overloaded, even though still present in
the code base.
> + *
> + * 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)
h2g.
> + /*
> + * 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;
> + }
default: g_assert_not_reached().
> + }
> +#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;
> + }
default: g_assert_not_reached().
> + }
> +#endif
#else
g_assert_not_reached();
r~
On Tue, Feb 14, 2023 at 2:16 PM Richard Henderson <
richard.henderson@linaro.org> wrote:
> On 2/13/23 14:27, Warner Losh wrote:
> > +/*
> > + * Convert the old value from host to target.
>
> host vs guest is clearer language; "target" gets overloaded, even though
> still present in
> the code base.
>
OK. Will do. We have that all over the place upstream... I'll start there
too...
>
> > + *
> > + * 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)
>
> h2g.
>
OK.
> > + /*
> > + * 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;
> > + }
>
> default: g_assert_not_reached().
>
Ah! I need that in several places... Thanks.
> > + }
> > +#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;
> > + }
>
> default: g_assert_not_reached().
>
> > + }
> > +#endif
>
> #else
> g_assert_not_reached();
>
Gotcha... Thanks!
Warner
>
> r~
>
© 2016 - 2026 Red Hat, Inc.