Skip to contentSkip to conversation

2023 Q1 bsd-user upstreaming: bugfixes and sysctl

PATCH Superseded Warner Losh 3 participants 30 messages Feb 10, 2023 – Feb 13, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#3 lore raw

PATCH 2/9 build: Don't specify -no-pie for --static user-mode programs

In reply to Warner Losh

When building with clang, -no-pie gives a warning on every single build, so remove it.

---
 configure | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
configure +1 -1
index 64960c6000f..eb284ccf308 100755
@@ -1313,7 +1313,7 @@ if test "$static" = "yes"; then
     error_exit "-static-pie not available due to missing toolchain support"
   else
     pie="no"
-    QEMU_CFLAGS="-fno-pie -no-pie $QEMU_CFLAGS"
+    QEMU_CFLAGS="-fno-pie $QEMU_CFLAGS"
   fi
 elif test "$pie" = "no"; then
   if compile_prog "-Werror -fno-pie" "-no-pie"; then
Signature · 1 line
2.39.1
#4 lore raw

PATCH 3/9 bsd-user: Add sysarch syscall

In reply to Warner Losh

From: Stacey Son <[email protected]>

Connect up the sysarch system call.

---
 bsd-user/freebsd/os-syscall.c | 7 +++++++
 1 file changed, 7 insertions(+)
bsd-user/freebsd/os-syscall.c +7 -0
index b4a663fc021..e00997a818c 100644
@@ -491,6 +491,13 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_undelete(arg1);
         break;
 
+        /*
+         * sys{ctl, arch, call}
+         */
+    case TARGET_FREEBSD_NR_sysarch: /* sysarch(2) */
+        ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
+        break;
+
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
         ret = -TARGET_ENOSYS;
Signature · 1 line
2.39.1
#6 lore raw

PATCH 4/9 bsd-user: Two helper routines oidfmt and sysctl_oldcvt

In reply to Warner Losh

From: Stacey Son <[email protected]>

oidfmt uses undocumented system call to get the type of the sysctl. sysctl_oldcvt does the byte swapping in the data to return it to the target.

---
 bsd-user/freebsd/os-sys.c | 94 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
bsd-user/freebsd/os-sys.c +94 -0
index 1676ec10f83..e3b9f168a2b 100644
@@ -21,6 +21,100 @@
 #include "qemu.h"
 #include "target_arch_sysarch.h"
 
+#include <sys/sysctl.h>
+
+/*
+ * This uses the undocumented oidfmt interface to find the kind of a requested
+ * sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt() (compare to
+ * src/sbin/sysctl/sysctl.c)
+ */
+static int oidfmt(int *oid, int len, char *fmt, uint32_t *kind)
+{
+    int qoid[CTL_MAXNAME + 2];
+    uint8_t buf[BUFSIZ];
+    int i;
+    size_t j;
+
+    qoid[0] = 0;
+    qoid[1] = 4;
+    memcpy(qoid + 2, oid, len * sizeof(int));
+
+    j = sizeof(buf);
+    i = sysctl(qoid, len + 2, buf, &j, 0, 0);
+    if (i) {
+        return i;
+    }
+
+    if (kind) {
+        *kind = *(uint32_t *)buf;
+    }
+
+    if (fmt) {
+        strcpy(fmt, (char *)(buf + sizeof(uint32_t)));
+    }
+    return 0;
+}
+
+/*
+ * try and convert sysctl return data for the target.
+ * Note: doesn't handle CTLTYPE_OPAQUE and CTLTYPE_STRUCT.
+ */
+static int sysctl_oldcvt(void *holdp, size_t *holdlen, uint32_t kind)
+{
+    switch (kind & CTLTYPE) {
+    case CTLTYPE_INT:
+    case CTLTYPE_UINT:
+        *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
+        break;
+
+#ifdef TARGET_ABI32
+    case CTLTYPE_LONG:
+    case CTLTYPE_ULONG:
+        /*
+         * If the sysctl has a type of long/ulong but seems to be bigger than
+         * these data types, its probably an array.  Double check that its
+         * evenly divisible by the size of long and convert holdp to a series of
+         * 32bit elements instead, adjusting holdlen to the new size.
+         */
+        if ((*holdlen > sizeof(abi_ulong)) &&
+            ((*holdlen % sizeof(abi_ulong)) == 0)) {
+            int array_size = *holdlen / sizeof(long);
+            int i;
+            if (holdp) {
+                for (i = 0; i < array_size; i++) {
+                    ((uint32_t *)holdp)[i] = tswap32(((long *)holdp)[i]);
+                }
+                *holdlen = array_size * sizeof(abi_ulong);
+            } else {
+                *holdlen = sizeof(abi_ulong);
+            }
+        } else {
+            *(uint32_t *)holdp = tswap32(*(long *)holdp);
+            *holdlen = sizeof(uint32_t);
+        }
+        break;
+#else
+    case CTLTYPE_LONG:
+        *(uint64_t *)holdp = tswap64(*(long *)holdp);
+        break;
+    case CTLTYPE_ULONG:
+        *(uint64_t *)holdp = tswap64(*(unsigned long *)holdp);
+        break;
+#endif
+    case CTLTYPE_U64:
+    case CTLTYPE_S64:
+        *(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
+        break;
+
+    case CTLTYPE_STRING:
+        break;
+
+    default:
+        return -1;
+    }
+    return 0;
+}
+
 /* sysarch() is architecture dependent. */
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2)
 {
Signature · 1 line
2.39.1
#25 lore raw

PATCH 9/9 bsd-user: Add -strict

In reply to Warner Losh

Most of the time, it's useful to make our best effort, but sometimes we want to know right away when we don't implement something. First place we use it is for unknown syscalls.

---
 bsd-user/freebsd/os-syscall.c | 4 ++++
 bsd-user/main.c               | 5 ++++-
 bsd-user/qemu.h               | 1 +
 3 files changed, 9 insertions(+), 1 deletion(-)
bsd-user/freebsd/os-syscall.c +4 -0
index 179a20c304b..e2b26ecb8dd 100644
@@ -508,6 +508,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
+        if (bsd_user_strict) {
+            printf("Unimplemented system call %d\n", num);
+            abort();
+        }
         ret = -TARGET_ENOSYS;
         break;
     }
bsd-user/main.c +4 -1
index 41290e16f98..ba0ad86ad28 100644
@@ -91,9 +91,10 @@ unsigned long reserved_va = MAX_RESERVED_VA;
 unsigned long reserved_va;
 #endif
 
-static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
+const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
 char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
+bool bsd_user_strict = false;	/* Abort for unimplemned things */
 
 unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
 unsigned long target_dfldsiz = TARGET_DFLDSIZ;   /* initial data size limit */
@@ -396,6 +397,8 @@ int main(int argc, char **argv)
             trace_opt_parse(optarg);
         } else if (!strcmp(r, "0")) {
             argv0 = argv[optind++];
+        } else if (!strcmp(r, "strict")) {
+            bsd_user_strict = true;
         } else {
             usage();
         }
bsd-user/qemu.h +1 -0
index e24a8cfcfb1..22bd5a3df42 100644
@@ -113,6 +113,7 @@ typedef struct TaskState {
 
 void stop_all_tasks(void);
 extern const char *qemu_uname_release;
+extern bool bsd_user_strict;
 
 /*
  * TARGET_ARG_MAX defines the number of bytes allocated for arguments
Signature · 1 line
2.39.1
#11 lore raw

PATCH 5/9 bsd-user: sysctl helper funtions: sysctl_name2oid and sysctl_oidfmt

In reply to Warner Losh

From: Juergen Lock <[email protected]>

Helper functions for sysctl implementations. sysctl_name2oid and sysctl_oidfmt convert oids between host and targets

---
 bsd-user/freebsd/os-sys.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
bsd-user/freebsd/os-sys.c +18 -0
index e3b9f168a2b..ac5ab9b17bc 100644
@@ -115,6 +115,24 @@ static int sysctl_oldcvt(void *holdp, size_t *holdlen, uint32_t kind)
     return 0;
 }
 
+/*
+ * Convert the undocmented name2oid sysctl data for the target.
+ */
+static inline void sysctl_name2oid(uint32_t *holdp, size_t holdlen)
+{
+    size_t i, num = holdlen / sizeof(uint32_t);
+
+    for (i = 0; i < num; i++) {
+        holdp[i] = tswap32(holdp[i]);
+    }
+}
+
+static inline void sysctl_oidfmt(uint32_t *holdp)
+{
+    /* byte swap the kind */
+    holdp[0] = tswap32(holdp[0]);
+}
+
 /* sysarch() is architecture dependent. */
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2)
 {
Signature · 1 line
2.39.1
#18 lore raw

PATCH 7/9 bsd-user: do_freebsd_sysctl helper for sysctl(2)

In reply to Warner Losh

From: Kyle Evans <[email protected]>

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

---
 bsd-user/freebsd/os-sys.c     | 50 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  4 +++
 2 files changed, 54 insertions(+)
bsd-user/freebsd/os-sys.c +50 -0
index a8fb29f36b7..13736936e5f 100644
@@ -345,6 +345,56 @@ 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;
+    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;
+
+    if (oldlenp) {
+        if (get_user_ual(oldlen, oldlenp)) {
+            return -TARGET_EFAULT;
+        }
+    }
+    hnamep = lock_user(VERIFY_READ, namep, namelen, 1);
+    if (hnamep == NULL) {
+        return -TARGET_EFAULT;
+    }
+    if (newp) {
+        hnewp = lock_user(VERIFY_READ, newp, newlen, 1);
+        if (hnewp == NULL) {
+            return -TARGET_EFAULT;
+        }
+    }
+    if (oldp) {
+        holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0);
+        if (holdp == NULL) {
+            return -TARGET_EFAULT;
+        }
+    }
+    holdlen = oldlen;
+    for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++) {
+        *q++ = tswap32(*p);
+    }
+
+    ret = do_freebsd_sysctl_oid(env, snamep, namelen, holdp, &holdlen, hnewp,
+        newlen);
+
+    if (oldlenp) {
+        put_user_ual(holdlen, oldlenp);
+    }
+    unlock_user(hnamep, namep, 0);
+    unlock_user(holdp, oldp, holdlen);
+    if (hnewp) {
+        unlock_user(hnewp, newp, 0);
+    }
+    g_free(snamep);
+    return ret;
+}
+
 /* sysarch() is architecture dependent. */
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2)
 {
bsd-user/freebsd/os-syscall.c +4 -0
index e00997a818c..20ab3d4d9a1 100644
@@ -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;
Signature · 1 line
2.39.1
#12 lore raw

PATCH 6/9 bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants

In reply to Warner Losh

From: Juergen Lock <[email protected]>

do_freebsd_sysctl_oid filters out some of the binary and special sysctls where host != target. This commit focuses on the simple sysctls that can be done in a few lines.

---
 bsd-user/freebsd/os-sys.c | 212 ++++++++++++++++++++++++++++++++++++++
 bsd-user/qemu.h           |   5 +
 2 files changed, 217 insertions(+)
bsd-user/freebsd/os-sys.c +212 -0
index ac5ab9b17bc..a8fb29f36b7 100644
@@ -133,6 +133,218 @@ static inline void sysctl_oidfmt(uint32_t *holdp)
     holdp[0] = tswap32(holdp[0]);
 }
 
+#define bsd_get_ncpu() 1 /* Placeholder */
+
+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)
+{
+    uint32_t kind = 0;
+#if TARGET_ABI_BITS != HOST_LONG_BITS
+    const abi_ulong maxmem = -0x100c000;
+#endif
+    abi_long ret;
+    size_t holdlen, oldlen;
+
+    holdlen = oldlen = *holdlenp;
+    oidfmt(snamep, namelen, NULL, &kind);
+
+    /* Handle some arch/emulator dependent sysctl()'s here. */
+    switch (snamep[0]) {
+#if defined(TARGET_PPC) || defined(TARGET_PPC64)
+    case CTL_MACHDEP:
+        switch (snamep[1]) {
+        case 1:    /* CPU_CACHELINE */
+            holdlen = sizeof(uint32_t);
+            (*(uint32_t *)holdp) = tswap32(env->dcache_line_size);
+            ret = 0;
+            goto out;
+        }
+        break;
+#endif
+    case CTL_KERN:
+        switch (snamep[1]) {
+        case KERN_USRSTACK:
+            if (oldlen) {
+                (*(abi_ulong *)holdp) = tswapal(TARGET_USRSTACK);
+            }
+            holdlen = sizeof(abi_ulong);
+            ret = 0;
+            goto out;
+
+        case KERN_PS_STRINGS:
+            if (oldlen) {
+                (*(abi_ulong *)holdp) = tswapal(TARGET_PS_STRINGS);
+            }
+            holdlen = sizeof(abi_ulong);
+            ret = 0;
+            goto out;
+
+        default:
+            break;
+        }
+        break;
+
+    case CTL_HW:
+        switch (snamep[1]) {
+        case HW_MACHINE:
+            holdlen = sizeof(TARGET_HW_MACHINE);
+            if (holdp) {
+                strlcpy(holdp, TARGET_HW_MACHINE, oldlen);
+            }
+            ret = 0;
+            goto out;
+
+        case HW_MACHINE_ARCH:
+        {
+            holdlen = sizeof(TARGET_HW_MACHINE_ARCH);
+            if (holdp) {
+                strlcpy(holdp, TARGET_HW_MACHINE_ARCH, oldlen);
+            }
+            ret = 0;
+            goto out;
+        }
+        case HW_NCPU:
+            if (oldlen) {
+                (*(int32_t *)holdp) = tswap32(bsd_get_ncpu());
+            }
+            holdlen = sizeof(int32_t);
+            ret = 0;
+            goto out;
+#if defined(TARGET_ARM)
+        case HW_FLOATINGPT:
+            if (oldlen) {
+#ifdef ARM_FEATURE_VFP /* XXX FIXME XXX */
+                if (env->features & ((1ULL << ARM_FEATURE_VFP)|
+                                     (1ULL << ARM_FEATURE_VFP3)|
+                                     (1ULL << ARM_FEATURE_VFP4)))
+                    *(int32_t *)holdp = 1;
+                else
+                    *(int32_t *)holdp = 0;
+#else
+                *(int32_t *)holdp = 1;
+#endif
+            }
+            holdlen = sizeof(int32_t);
+            ret = 0;
+            goto out;
+#endif
+
+
+#if TARGET_ABI_BITS != HOST_LONG_BITS
+        case HW_PHYSMEM:
+        case HW_USERMEM:
+        case HW_REALMEM:
+            holdlen = sizeof(abi_ulong);
+            ret = 0;
+
+            if (oldlen) {
+                int mib[2] = {snamep[0], snamep[1]};
+                unsigned long lvalue;
+                size_t len = sizeof(lvalue);
+
+                if (sysctl(mib, 2, &lvalue, &len, NULL, 0) == -1) {
+                    ret = -1;
+                } else {
+                    if (((unsigned long)maxmem) < lvalue) {
+                        lvalue = maxmem;
+                    }
+                    (*(abi_ulong *)holdp) = tswapal((abi_ulong)lvalue);
+                }
+            }
+            goto out;
+#endif
+
+        default:
+        {
+            static int oid_hw_availpages;
+            static int oid_hw_pagesizes;
+
+            if (!oid_hw_availpages) {
+                int real_oid[CTL_MAXNAME + 2];
+                size_t len = sizeof(real_oid) / sizeof(int);
+
+                if (sysctlnametomib("hw.availpages", real_oid, &len) >= 0) {
+                    oid_hw_availpages = real_oid[1];
+                }
+            }
+            if (!oid_hw_pagesizes) {
+                int real_oid[CTL_MAXNAME + 2];
+                size_t len = sizeof(real_oid) / sizeof(int);
+
+                if (sysctlnametomib("hw.pagesizes", real_oid, &len) >= 0) {
+                    oid_hw_pagesizes = real_oid[1];
+                }
+            }
+
+            if (oid_hw_availpages && snamep[1] == oid_hw_availpages) {
+                long lvalue;
+                size_t len = sizeof(lvalue);
+
+                if (sysctlbyname("hw.availpages", &lvalue, &len, NULL, 0) == -1) {
+                    ret = -1;
+                } else {
+                    if (oldlen) {
+#if TARGET_ABI_BITS != HOST_LONG_BITS
+                        abi_ulong maxpages = maxmem / (abi_ulong)getpagesize();
+                        if (((unsigned long)maxpages) < lvalue) {
+                            lvalue = maxpages;
+                        }
+#endif
+                        (*(abi_ulong *)holdp) = tswapal((abi_ulong)lvalue);
+                    }
+                    holdlen = sizeof(abi_ulong);
+                    ret = 0;
+                }
+                goto out;
+            }
+
+            if (oid_hw_pagesizes && snamep[1] == oid_hw_pagesizes) {
+                if (oldlen) {
+                    (*(abi_ulong *)holdp) = tswapal((abi_ulong)getpagesize());
+                    ((abi_ulong *)holdp)[1] = 0;
+                }
+                holdlen = sizeof(abi_ulong) * 2;
+                ret = 0;
+                goto out;
+            }
+            break;
+        }
+        }
+        break;
+
+    default:
+        break;
+    }
+
+    ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
+    if (!ret && (holdp != 0)) {
+
+        if (0 == snamep[0] &&
+            (2 == snamep[1] || 3 == snamep[1] || 4 == snamep[1])) {
+            switch (snamep[1]) {
+            case 2:
+            case 3:
+                /* Handle the undocumented name2oid special case. */
+                sysctl_name2oid(holdp, holdlen);
+                break;
+
+            case 4:
+            default:
+                /* Handle oidfmt */
+                sysctl_oidfmt(holdp);
+                break;
+            }
+        } else {
+            sysctl_oldcvt(holdp, &holdlen, kind);
+        }
+    }
+
+out:
+    *holdlenp = holdlen;
+    return ret;
+}
+
 /* sysarch() is architecture dependent. */
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2)
 {
bsd-user/qemu.h +5 -0
index 0ceecfb6dfa..e24a8cfcfb1 100644
@@ -252,6 +252,11 @@ 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_sysctlbyname(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 */
Signature · 1 line
2.39.1
#0 lore raw

cover 2023 Q1 bsd-user upstreaming: bugfixes and sysctl

This group of patches gets the basic framework for sysctl upstreamed. There's a lot more to translate far too many binary blobs the kernel publishes via sysctls, but I'm leaving those out in the name of simplicity.

There's also a bug fix from Doug Rabson that fixes a long int confusion leading to a trunctation of addresses (oops)

I've added a new command line arg -strict for debugging when you want to stop dead in the tracks when there's something unimplemented (like system calls) rather than trying one's best to cope. It will also let whoever is working on upstreaming to get something working to run it this way and find the missing bits more easily. sysctl happens to be the first unimplemented system call for a dynamically linked hello world.

There's a fix for the -static option, since clang hates -no-pie and needs only -fno-pie.

Finally, I'm changing how I'm upstreaming a little. I'm doing a little deeper dives into our rather chaotic repo to find a couple of authors I might have missed. From here on out, I'll be using the original author's name as the git author. I'll also tag the co-authors better as well when there's multiple people that did something (other than reformat and/or move code around). I've discovered more code moved about than I'd previously known. This seems more in line with standard practice. Also, I've reviewed all these changes, but I don't know if I need to add Reviewed-by: or not, so I've done it for one or two and will make it consistent before the pull request. git log suggests maintainers are inconsistent about this (or I've not discovered the rules they follow).

check-patch gives some line lenght warnings, but should otherwise be OK. There's several static functions that aren't used until the end of the patch series... Not sure the best way to suppress the build warnings there (but since they are just warnings...).

Doug Rabson (1):
bsd-user: Don't truncate the return value from freebsd_syscall

Juergen Lock (2):
bsd-user: sysctl helper funtions: sysctl_name2oid and sysctl_oidfmt
bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants

Kyle Evans (2):
bsd-user: do_freebsd_sysctl helper for sysctl(2)
bsd-user: implement sysctlbyname(2)

Stacey Son (2):
bsd-user: Add sysarch syscall
bsd-user: Two helper routines oidfmt and sysctl_oldcvt

Warner Losh (2):
build: Don't specify -no-pie for --static user-mode programs
bsd-user: Add -strict

 bsd-user/freebsd/os-sys.c     | 432 ++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  21 +-
 bsd-user/main.c               |   5 +-
 bsd-user/qemu.h               |   6 +
 configure                     |   2 +-
 5 files changed, 463 insertions(+), 3 deletions(-)
Signature · 1 line
2.39.1
#1 lore raw

PATCH 1/9 bsd-user: Don't truncate the return value from freebsd_syscall

From: Doug Rabson <[email protected]>

System call return values on FreeBSD are in a register (which is spelled api_long in qemu). This was being assigned into an int variable which causes problems for 64bit targets.

Resolves: https://github.com/qemu-bsd-user/qemu-bsd-user/issues/40

[ Edited commit message for upstreaming into qemu-project ]
---
 bsd-user/freebsd/os-syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bsd-user/freebsd/os-syscall.c +1 -1
index 57996cad8ae..b4a663fc021 100644
@@ -512,7 +512,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
                             abi_long arg8)
 {
     CPUState *cpu = env_cpu(cpu_env);
-    int ret;
+    abi_long ret;
 
     trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
     if (do_strace) {
Signature · 1 line
2.39.1
#21 lore raw

PATCH 8/9 bsd-user: implement sysctlbyname(2)

In reply to Warner Losh

From: Kyle Evans <[email protected]>

do_freebsd_sysctlbyname needs to translate the 'name' back down to a OID so we can intercept the special ones. Do that and call the common wrapper do_freebsd_sysctl_oid.

---
 bsd-user/freebsd/os-sys.c     | 58 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  4 +++
 2 files changed, 62 insertions(+)
bsd-user/freebsd/os-sys.c +58 -0
index 13736936e5f..62c729dfe47 100644
@@ -345,6 +345,64 @@ out:
     return ret;
 }
 
+/*
+ * This syscall was created to make sysctlbyname(3) more efficient.
+ * Unfortunately, because we have to fake some sysctls, we can't do that.
+ */
+abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
+        int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
+        abi_ulong newlen)
+{
+    abi_long ret;
+    void *holdp = NULL, *hnewp = NULL;
+    char *snamep;
+    int oid[CTL_MAXNAME + 2];
+    size_t holdlen, oidplen;
+    abi_ulong oldlen = 0;
+
+    if (oldlenp) {
+        if (get_user_ual(oldlen, oldlenp)) {
+            return -TARGET_EFAULT;
+        }
+    }
+    snamep = lock_user_string(namep);
+    if (snamep == NULL) {
+        return -TARGET_EFAULT;
+    }
+    if (newp) {
+        hnewp = lock_user(VERIFY_READ, newp, newlen, 1);
+        if (hnewp == NULL) {
+            return -TARGET_EFAULT;
+        }
+    }
+    if (oldp) {
+        holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0);
+        if (holdp == NULL) {
+            return -TARGET_EFAULT;
+        }
+    }
+    holdlen = oldlen;
+
+    oidplen = sizeof(oid) / sizeof(int);
+    if (sysctlnametomib(snamep, oid, &oidplen) != 0) {
+        return -TARGET_EINVAL;
+    }
+
+    ret = do_freebsd_sysctl_oid(env, oid, oidplen, holdp, &holdlen, hnewp,
+        newlen);
+
+    if (oldlenp) {
+        put_user_ual(holdlen, oldlenp);
+    }
+    unlock_user(snamep, namep, 0);
+    unlock_user(holdp, oldp, holdlen);
+    if (hnewp) {
+        unlock_user(hnewp, newp, 0);
+    }
+
+    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)
 {
bsd-user/freebsd/os-syscall.c +4 -0
index 20ab3d4d9a1..179a20c304b 100644
@@ -498,6 +498,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd_sysctl(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
         break;
 
+    case TARGET_FREEBSD_NR___sysctlbyname: /* sysctlbyname(2) */
+        ret = do_freebsd_sysctlbyname(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;
Signature · 1 line
2.39.1
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] bsd-user: Don't truncate the return value from freebsd_syscall

In reply to Warner Losh
Quoted 4 lines · Warner Losh

From: Doug Rabson <[email protected]>

System call return values on FreeBSD are in a register (which is spelled api_long in qemu). This was being assigned into an int variable which

s/api/abi/

Quoted 10 lines

causes problems for 64bit targets.

Resolves: https://github.com/qemu-bsd-user/qemu-bsd-user/issues/40

[ Edited commit message for upstreaming into qemu-project ]
---
  bsd-user/freebsd/os-syscall.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

Quoted 14 lines
bsd-user/freebsd/os-syscall.c +1 -1
index 57996cad8ae..b4a663fc021 100644
@@ -512,7 +512,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
                              abi_long arg8)
  {
      CPUState *cpu = env_cpu(cpu_env);
-    int ret;
+    abi_long ret;
 
      trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
      if (do_strace) {
Reviewed-by on v1 3/9
#5 lore raw

Re: [PATCH 3/9] bsd-user: Add sysarch syscall

In reply to Warner Losh
Quoted 4 lines · Warner Losh

From: Stacey Son<[email protected]>

Connect up the sysarch system call.

Quoted 8 lines
---
  bsd-user/freebsd/os-syscall.c | 7 +++++++
  1 file changed, 7 insertions(+)

r~

#28 lore raw
In reply to Warner Losh
Quoted 4 lines · Warner Losh

There's
several static functions that aren't used until the end of the patch series... Not sure the best way to suppress the build warnings there (but since they are just warnings...).

Are they just warnings? --enable-werror is default...

Anyway, I've used

static type G_GNUC_UNUSED function(args...)

in the past to ensure bisection, removing the UNUSED marker when they become used.

r~

#7 lore raw

Re: [PATCH 4/9] bsd-user: Two helper routines oidfmt and sysctl_oldcvt

In reply to Warner Losh
Quoted 22 lines · Warner Losh
bsd-user/freebsd/os-sys.c +22 -0
+static int sysctl_oldcvt(void *holdp, size_t *holdlen, uint32_t kind)
+{
+    switch (kind & CTLTYPE) {
+    case CTLTYPE_INT:
+    case CTLTYPE_UINT:
+        *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
+        break;
+
+#ifdef TARGET_ABI32
+    case CTLTYPE_LONG:
+    case CTLTYPE_ULONG:
+        /*
+         * If the sysctl has a type of long/ulong but seems to be bigger than
+         * these data types, its probably an array.  Double check that its
+         * evenly divisible by the size of long and convert holdp to a series of
+         * 32bit elements instead, adjusting holdlen to the new size.
+         */
+        if ((*holdlen > sizeof(abi_ulong)) &&
+            ((*holdlen % sizeof(abi_ulong)) == 0)) {
+            int array_size = *holdlen / sizeof(long);
+            int i;
+            if (holdp) {

bsd-user/freebsd/os-sys.c line 93

Quoted 10 lines
bsd-user/freebsd/os-sys.c +10 -0
+                for (i = 0; i < array_size; i++) {
+                    ((uint32_t *)holdp)[i] = tswap32(((long *)holdp)[i]);
+                }
+                *holdlen = array_size * sizeof(abi_ulong);
+            } else {
+                *holdlen = sizeof(abi_ulong);
+            }
+        } else {
+            *(uint32_t *)holdp = tswap32(*(long *)holdp);
+            *holdlen = sizeof(uint32_t);

This is totally confusing. Why would it ever be an array? Why is this section the only place we ever assign back into holdlen?

Can you point to anything similar in the freebsd source? The whole thing is pretty hard to track, starting from sys/kern/kern_sysctl.c.

r~

#29 lore raw
In reply to Richard Henderson
Quoted 7 lines · Richard Henderson
Quoted 3 lines · Warner Losh

There's
several static functions that aren't used until the end of the patch series... Not sure the best way to suppress the build warnings there

(but since

Quoted 1 line

they are just warnings...).

Quoted 9 lines

Are they just warnings? --enable-werror is default...

Anyway, I've used

static type G_GNUC_UNUSED function(args...)

in the past to ensure bisection, removing the UNUSED marker when they become used.

I like that suggestion, and it's easy to implement. Thanks for this and the reviews so far. I've
updated things with your comments, but will give it another day or two before I send v2 for
others to comment.

Warner

#13 lore raw

Re: [PATCH 6/9] bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants

In reply to Warner Losh

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 164

Quoted 13 lines · Warner Losh
bsd-user/freebsd/os-sys.c +13 -0
+    /* Handle some arch/emulator dependent sysctl()'s here. */
+    switch (snamep[0]) {
+#if defined(TARGET_PPC) || defined(TARGET_PPC64)
+    case CTL_MACHDEP:
+        switch (snamep[1]) {
+        case 1:    /* CPU_CACHELINE */
+            holdlen = sizeof(uint32_t);
+            (*(uint32_t *)holdp) = tswap32(env->dcache_line_size);
+            ret = 0;
+            goto out;
+        }
+        break;
+#endif

abi_int instead of uint32_t.

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 194

Quoted 7 lines
bsd-user/freebsd/os-sys.c +7 -0
+    case CTL_HW:
+        switch (snamep[1]) {
+        case HW_MACHINE:
+            holdlen = sizeof(TARGET_HW_MACHINE);
+            if (holdp) {
+                strlcpy(holdp, TARGET_HW_MACHINE, oldlen);
+            }

What's the semantics here when oldlen < sizeof(literal)?
I was expecting something like sysctl_old_kernel.
It would probably be good to create a number of small helper functions per type.

Quoted 1 line

+#ifdef ARM_FEATURE_VFP /* XXX FIXME XXX */

This define has been removed, so this part is dead,

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 225

Quoted 8 lines
bsd-user/freebsd/os-sys.c +8 -0
+                if (env->features & ((1ULL << ARM_FEATURE_VFP)|
+                                     (1ULL << ARM_FEATURE_VFP3)|
+                                     (1ULL << ARM_FEATURE_VFP4)))
+                    *(int32_t *)holdp = 1;
+                else
+                    *(int32_t *)holdp = 0;
+#else
+                *(int32_t *)holdp = 1;

and this is not right.

You're looking for

    ARMCPU *cpu = env_archcpu(env);
    *(abi_int *)holdp = cpu_isar_feature(aa32_vfp, cpu);
Quoted 6 lines
bsd-user/freebsd/os-sys.c +6 -0
+#if TARGET_ABI_BITS != HOST_LONG_BITS
+        case HW_PHYSMEM:
+        case HW_USERMEM:
+        case HW_REALMEM:
+            holdlen = sizeof(abi_ulong);
+            ret = 0;

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 249

Quoted 10 lines
bsd-user/freebsd/os-sys.c +10 -0
+
+            if (oldlen) {
+                int mib[2] = {snamep[0], snamep[1]};
+                unsigned long lvalue;
+                size_t len = sizeof(lvalue);
+
+                if (sysctl(mib, 2, &lvalue, &len, NULL, 0) == -1) {
+                    ret = -1;
+                } else {
+                    if (((unsigned long)maxmem) < lvalue) {

Where is maxmem defined?
Why are these numbers only special-cased for TARGET_ABI_BITS != HOST_LONG_BITS?

Quoted 8 lines
bsd-user/freebsd/os-sys.c +8 -0
+            static int oid_hw_pagesizes;
+
+            if (!oid_hw_availpages) {
+                int real_oid[CTL_MAXNAME + 2];
+                size_t len = sizeof(real_oid) / sizeof(int);
+
+                if (sysctlnametomib("hw.availpages", real_oid, &len) >= 0) {
+                    oid_hw_availpages = real_oid[1];

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 278

Quoted 10 lines
bsd-user/freebsd/os-sys.c +10 -0
+                }
+            }
+            if (!oid_hw_pagesizes) {
+                int real_oid[CTL_MAXNAME + 2];
+                size_t len = sizeof(real_oid) / sizeof(int);
+
+                if (sysctlnametomib("hw.pagesizes", real_oid, &len) >= 0) {
+                    oid_hw_pagesizes = real_oid[1];
+                }
+            }

Host pagesizes are not relevant to the guest.

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 289

Quoted 11 lines
bsd-user/freebsd/os-sys.c +11 -0
+
+            if (oid_hw_availpages && snamep[1] == oid_hw_availpages) {
+                long lvalue;
+                size_t len = sizeof(lvalue);
+
+                if (sysctlbyname("hw.availpages", &lvalue, &len, NULL, 0) == -1) {
+                    ret = -1;
+                } else {
+                    if (oldlen) {
+#if TARGET_ABI_BITS != HOST_LONG_BITS
+                        abi_ulong maxpages = maxmem / (abi_ulong)getpagesize();

Again with maxmem...

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 294

Quoted 5 lines
bsd-user/freebsd/os-sys.c +5 -0
+                        if (((unsigned long)maxpages) < lvalue) {
+                            lvalue = maxpages;
+                        }
+#endif
+                        (*(abi_ulong *)holdp) = tswapal((abi_ulong)lvalue);

I would expect a 64-bit guest to rescale the result for TARGET_PAGE_SIZE != getpagesize().

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 304

Quoted 10 lines
bsd-user/freebsd/os-sys.c +10 -0
+                    }
+                    holdlen = sizeof(abi_ulong);
+                    ret = 0;
+                }
+                goto out;
+            }
+
+            if (oid_hw_pagesizes && snamep[1] == oid_hw_pagesizes) {
+                if (oldlen) {
+                    (*(abi_ulong *)holdp) = tswapal((abi_ulong)getpagesize());

Indeed, this needs TARGET_PAGE_SIZE.

bsd-user/qemu.h bool is_error(abi_long ret); · line 259

Quoted 13 lines
bsd-user/qemu.h +5 -0
index 0ceecfb6dfa..e24a8cfcfb1 100644
@@ -252,6 +252,11 @@ 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_sysctlbyname(CPUArchState *env, abi_ulong namep,
+        int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
+        abi_ulong newlen);

These belong to different patches.

r~

#19 lore raw

Re: [PATCH 7/9] bsd-user: do_freebsd_sysctl helper for sysctl(2)

In reply to Warner Losh
Quoted 4 lines · Warner Losh
bsd-user/freebsd/os-sys.c +4 -0
+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;

bsd-user/freebsd/os-sys.c out: · line 361

Quoted 10 lines
bsd-user/freebsd/os-sys.c +10 -0
+    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;
+
+    if (oldlenp) {
+        if (get_user_ual(oldlen, oldlenp)) {
+            return -TARGET_EFAULT;
+        }
+    }

You need to check for write early. Either access_ok, or lock_user.

bsd-user/freebsd/os-sys.c out: · line 381

Quoted 3 lines
bsd-user/freebsd/os-sys.c +3 -0
+    for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++) {
+        *q++ = tswap32(*p);
+    }

Why the inconsistent increments?

Quoted 1 line

+ unlock_user(holdp, oldp, holdlen);

Usually we don't want writeback on error.

r~

#22 lore raw

Re: [PATCH 8/9] bsd-user: implement sysctlbyname(2)

In reply to Warner Losh
Quoted 18 lines · Warner Losh

From: Kyle Evans <[email protected]>

do_freebsd_sysctlbyname needs to translate the 'name' back down to a OID so we can intercept the special ones. Do that and call the common wrapper do_freebsd_sysctl_oid.

---
  bsd-user/freebsd/os-sys.c     | 58 +++++++++++++++++++++++++++++++++++
  bsd-user/freebsd/os-syscall.c |  4 +++
  2 files changed, 62 insertions(+)
bsd-user/freebsd/os-sys.c +0 -0
index 13736936e5f..62c729dfe47 100644

bsd-user/freebsd/os-sys.c out: · line 350

Quoted 6 lines
bsd-user/freebsd/os-sys.c +3 -0
@@ -345,6 +345,64 @@ out:
      return ret;
  }
 
+/*
+ * This syscall was created to make sysctlbyname(3) more efficient.
+ * Unfortunately, because we have to fake some sysctls, we can't do that.

Can't do what? Directly use sysctlbyname?

bsd-user/freebsd/os-sys.c out: · line 366

Quoted 4 lines
bsd-user/freebsd/os-sys.c +4 -0
+    if (oldlenp) {
+        if (get_user_ual(oldlen, oldlenp)) {
+            return -TARGET_EFAULT;
+        }

Same comment about verifying write early.

bsd-user/freebsd/os-sys.c out: · line 398

Quoted 1 line
bsd-user/freebsd/os-sys.c +1 -0
+    unlock_user(holdp, oldp, holdlen);

And writeback vs error.

r~

#26 lore raw

Re: [PATCH 9/9] bsd-user: Add -strict

In reply to Warner Losh
Quoted 16 lines · Warner Losh

Most of the time, it's useful to make our best effort, but sometimes we want to know right away when we don't implement something. First place we use it is for unknown syscalls.

---
  bsd-user/freebsd/os-syscall.c | 4 ++++
  bsd-user/main.c               | 5 ++++-
  bsd-user/qemu.h               | 1 +
  3 files changed, 9 insertions(+), 1 deletion(-)
bsd-user/freebsd/os-syscall.c +0 -0
index 179a20c304b..e2b26ecb8dd 100644

bsd-user/freebsd/os-syscall.c static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, · line 514

Quoted 7 lines
bsd-user/freebsd/os-syscall.c +4 -0
@@ -508,6 +508,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
 
      default:
          qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
+        if (bsd_user_strict) {
+            printf("Unimplemented system call %d\n", num);
+            abort();
+        }

I don't like the raw printf, even if you did write to stderr. Perhaps just the abort, letting the error message be handled by qemu_log?

bsd-user/freebsd/os-syscall.c int main(int argc, char **argv) · line 401

Quoted 6 lines
bsd-user/freebsd/os-syscall.c +2 -0
@@ -396,6 +397,8 @@ int main(int argc, char **argv)
              trace_opt_parse(optarg);
          } else if (!strcmp(r, "0")) {
              argv0 = argv[optind++];
+        } else if (!strcmp(r, "strict")) {
+            bsd_user_strict = true;

Perhaps force LOG_UNIMP? Without -D, you'll get the qemu_log above to stderr.

r~

#14 lore raw

Re: [PATCH 6/9] bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants

In reply to Richard Henderson
Quoted 17 lines · Richard Henderson

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 164

Quoted 13 lines · Warner Losh
bsd-user/freebsd/os-sys.c +13 -0
+    /* Handle some arch/emulator dependent sysctl()'s here. */
+    switch (snamep[0]) {
+#if defined(TARGET_PPC) || defined(TARGET_PPC64)
+    case CTL_MACHDEP:
+        switch (snamep[1]) {
+        case 1:    /* CPU_CACHELINE */
+            holdlen = sizeof(uint32_t);
+            (*(uint32_t *)holdp) = tswap32(env->dcache_line_size);
+            ret = 0;
+            goto out;
+        }
+        break;
+#endif

abi_int instead of uint32_t.

Indeed. Thanks! Turns out, though, there's no upstream support for PPC for bsd-user, so I'll drop this hunk of the patch... I thought I'd done it already when
preparing things...

Quoted 8 lines

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 194

Quoted 7 lines
bsd-user/freebsd/os-sys.c +7 -0
+    case CTL_HW:
+        switch (snamep[1]) {
+        case HW_MACHINE:
+            holdlen = sizeof(TARGET_HW_MACHINE);
+            if (holdp) {
+                strlcpy(holdp, TARGET_HW_MACHINE, oldlen);
+            }
Quoted 9 lines

What's the semantics here when oldlen < sizeof(literal)?
I was expecting something like sysctl_old_kernel.
It would probably be good to create a number of small helper functions per type.

Quoted 1 line

+#ifdef ARM_FEATURE_VFP /* XXX FIXME XXX */

This define has been removed, so this part is dead,

Yup. I added it as a hack... I kept this in because I knew I'd find the right way to
do this :)

Quoted 9 lines

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 225

Quoted 8 lines
bsd-user/freebsd/os-sys.c +8 -0
+                if (env->features & ((1ULL << ARM_FEATURE_VFP)|
+                                     (1ULL << ARM_FEATURE_VFP3)|
+                                     (1ULL << ARM_FEATURE_VFP4)))
+                    *(int32_t *)holdp = 1;
+                else
+                    *(int32_t *)holdp = 0;
+#else
+                *(int32_t *)holdp = 1;
Quoted 7 lines

and this is not right.

You're looking for

     ARMCPU *cpu = env_archcpu(env);
     *(abi_int *)holdp = cpu_isar_feature(aa32_vfp, cpu);

Yes. That looks right to me... I was having trouble finding it and the merge it came
in on was bigger than normal, and I put the above kludge in to get through it and
then never followed up...

Quoted 13 lines
Quoted 13 lines
bsd-user/freebsd/os-sys.c +13 -0
+#if TARGET_ABI_BITS != HOST_LONG_BITS
+        case HW_PHYSMEM:
+        case HW_USERMEM:
+        case HW_REALMEM:
+            holdlen = sizeof(abi_ulong);
+            ret = 0;
+
+            if (oldlen) {
+                int mib[2] = {snamep[0], snamep[1]};
+                unsigned long lvalue;
+                size_t len = sizeof(lvalue);
+
+                if (sysctl(mib, 2, &lvalue, &len, NULL, 0) == -1) {
Quoted 9 lines

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 249

Quoted 3 lines
bsd-user/freebsd/os-sys.c +3 -0
+                    ret = -1;
+                } else {
+                    if (((unsigned long)maxmem) < lvalue) {

Where is maxmem defined?
Why are these numbers only special-cased for TARGET_ABI_BITS != HOST_LONG_BITS?

maxmem is defined earlier in this patch:

+#if TARGET_ABI_BITS != HOST_LONG_BITS
+ const abi_ulong maxmem = -0x100c000;

but I'm not at all sure how that number was arrived at... It's a little less than ULONG_MAX is all I can say for sure.

As to why it's a special case only sometimes, I believe that it's there for 32-bit
targets running on 64-bit hosts so that we return a sane amount of memory because
64-bit hosts can have > 4GB of ram... I'm not 100% sure of this, and it would
likely be wrong for 32-bit host and 64-bit target, but that case isn't supported at
all by the bsd-user project (though in the past it may have been, we no longer
built even 32 on 32 target/host emulation).

Quoted 16 lines

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp)

Quoted 7 lines
bsd-user/freebsd/os-sys.c +7 -0
+            static int oid_hw_pagesizes;
+
+            if (!oid_hw_availpages) {
+                int real_oid[CTL_MAXNAME + 2];
+                size_t len = sizeof(real_oid) / sizeof(int);
+
+                if (sysctlnametomib("hw.availpages", real_oid, &len) >=

0) {

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp)

Quoted 8 lines
bsd-user/freebsd/os-sys.c +8 -0
+                    oid_hw_availpages = real_oid[1];
+                }
+            }
+            if (!oid_hw_pagesizes) {
+                int real_oid[CTL_MAXNAME + 2];
+                size_t len = sizeof(real_oid) / sizeof(int);
+
+                if (sysctlnametomib("hw.pagesizes", real_oid, &len) >=
Quoted 7 lines

0) {

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp) · line 278

Quoted 3 lines
bsd-user/freebsd/os-sys.c +3 -0
+                    oid_hw_pagesizes = real_oid[1];
+                }
+            }

Host pagesizes are not relevant to the guest.

Yes. I noticed after I submitted this that I wondered if I should be using the
host's notion, or the softmmu's notion of page size... But it's clear from the
other comments below, that it should be TARGET_PAGE_SIZE for all of these.

Quoted 21 lines

+

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp)

Quoted 5 lines
bsd-user/freebsd/os-sys.c +5 -0
+            if (oid_hw_availpages && snamep[1] == oid_hw_availpages) {
+                long lvalue;
+                size_t len = sizeof(lvalue);
+
+                if (sysctlbyname("hw.availpages", &lvalue, &len, NULL,

0) == -1) {

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp)

Quoted 5 lines
bsd-user/freebsd/os-sys.c +5 -0
+                    ret = -1;
+                } else {
+                    if (oldlen) {
+#if TARGET_ABI_BITS != HOST_LONG_BITS
+                        abi_ulong maxpages = maxmem /

(abi_ulong)getpagesize();

Again with maxmem...

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp)

Quoted 5 lines
bsd-user/freebsd/os-sys.c +5 -0
+                        if (((unsigned long)maxpages) < lvalue) {
+                            lvalue = maxpages;
+                        }
+#endif
+                        (*(abi_ulong *)holdp) =
Quoted 5 lines

tswapal((abi_ulong)lvalue);

I would expect a 64-bit guest to rescale the result for TARGET_PAGE_SIZE != getpagesize().

I would too. I suspect that the reason this is here like this is that an attempt
was being made to handle it, but since TARGET_PAGE_SIZE == getpagesize() on all hosts / target pairs until very recently (with the 16k arm64 kernels), this was
a latent bug in the code and I should fix it before my next submission. And aarch64
hosts for this are quite rare (most people use bsd-user on amd64 hosts to build for
all the other architectures).

Quoted 10 lines

bsd-user/freebsd/os-sys.c static inline void sysctl_oidfmt(uint32_t *holdp)

Quoted 10 lines
bsd-user/freebsd/os-sys.c +10 -0
+                    }
+                    holdlen = sizeof(abi_ulong);
+                    ret = 0;
+                }
+                goto out;
+            }
+
+            if (oid_hw_pagesizes && snamep[1] == oid_hw_pagesizes) {
+                if (oldlen) {
+                    (*(abi_ulong *)holdp) =
Quoted 4 lines

tswapal((abi_ulong)getpagesize());

Indeed, this needs TARGET_PAGE_SIZE.

That makes things somewhat simpler for rearranging here...

Quoted 10 lines

bsd-user/qemu.h bool is_error(abi_long ret); · line 255

Quoted 9 lines
bsd-user/qemu.h +1 -0
index 0ceecfb6dfa..e24a8cfcfb1 100644
@@ -252,6 +252,11 @@ 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,

Quoted 9 lines

bsd-user/qemu.h bool is_error(abi_long ret); · line 256

Quoted 1 line
bsd-user/qemu.h +1 -0
+        abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong

newlen);

bsd-user/qemu.h bool is_error(abi_long ret); · line 258

Quoted 2 lines
bsd-user/qemu.h +2 -0
+abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
+        int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong

newp,

bsd-user/qemu.h bool is_error(abi_long ret); · line 259

Quoted 1 line
bsd-user/qemu.h +1 -0
+        abi_ulong newlen);

These belong to different patches.

Oh yes. I'll take care of that... They were, but then they weren't and then i thought I'd
fixed that (a bit of a rebase misadventure when re-ordering patches occurred and
I thought I'd fixed it entirely...)

Thanks for helping me clear a few things up in the code that my understanding was
hazy, but I wasn't sure where it was hazy and it turns out these comments clear the haze
for me.

Warner

Quoted 3 lines

r~

#15 lore raw

Re: [PATCH 6/9] bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants

Quoted 9 lines · Warner Losh

maxmem is defined earlier in this patch:

+#if TARGET_ABI_BITS != HOST_LONG_BITS
+    const abi_ulong maxmem = -0x100c000;

but I'm not at all sure how that number was arrived at... It's a little less than ULONG_MAX is all I can say for sure.

Quoted 6 lines

As to why it's a special case only sometimes, I believe that it's there for 32-bit targets running on 64-bit hosts so that we return a sane amount of memory because 64-bit hosts can have > 4GB of ram... I'm not 100% sure of this, and it would likely be wrong for 32-bit host and 64-bit target, but that case isn't supported at all by the bsd-user project (though in the past it may have been, we no longer built even 32 on 32 target/host emulation).

Perhaps you're looking for reserved_va? I.e. the max va the guest is limited to?

Or, given this is a system-wide number of pages, not per-process, and given the types involved, cap at UINT32_MAX?

Quoted 9 lines
    I would expect a 64-bit guest to rescale the result for TARGET_PAGE_SIZE != getpagesize().

I would too. I suspect that the reason this is here like this is that an attempt was being made to handle it, but since TARGET_PAGE_SIZE == getpagesize() on all hosts / target pairs until very recently (with the 16k arm64 kernels), this was a latent bug in the code and I should fix it before my next submission. And aarch64 hosts for this are quite rare (most people use bsd-user on amd64 hosts to build for all the other architectures).

Ok. When you do this, remember muldiv64.

r~

#16 lore raw

Re: [PATCH 6/9] bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants

Quoted 20 lines · Richard Henderson
Quoted 10 lines · Warner Losh

maxmem is defined earlier in this patch:

bsd-user/freebsd/os-sys.c +2 -0
+#if TARGET_ABI_BITS != HOST_LONG_BITS
+    const abi_ulong maxmem = -0x100c000;

but I'm not at all sure how that number was arrived at... It's a little less than ULONG_MAX is all I can say for sure.

As to why it's a special case only sometimes, I believe that it's there

for 32-bit

Quoted 1 line

targets running on 64-bit hosts so that we return a sane amount of

memory because

Quoted 1 line

64-bit hosts can have > 4GB of ram... I'm not 100% sure of this, and it

would

Quoted 1 line

likely be wrong for 32-bit host and 64-bit target, but that case isn't

supported at

Quoted 1 line

all by the bsd-user project (though in the past it may have been, we no

longer

Quoted 9 lines
Quoted 1 line

built even 32 on 32 target/host emulation).

Perhaps you're looking for reserved_va? I.e. the max va the guest is limited to?

Or, given this is a system-wide number of pages, not per-process, and given the types
involved, cap at UINT32_MAX?

I think that I'll use UINT32_MAX - <magic number> + 1 here. I'll explain that <magic number>
was empirically determined. I'm looking at all repos to see if there's a better explanation there.

Quoted 9 lines
Quoted 1 line

I would expect a 64-bit guest to rescale the result for

TARGET_PAGE_SIZE != getpagesize().

Quoted 3 lines

I would too. I suspect that the reason this is here like this is that an

attempt

Quoted 1 line

was being made to handle it, but since TARGET_PAGE_SIZE == getpagesize()

on

Quoted 1 line

all hosts / target pairs until very recently (with the 16k arm64

Quoted 9 lines

kernels), this was

Quoted 1 line

a latent bug in the code and I should fix it before my next submission.

And aarch64

Quoted 1 line

hosts for this are quite rare (most people use bsd-user on amd64 hosts

to build for

Quoted 1 line

all the other architectures).

Ok. When you do this, remember muldiv64.

I was going to do something like:

+ if (host_page_size != TARGET_PAGE_SIZE) {
+ if (host_page_size > TARGET_PAGE_SIZE) {
+ /* Scale up */
+ pages *= host_page_size / TARGET_PAGE_SIZE;
+ } else {
+ /* Scale down with truncation */
+ pages /= TARGET_PAGE_SIZE / host_page_size;
+ }
+ }

in a helper function. Does multdiv64 replace that? It's currently unused in both linux-user
and bsd-user. The above does things in a known-good order (or at least that's my belief,
even after 30 years C surprises me).

Warner

#17 lore raw

Re: [PATCH 6/9] bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants

Quoted 12 lines · Warner Losh

I was going to do something like:

+    if (host_page_size != TARGET_PAGE_SIZE) {
+        if (host_page_size > TARGET_PAGE_SIZE) {
+            /* Scale up */
+            pages *= host_page_size / TARGET_PAGE_SIZE;
+        } else {
+            /* Scale down with truncation */
+            pages /= TARGET_PAGE_SIZE / host_page_size;
+        }
+    }
in a helper function. Does multdiv64 replace that?

Yes, it uses a 128-bit intermediate result, so no overflow. Obviously the above works as well, but perhaps premature optimization.

r~

#8 lore raw

Re: [PATCH 4/9] bsd-user: Two helper routines oidfmt and sysctl_oldcvt

In reply to Richard Henderson
Quoted 38 lines · Richard Henderson

bsd-user/freebsd/os-sys.c

Quoted 13 lines · Warner Losh
bsd-user/freebsd/os-sys.c +13 -0
+static int sysctl_oldcvt(void *holdp, size_t *holdlen, uint32_t kind)
+{
+    switch (kind & CTLTYPE) {
+    case CTLTYPE_INT:
+    case CTLTYPE_UINT:
+        *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
+        break;
+
+#ifdef TARGET_ABI32
+    case CTLTYPE_LONG:
+    case CTLTYPE_ULONG:
+        /*
+         * If the sysctl has a type of long/ulong but seems to be

bigger than

Quoted 1 line

+ * these data types, its probably an array. Double check that

its

Quoted 1 line

+ * evenly divisible by the size of long and convert holdp to a

series of

bsd-user/freebsd/os-sys.c

Quoted 9 lines
bsd-user/freebsd/os-sys.c +9 -0
+         * 32bit elements instead, adjusting holdlen to the new size.
+         */
+        if ((*holdlen > sizeof(abi_ulong)) &&
+            ((*holdlen % sizeof(abi_ulong)) == 0)) {
+            int array_size = *holdlen / sizeof(long);
+            int i;
+            if (holdp) {
+                for (i = 0; i < array_size; i++) {
+                    ((uint32_t *)holdp)[i] = tswap32(((long

*)holdp)[i]);

bsd-user/freebsd/os-sys.c line 93

Quoted 8 lines
bsd-user/freebsd/os-sys.c +8 -0
+                }
+                *holdlen = array_size * sizeof(abi_ulong);
+            } else {
+                *holdlen = sizeof(abi_ulong);
+            }
+        } else {
+            *(uint32_t *)holdp = tswap32(*(long *)holdp);
+            *holdlen = sizeof(uint32_t);
Quoted 7 lines

This is totally confusing. Why would it ever be an array? Why is this section the only place we ever assign back into holdlen?

Can you point to anything similar in the freebsd source? The whole thing is pretty hard
to track, starting from sys/kern/kern_sysctl.c.

I need to understand this... I've been looking for where we export an array, and we just don't.

I've asked the original author who said it had something to do with different size longs. I'll
look into that a bit and get back to this.

I think we assign back into holdlen in a weird attempt adjust for the difference of LONG between
the two. But I'm not sure that that's where we should assign.

Warner

#23 lore raw

Re: [PATCH 8/9] bsd-user: implement sysctlbyname(2)

In reply to Richard Henderson
Quoted 23 lines · Richard Henderson
Quoted 21 lines · Warner Losh

From: Kyle Evans <[email protected]>

do_freebsd_sysctlbyname needs to translate the 'name' back down to a OID so we can intercept the special ones. Do that and call the common wrapper do_freebsd_sysctl_oid.

---
  bsd-user/freebsd/os-sys.c     | 58 +++++++++++++++++++++++++++++++++++
  bsd-user/freebsd/os-syscall.c |  4 +++
  2 files changed, 62 insertions(+)
bsd-user/freebsd/os-sys.c +0 -0
index 13736936e5f..62c729dfe47 100644
@@ -345,6 +345,64 @@ out:
      return ret;
  }
 
Quoted 6 lines

bsd-user/freebsd/os-sys.c out: · line 350

Quoted 3 lines
bsd-user/freebsd/os-sys.c +3 -0
+/*
+ * This syscall was created to make sysctlbyname(3) more efficient.
+ * Unfortunately, because we have to fake some sysctls, we can't do that.

Can't do what? Directly use sysctlbyname?

How about:

/*
* This syscall was created to make sysctlbyname(3) more efficient, but
* we can't really provide it in bsd-user. Notably, we must always translate
* the names independently since some sysctl values have to be faked
* for the target environment, so it still has to break down to two syscalls
* for the underlying implementation.
*/

Quoted 13 lines

bsd-user/freebsd/os-sys.c out: · line 366

Quoted 4 lines
bsd-user/freebsd/os-sys.c +4 -0
+    if (oldlenp) {
+        if (get_user_ual(oldlen, oldlenp)) {
+            return -TARGET_EFAULT;
+        }

Same comment about verifying write early.

bsd-user/freebsd/os-sys.c out: · line 398

Quoted 1 line
bsd-user/freebsd/os-sys.c +1 -0
+    unlock_user(holdp, oldp, holdlen);

And writeback vs error.

r~

#24 lore raw

Re: [PATCH 8/9] bsd-user: implement sysctlbyname(2)

Quoted 9 lines · Kyle Evans
Quoted 6 lines
Quoted 3 lines

+/*
+ * This syscall was created to make sysctlbyname(3) more efficient.
+ * Unfortunately, because we have to fake some sysctls, we can't do that.

Can't do what? Directly use sysctlbyname?

How about:

Quoted 7 lines

/*
* This syscall was created to make sysctlbyname(3) more efficient, but
* we can't really provide it in bsd-user. Notably, we must always translate
* the names independently since some sysctl values have to be faked
* for the target environment, so it still has to break down to two syscalls
* for the underlying implementation.
*/

Better, thanks.

r~

#9 lore raw

Re: [PATCH 4/9] bsd-user: Two helper routines oidfmt and sysctl_oldcvt

In reply to Warner Losh

Hey Richard

Thanks for the very interesting question... This kept me up...

Kyle,

Please double check what I've written below to make sure I've not missed anything.
This might well be the source of some of the weird errors we're seeing on some
ports, but sysctl is rare enough I'm guessing that any of the overflows are in the
end benign.

Quoted 54 lines · Warner Losh
Quoted 38 lines · Richard Henderson

bsd-user/freebsd/os-sys.c

Quoted 13 lines · Warner Losh
bsd-user/freebsd/os-sys.c +13 -0
+static int sysctl_oldcvt(void *holdp, size_t *holdlen, uint32_t kind)
+{
+    switch (kind & CTLTYPE) {
+    case CTLTYPE_INT:
+    case CTLTYPE_UINT:
+        *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
+        break;
+
+#ifdef TARGET_ABI32
+    case CTLTYPE_LONG:
+    case CTLTYPE_ULONG:
+        /*
+         * If the sysctl has a type of long/ulong but seems to be

bigger than

Quoted 1 line

+ * these data types, its probably an array. Double check that

its

Quoted 1 line

+ * evenly divisible by the size of long and convert holdp to a

series of

bsd-user/freebsd/os-sys.c

Quoted 9 lines
bsd-user/freebsd/os-sys.c +9 -0
+         * 32bit elements instead, adjusting holdlen to the new size.
+         */
+        if ((*holdlen > sizeof(abi_ulong)) &&
+            ((*holdlen % sizeof(abi_ulong)) == 0)) {
+            int array_size = *holdlen / sizeof(long);
+            int i;
+            if (holdp) {
+                for (i = 0; i < array_size; i++) {
+                    ((uint32_t *)holdp)[i] = tswap32(((long

*)holdp)[i]);

bsd-user/freebsd/os-sys.c line 93

Quoted 8 lines
bsd-user/freebsd/os-sys.c +8 -0
+                }
+                *holdlen = array_size * sizeof(abi_ulong);
+            } else {
+                *holdlen = sizeof(abi_ulong);
+            }
+        } else {
+            *(uint32_t *)holdp = tswap32(*(long *)holdp);
+            *holdlen = sizeof(uint32_t);
Quoted 7 lines

This is totally confusing. Why would it ever be an array? Why is this section the only place we ever assign back into holdlen?

Can you point to anything similar in the freebsd source? The whole thing is pretty hard
to track, starting from sys/kern/kern_sysctl.c.

I need to understand this... I've been looking for where we export an array, and we just don't.

Quoted 8 lines

I've asked the original author who said it had something to do with different size longs. I'll
look into that a bit and get back to this.

I think we assign back into holdlen in a weird attempt adjust for the difference of LONG between
the two. But I'm not sure that that's where we should assign.

OK. I understand what's going on. If you look at kern_sysctl.c sysctl_old_ddb or
sbin/sysctl/sysctl.c show_var, you'll see that these values canbe arrays. This code
only implements the array part for long and ulong, most likely because that's
all that was encountered in the field.

So the code is right, as far as it goes.... But if the value is bigger
than a long, it
will be truncated, which strikes me as a rather weird thing to do since most longs
are for sizes of things, so I'd think it would be better to saturate.

We also adjust the length here because the host's memory requirements are larger than tha targets. This also means that we're likely returning an error for long/ulong fetches since the target would pass in 4 and the host would want 8, and would return ENOMEM. There's no code to cope with this at all, but I think there needs to be a temporary host buffer that's then copied to the target buffer once it's converted. So I need to write that code.

Also, this code doesn't handle the newer types that FreeBSD has grown in the last few years: _{S,U}{8,16,32,64}. At least those are fixed between the two different ABIs that freebsd supports (ILP32 and LP64).

Also, there's a size issue. *holdlen is a size_t, so we need to do a similar brokering for ABI32 targets. The interface is such that we need to read/write
this variable because that's what the kernel is doing (reading it to make sure
it's big enough, and then writing it to the actual size).

Also (not relevant to this patch), we must not set sysctls very often. newp needs similar treatment tooldp (except the reverse direction), but isn't getting any of the tswaptreatment, so it's broken for long/ulong types as well
as on powerpc which we have out-of-tree now and is the only big-endian port we have left.

tl;dr: I think I'm going to have to do a bit of a rewrite here...

Warner

#10 lore raw

Re: [PATCH 4/9] bsd-user: Two helper routines oidfmt and sysctl_oldcvt

Quoted 105 lines · Warner Losh

Hey Richard

Thanks for the very interesting question... This kept me up...

Kyle,

Please double check what I've written below to make sure I've not missed anything.
This might well be the source of some of the weird errors we're seeing on some
ports, but sysctl is rare enough I'm guessing that any of the overflows are in the
end benign.

Quoted 54 lines · Warner Losh
Quoted 38 lines · Richard Henderson

bsd-user/freebsd/os-sys.c

Quoted 13 lines · Warner Losh
bsd-user/freebsd/os-sys.c +13 -0
+static int sysctl_oldcvt(void *holdp, size_t *holdlen, uint32_t kind)
+{
+    switch (kind & CTLTYPE) {
+    case CTLTYPE_INT:
+    case CTLTYPE_UINT:
+        *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
+        break;
+
+#ifdef TARGET_ABI32
+    case CTLTYPE_LONG:
+    case CTLTYPE_ULONG:
+        /*
+         * If the sysctl has a type of long/ulong but seems to be

bigger than

Quoted 1 line

+ * these data types, its probably an array. Double check

that its

Quoted 1 line

+ * evenly divisible by the size of long and convert holdp to

a series of

bsd-user/freebsd/os-sys.c

Quoted 9 lines
bsd-user/freebsd/os-sys.c +9 -0
+         * 32bit elements instead, adjusting holdlen to the new size.
+         */
+        if ((*holdlen > sizeof(abi_ulong)) &&
+            ((*holdlen % sizeof(abi_ulong)) == 0)) {
+            int array_size = *holdlen / sizeof(long);
+            int i;
+            if (holdp) {
+                for (i = 0; i < array_size; i++) {
+                    ((uint32_t *)holdp)[i] = tswap32(((long

*)holdp)[i]);

bsd-user/freebsd/os-sys.c line 93

Quoted 8 lines
bsd-user/freebsd/os-sys.c +8 -0
+                }
+                *holdlen = array_size * sizeof(abi_ulong);
+            } else {
+                *holdlen = sizeof(abi_ulong);
+            }
+        } else {
+            *(uint32_t *)holdp = tswap32(*(long *)holdp);
+            *holdlen = sizeof(uint32_t);
Quoted 7 lines

This is totally confusing. Why would it ever be an array? Why is this section the only place we ever assign back into holdlen?

Can you point to anything similar in the freebsd source? The whole thing is pretty hard
to track, starting from sys/kern/kern_sysctl.c.

I need to understand this... I've been looking for where we export an array, and we just don't.

Quoted 8 lines

I've asked the original author who said it had something to do with different size longs. I'll
look into that a bit and get back to this.

I think we assign back into holdlen in a weird attempt adjust for the difference of LONG between
the two. But I'm not sure that that's where we should assign.

OK. I understand what's going on. If you look at kern_sysctl.c sysctl_old_ddb or
sbin/sysctl/sysctl.c show_var, you'll see that these values canbe arrays. This code
only implements the array part for long and ulong, most likely because that's
all that was encountered in the field.

So the code is right, as far as it goes.... But if the value is bigger
than a long, it
will be truncated, which strikes me as a rather weird thing to do since most longs
are for sizes of things, so I'd think it would be better to saturate.

We also adjust the length here because the host's memory requirements are larger than tha targets. This also means that we're likely returning an error for long/ulong fetches since the target would pass in 4 and the host would want 8, and would return ENOMEM. There's no code to cope with this at all, but I think there needs to be a temporary host buffer that's then copied to the target buffer once it's converted. So I need to write that code.

Also, this code doesn't handle the newer types that FreeBSD has grown in the last few years: _{S,U}{8,16,32,64}. At least those are fixed between the two different ABIs that freebsd supports (ILP32 and LP64).

Quoted 8 lines

Also, there's a size issue. *holdlen is a size_t, so we need to do a similar
brokering for ABI32 targets. The interface is such that we need to read/write
this variable because that's what the kernel is doing (reading it to make sure
it's big enough, and then writing it to the actual size).

Actually, this issue isn't an issue because, modulo bugs, the callers of sysctl_freebsd_oid() handle it.

Quoted 11 lines

Also (not relevant to this patch), we must not set sysctls very often. newp needs similar treatment tooldp (except the reverse direction), but isn't getting any of the tswaptreatment, so it's broken for long/ulong types as well
as on powerpc which we have out-of-tree now and is the only big-endian port we have left.

tl;dr: I think I'm going to have to do a bit of a rewrite here...

Warner

#20 lore raw

Re: [PATCH 7/9] bsd-user: do_freebsd_sysctl helper for sysctl(2)

In reply to Richard Henderson
Quoted 20 lines · Richard Henderson
Quoted 1 line · Warner Losh

+abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t

namelen,

Quoted 1 line

+ abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong

newlen)

bsd-user/freebsd/os-sys.c out: · line 361

Quoted 12 lines
bsd-user/freebsd/os-sys.c +12 -0
+{
+    abi_long ret;
+    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;
+
+    if (oldlenp) {
+        if (get_user_ual(oldlen, oldlenp)) {
+            return -TARGET_EFAULT;
+        }
+    }

You need to check for write early. Either access_ok, or lock_user.

Quoted 6 lines

bsd-user/freebsd/os-sys.c out: · line 381

Quoted 3 lines
bsd-user/freebsd/os-sys.c +3 -0
+    for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++) {
+        *q++ = tswap32(*p);
+    }

Why the inconsistent increments?

no reason... Fixed.

Quoted 4 lines
Quoted 1 line

+ unlock_user(holdp, oldp, holdlen);

Usually we don't want writeback on error.

Indeed. Fixed as well.. in the other fixes for error handling.

Warner

Quoted 3 lines

r~

#27 lore raw

Re: [PATCH 9/9] bsd-user: Add -strict

In reply to Richard Henderson
Quoted 30 lines · Richard Henderson
Quoted 4 lines · Warner Losh

Most of the time, it's useful to make our best effort, but sometimes we want to know right away when we don't implement something. First place we use it is for unknown syscalls.

Review

Quoted 8 lines
---
  bsd-user/freebsd/os-syscall.c | 4 ++++
  bsd-user/main.c               | 5 ++++-
  bsd-user/qemu.h               | 1 +
  3 files changed, 9 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/bsd-user/freebsd/os-syscall.c

bsd-user/freebsd/os-syscall.c static abi_long freebsd_syscall(void *cpu_env, int

Quoted 4 lines

index 179a20c304b..e2b26ecb8dd 100644

bsd-user/freebsd/os-syscall.c +0 -0
@@ -508,6 +508,10 @@ static abi_long freebsd_syscall(void *cpu_env, int

num, abi_long arg1,

bsd-user/freebsd/os-syscall.c static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, · line 514

Quoted 7 lines
bsd-user/freebsd/os-syscall.c +4 -0
      default:
          qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
+        if (bsd_user_strict) {
+            printf("Unimplemented system call %d\n", num);
+            abort();
+        }

I don't like the raw printf, even if you did write to stderr. Perhaps just the abort, letting the error message be handled by qemu_log?

Quoted 10 lines

bsd-user/freebsd/os-syscall.c int main(int argc, char **argv) · line 401

Quoted 6 lines
bsd-user/freebsd/os-syscall.c +2 -0
@@ -396,6 +397,8 @@ int main(int argc, char **argv)
              trace_opt_parse(optarg);
          } else if (!strcmp(r, "0")) {
              argv0 = argv[optind++];
+        } else if (!strcmp(r, "strict")) {
+            bsd_user_strict = true;

Perhaps force LOG_UNIMP? Without -D, you'll get the qemu_log above to stderr.

Given the number of other changes in the other bits, I'm going to defer this feedback until round 3, since
the number of changes I've made for round 2 means there will almost certainly result in a round 3...

Warner