Skip to contentSkip to conversation

bsd-user: Start upstreaming the system calls.

PATCH Superseded Warner Losh 3 participants 67 messages Feb 1, 2022 – Feb 26, 2022 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#23 lore raw

PATCH 8/22 bsd-user: Remove bsd_type

In reply to Warner Losh

Remove keeping track of which type of bsd we're running on. It's no longer referenced in the code. Building bsd-user on NetBSD or OpenBSD isn't possible, let alone running that code. Stop pretending that we can do the cross BSD thing since there's been a large divergence since 2000 that makes this nearly impossible between FreeBSD and {Net,Open}BSD and at least quite difficult between NetBSD and OpenBSD.

---
 bsd-user/main.c | 2 --
 bsd-user/qemu.h | 7 -------
 2 files changed, 9 deletions(-)
bsd-user/main.c +0 -2
index bddb830e99b..88d347d05eb 100644
@@ -96,7 +96,6 @@ unsigned long reserved_va;
 
 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
-enum BSDType bsd_type;
 char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
 
 unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
@@ -284,7 +283,6 @@ int main(int argc, char **argv)
     const char *gdbstub = NULL;
     char **target_environ, **wrk;
     envlist_t *envlist = NULL;
-    bsd_type = HOST_DEFAULT_BSD_TYPE;
     char *argv0 = NULL;
 
     adjust_ssize();
bsd-user/qemu.h +0 -7
index 02921ac8b3b..e5742bd6c03 100644
@@ -29,13 +29,6 @@
 
 extern char **environ;
 
-enum BSDType {
-    target_freebsd,
-    target_netbsd,
-    target_openbsd,
-};
-extern enum BSDType bsd_type;
-
 #include "exec/user/thunk.h"
 #include "target_arch.h"
 #include "syscall_defs.h"
Signature · 1 line
2.33.1
#20 lore raw

PATCH 7/22 bsd-user/x86_64/target_arch_thread.h: Assume a FreeBSD target

In reply to Warner Losh

Since we can't run on anything else, assume for the moment that this is a FreeBSD target. In the future, we'll need to handle this properly via some include file in bsd-user/*bsd/x86_64/mumble.h. There's a number of other diffs that would be needed to make things work on OtherBSD, so it doesn't make sense to preseve this one detail today.

---
 bsd-user/x86_64/target_arch_thread.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
bsd-user/x86_64/target_arch_thread.h +1 -3
index d105e43fd35..b745d7ffeb7 100644
@@ -32,9 +32,7 @@ static inline void target_thread_init(struct target_pt_regs *regs,
     regs->rax = 0;
     regs->rsp = infop->start_stack;
     regs->rip = infop->entry;
-    if (bsd_type == target_freebsd) {
-        regs->rdi = infop->start_stack;
-    }
+    regs->rdi = infop->start_stack;
 }
 
 #endif /* !_TARGET_ARCH_THREAD_H_ */
Signature · 1 line
2.33.1
#13 lore raw

PATCH 5/22 bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls

In reply to Warner Losh

Since we don't build on OpenBSD, only do FreeBSD system calls here. In the future, we'll need to move this to some place like bsd-user/freebsd/arm/mumble.h, but until then just leave this inline. This reflects changes to the upstream.

---
 bsd-user/arm/target_arch_cpu.h | 139 ++++++++++++++++-----------------
 1 file changed, 66 insertions(+), 73 deletions(-)
bsd-user/arm/target_arch_cpu.h +66 -73
index b087db48fa4..afb7814a8d1 100644
@@ -40,7 +40,6 @@ static inline void target_cpu_init(CPUARMState *env,
 static inline void target_cpu_loop(CPUARMState *env)
 {
     int trapnr, si_signo, si_code;
-    unsigned int n;
     CPUState *cs = env_cpu(env);
 
     for (;;) {
@@ -66,82 +65,76 @@ static inline void target_cpu_loop(CPUARMState *env)
             break;
         case EXCP_SWI:
             {
-                n = env->regs[7];
-                if (bsd_type == target_freebsd) {
-                    int ret;
-                    abi_ulong params = get_sp_from_cpustate(env);
-                    int32_t syscall_nr = n;
-                    int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
+                int ret;
+                abi_ulong params = get_sp_from_cpustate(env);
+                int32_t syscall_nr = env->regs[7];
+                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
 
-                    /* See arm/arm/syscall.c cpu_fetch_syscall_args() */
-                    if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
-                        syscall_nr = env->regs[0];
-                        arg1 = env->regs[1];
-                        arg2 = env->regs[2];
-                        arg3 = env->regs[3];
-                        get_user_s32(arg4, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg7, params);
-                        arg8 = 0;
-                    } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
-                        syscall_nr = env->regs[0];
-                        arg1 = env->regs[2];
-                        arg2 = env->regs[3];
-                        get_user_s32(arg3, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg4, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        arg7 = 0;
-                        arg8 = 0;
-                    } else {
-                        arg1 = env->regs[0];
-                        arg2 = env->regs[1];
-                        arg3 = env->regs[2];
-                        arg4 = env->regs[3];
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg7, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg8, params);
-                    }
-                    ret = do_freebsd_syscall(env, syscall_nr, arg1, arg2, arg3,
-                            arg4, arg5, arg6, arg7, arg8);
+                /* See arm/arm/syscall.c cpu_fetch_syscall_args() */
+                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
+                    syscall_nr = env->regs[0];
+                    arg1 = env->regs[1];
+                    arg2 = env->regs[2];
+                    arg3 = env->regs[3];
+                    get_user_s32(arg4, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg7, params);
+                    arg8 = 0;
+                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
+                    syscall_nr = env->regs[0];
+                    arg1 = env->regs[2];
+                    arg2 = env->regs[3];
+                    get_user_s32(arg3, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg4, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    arg7 = 0;
+                    arg8 = 0;
+                } else {
+                    arg1 = env->regs[0];
+                    arg2 = env->regs[1];
+                    arg3 = env->regs[2];
+                    arg4 = env->regs[3];
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg7, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg8, params);
+                }
+                ret = do_freebsd_syscall(env, syscall_nr, arg1, arg2, arg3,
+                                         arg4, arg5, arg6, arg7, arg8);
+                /*
+                 * Compare to arm/arm/vm_machdep.c
+                 * cpu_set_syscall_retval()
+                 */
+                if (-TARGET_EJUSTRETURN == ret) {
                     /*
-                     * Compare to arm/arm/vm_machdep.c
-                     * cpu_set_syscall_retval()
+                     * Returning from a successful sigreturn syscall.
+                     * Avoid clobbering register state.
                      */
-                    if (-TARGET_EJUSTRETURN == ret) {
-                        /*
-                         * Returning from a successful sigreturn syscall.
-                         * Avoid clobbering register state.
-                         */
-                        break;
-                    }
-                    if (-TARGET_ERESTART == ret) {
-                        env->regs[15] -= env->thumb ? 2 : 4;
-                        break;
-                    }
-                    if ((unsigned int)ret >= (unsigned int)(-515)) {
-                        ret = -ret;
-                        cpsr_write(env, CPSR_C, CPSR_C, CPSRWriteByInstr);
-                        env->regs[0] = ret;
-                    } else {
-                        cpsr_write(env, 0, CPSR_C, CPSRWriteByInstr);
-                        env->regs[0] = ret; /* XXX need to handle lseek()? */
-                        /* env->regs[1] = 0; */
-                    }
+                    break;
+                }
+                if (-TARGET_ERESTART == ret) {
+                    env->regs[15] -= env->thumb ? 2 : 4;
+                    break;
+                }
+                if ((unsigned int)ret >= (unsigned int)(-515)) {
+                    ret = -ret;
+                    cpsr_write(env, CPSR_C, CPSR_C, CPSRWriteByInstr);
+                    env->regs[0] = ret;
                 } else {
-                    fprintf(stderr, "qemu: bsd_type (= %d) syscall "
-                            "not supported\n", bsd_type);
+                    cpsr_write(env, 0, CPSR_C, CPSRWriteByInstr);
+                    env->regs[0] = ret; /* XXX need to handle lseek()? */
+                    /* env->regs[1] = 0; */
                 }
             }
             break;
Signature · 1 line
2.33.1
#1 lore raw

PATCH 1/22 bsd-user/main.c: Drop syscall flavor arg -bsd

In reply to Warner Losh

We've not realistically been able to actually run any bsd program on any other bsd program. They are too diverged to do this easily. The current code is setup to do it, but implementing it is hard. Stop pretending that we can do this.

---
 bsd-user/main.c | 12 ------------
 1 file changed, 12 deletions(-)
bsd-user/main.c +0 -12
index f1d58e905e7..bddb830e99b 100644
@@ -164,7 +164,6 @@ static void usage(void)
            "-E var=value      sets/modifies targets environment variable(s)\n"
            "-U var            unsets targets environment variable(s)\n"
            "-B address        set guest_base address to address\n"
-           "-bsd type         select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
            "\n"
            "Debug options:\n"
            "-d item1[,...]    enable logging of specified items\n"
@@ -392,17 +391,6 @@ int main(int argc, char **argv)
             have_guest_base = true;
         } else if (!strcmp(r, "drop-ld-preload")) {
             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
-        } else if (!strcmp(r, "bsd")) {
-            if (!strcasecmp(argv[optind], "freebsd")) {
-                bsd_type = target_freebsd;
-            } else if (!strcasecmp(argv[optind], "netbsd")) {
-                bsd_type = target_netbsd;
-            } else if (!strcasecmp(argv[optind], "openbsd")) {
-                bsd_type = target_openbsd;
-            } else {
-                usage();
-            }
-            optind++;
         } else if (!strcmp(r, "seed")) {
             seed_optarg = optarg;
         } else if (!strcmp(r, "singlestep")) {
Signature · 1 line
2.33.1
#43 lore raw

PATCH 14/22 bsd-user/freebsd/os-syscall.c: lock_iovec

In reply to Warner Losh

lock_iovec will lock an I/O vec and the memory to which it referrs and create a iovec in the host space that referrs to it, with full error unwinding.

---
 bsd-user/freebsd/os-syscall.c | 92 +++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
bsd-user/freebsd/os-syscall.c +92 -0
index 060134a9ecd..c21759ae7ce 100644
@@ -75,6 +75,98 @@ bool is_error(abi_long ret)
     return (abi_ulong)ret >= (abi_ulong)(-4096);
 }
 
+struct iovec *lock_iovec(int type, abi_ulong target_addr,
+        int count, int copy)
+{
+    struct target_iovec *target_vec;
+    struct iovec *vec;
+    abi_ulong total_len, max_len;
+    int i;
+    int err = 0;
+    bool bad_address = false;
+
+    if (count == 0) {
+        errno = 0;
+        return NULL;
+    }
+    if (count < 0 || count > IOV_MAX) {
+        errno = EINVAL;
+        return NULL;
+    }
+
+    vec = calloc(count, sizeof(struct iovec));
+    if (vec == NULL) {
+        errno = ENOMEM;
+        return NULL;
+    }
+
+    target_vec = lock_user(VERIFY_READ, target_addr,
+                           count * sizeof(struct target_iovec), 1);
+    if (target_vec == NULL) {
+        err = EFAULT;
+        goto fail2;
+    }
+
+    /*
+     * ??? If host page size > target page size, this will result in a value
+     * larger than what we can actually support.
+     */
+    max_len = 0x7fffffff & TARGET_PAGE_MASK;
+    total_len = 0;
+
+    for (i = 0; i < count; i++) {
+        abi_ulong base = tswapal(target_vec[i].iov_base);
+        abi_long len = tswapal(target_vec[i].iov_len);
+
+        if (len < 0) {
+            err = EINVAL;
+            goto fail;
+        } else if (len == 0) {
+            /* Zero length pointer is ignored.  */
+            vec[i].iov_base = 0;
+        } else {
+            vec[i].iov_base = lock_user(type, base, len, copy);
+            /*
+             * If the first buffer pointer is bad, this is a fault.  But
+             * subsequent bad buffers will result in a partial write; this is
+             * realized by filling the vector with null pointers and zero
+             * lengths.
+             */
+            if (!vec[i].iov_base) {
+                if (i == 0) {
+                    err = EFAULT;
+                    goto fail;
+                } else {
+                    bad_address = true;
+                }
+            }
+            if (bad_address) {
+                len = 0;
+            }
+            if (len > max_len - total_len) {
+                len = max_len - total_len;
+            }
+        }
+        vec[i].iov_len = len;
+        total_len += len;
+    }
+
+    unlock_user(target_vec, target_addr, 0);
+    return vec;
+
+ fail:
+    while (--i >= 0) {
+        if (tswapal(target_vec[i].iov_len) > 0) {
+            unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
+        }
+    }
+    unlock_user(target_vec, target_addr, 0);
+ fail2:
+    free(vec);
+    errno = err;
+    return NULL;
+}
+
 /*
  * do_syscall() should always have a single exit point at the end so that
  * actions, such as logging of syscall results, can be performed.  All errnos
Signature · 1 line
2.33.1
#46 lore raw

PATCH 15/22 bsd-user/freebsd/os-syscall.c: unlock_iovec

In reply to Warner Losh

Releases the references to the iovec created by lock_iovec.

---
 bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
bsd-user/freebsd/os-syscall.c +23 -0
index c21759ae7ce..d49945f0fcc 100644
@@ -167,6 +167,29 @@ struct iovec *lock_iovec(int type, abi_ulong target_addr,
     return NULL;
 }
 
+void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
+        int count, int copy)
+{
+    struct target_iovec *target_vec;
+    int i;
+
+    target_vec = lock_user(VERIFY_READ, target_addr,
+                           count * sizeof(struct target_iovec), 1);
+    if (target_vec) {
+        for (i = 0; i < count; i++) {
+            abi_ulong base = tswapal(target_vec[i].iov_base);
+            abi_long len = tswapal(target_vec[i].iov_len);
+            if (len < 0) {
+                break;
+            }
+            unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
+        }
+        unlock_user(target_vec, target_addr, 0);
+    }
+
+    free(vec);
+}
+
 /*
  * do_syscall() should always have a single exit point at the end so that
  * actions, such as logging of syscall results, can be performed.  All errnos
Signature · 1 line
2.33.1
#17 lore raw

PATCH 6/22 bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target

In reply to Warner Losh

Since we can't run on anything else, assume for the moment that this is a FreeBSD target. In the future, we'll need to handle this properly via some include file in bsd-user/*bsd/arm/mumble.h. There's a number of other diffs that would be needed to make things work on OtherBSD, so it doesn't make sense to preseve this one detail today.

---
 bsd-user/arm/target_arch_thread.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
bsd-user/arm/target_arch_thread.h +1 -3
index 11c7f765838..fcafca2408c 100644
@@ -62,9 +62,7 @@ static inline void target_thread_init(struct target_pt_regs *regs,
     }
     regs->ARM_pc = infop->entry & 0xfffffffe;
     regs->ARM_sp = stack;
-    if (bsd_type == target_freebsd) {
-        regs->ARM_lr = infop->entry & 0xfffffffe;
-    }
+    regs->ARM_lr = infop->entry & 0xfffffffe;
     /*
      * FreeBSD kernel passes the ps_strings pointer in r0. This is used by some
      * programs to set status messages that we see in ps. bsd-user doesn't
Signature · 1 line
2.33.1
#59 lore raw

PATCH 20/22 bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv

In reply to Warner Losh

Implement do_bsd_{read,pread,readv,preadv}. Connect them to the system call table.

---
 bsd-user/bsd-file.h           | 79 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 24 +++++++++++
 2 files changed, 103 insertions(+)
bsd-user/bsd-file.h +79 -0
index 2f743db38e1..5934cbd5612 100644
@@ -36,4 +36,83 @@ extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,
 extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count,
         int copy);
 
+ssize_t safe_read(int fd, void *buf, size_t nbytes);
+ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset);
+ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt);
+ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
+
+/* read(2) */
+static inline abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3)
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(safe_read(arg1, p, arg3));
+    unlock_user(p, arg2, ret);
+
+    return ret;
+}
+
+/* pread(2) */
+static inline abi_long do_bsd_pread(void *cpu_env, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    if (regpairs_aligned(cpu_env) != 0) {
+        arg4 = arg5;
+        arg5 = arg6;
+    }
+    ret = get_errno(safe_pread(arg1, p, arg3, target_arg64(arg4, arg5)));
+    unlock_user(p, arg2, ret);
+
+    return ret;
+}
+
+/* readv(2) */
+static inline abi_long do_bsd_readv(abi_long arg1, abi_long arg2, abi_long arg3)
+{
+    abi_long ret;
+    struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
+
+    if (vec != NULL) {
+        ret = get_errno(safe_readv(arg1, vec, arg3));
+        unlock_iovec(vec, arg2, arg3, 1);
+    } else {
+        ret = -host_to_target_errno(errno);
+    }
+
+    return ret;
+}
+
+/* preadv(2) */
+static inline abi_long do_bsd_preadv(void *cpu_env, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
+{
+    abi_long ret;
+    struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 1);
+
+    if (vec != NULL) {
+        if (regpairs_aligned(cpu_env) != 0) {
+            arg4 = arg5;
+            arg5 = arg6;
+        }
+        ret = get_errno(safe_preadv(arg1, vec, arg3, target_arg64(arg4, arg5)));
+        unlock_iovec(vec, arg2, arg3, 0);
+    } else {
+        ret = -host_to_target_errno(errno);
+    }
+
+    return ret;
+}
+
 #endif /* !BSD_FILE_H_ */
bsd-user/freebsd/os-syscall.c +24 -0
index fcfa6221182..dda79af53de 100644
@@ -42,6 +42,14 @@
 
 #include "bsd-file.h"
 
+/* I/O */
+safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes);
+safe_syscall4(ssize_t, pread, int, fd, void *, buf, size_t, nbytes, off_t,
+    offset);
+safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt);
+safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
+    off_t, offset);
+
 void target_set_brk(abi_ulong new_brk)
 {
 }
@@ -212,6 +220,22 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
     }
 
     switch (num) {
+
+        /*
+         * File system calls.
+         */
+    case TARGET_FREEBSD_NR_read: /* read(2) */
+        ret = do_bsd_read(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_pread: /* pread(2) */
+        ret = do_bsd_pread(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
+        break;
+
+    case TARGET_FREEBSD_NR_readv: /* readv(2) */
+        ret = do_bsd_readv(arg1, arg2, arg3);
+        break;
+
     default:
         gemu_log("qemu: unsupported syscall: %d\n", num);
         ret = -TARGET_ENOSYS;
Signature · 1 line
2.33.1
#57 lore raw

PATCH 19/22 bsd-user: Add safe system call macros

In reply to Warner Losh

Add a series of macros to create system call macros that go via the safe_syscall path.

---
 bsd-user/syscall_defs.h | 47 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
bsd-user/syscall_defs.h +47 -0
index 62b472b990b..c3bf14f38f4 100644
@@ -179,4 +179,51 @@ struct target_freebsd__wrusage {
     struct target_freebsd_rusage wru_children;
 };
 
+#define safe_syscall0(type, name) \
+type safe_##name(void) \
+{ \
+    return safe_syscall(SYS_##name); \
+}
+
+#define safe_syscall1(type, name, type1, arg1) \
+type safe_##name(type1 arg1) \
+{ \
+    return safe_syscall(SYS_##name, arg1); \
+}
+
+#define safe_syscall2(type, name, type1, arg1, type2, arg2) \
+type safe_##name(type1 arg1, type2 arg2) \
+{ \
+    return safe_syscall(SYS_##name, arg1, arg2); \
+}
+
+#define safe_syscall3(type, name, type1, arg1, type2, arg2, type3, arg3) \
+type safe_##name(type1 arg1, type2 arg2, type3 arg3) \
+{ \
+    return safe_syscall(SYS_##name, arg1, arg2, arg3); \
+}
+
+#define safe_syscall4(type, name, type1, arg1, type2, arg2, type3, arg3, \
+    type4, arg4) \
+type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
+{ \
+    return safe_syscall(SYS_##name, arg1, arg2, arg3, arg4); \
+}
+
+#define safe_syscall5(type, name, type1, arg1, type2, arg2, type3, arg3, \
+    type4, arg4, type5, arg5) \
+type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
+    type5 arg5) \
+{ \
+    return safe_syscall(SYS_##name, arg1, arg2, arg3, arg4, arg5); \
+}
+
+#define safe_syscall6(type, name, type1, arg1, type2, arg2, type3, arg3, \
+    type4, arg4, type5, arg5, type6, arg6) \
+type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
+    type5 arg5, type6 arg6) \
+{ \
+    return safe_syscall(SYS_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
+}
+
 #endif /* ! _SYSCALL_DEFS_H_ */
Signature · 1 line
2.33.1
#3 lore raw

PATCH 2/22 bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64

In reply to Warner Losh

Although initial versions of NetBSD did use int $80, it was replaced by syscall before any releases. OpenBSD and FreeBSD always did syscall.

---
 bsd-user/x86_64/target_arch_cpu.h | 58 -------------------------------
 1 file changed, 58 deletions(-)
bsd-user/x86_64/target_arch_cpu.h +0 -58
index 0a9c0f08946..9dc52d5afc4 100644
@@ -124,64 +124,6 @@ static inline void target_cpu_loop(CPUX86State *env)
         process_queued_cpu_work(cs);
 
         switch (trapnr) {
-        case 0x80:
-            /* syscall from int $0x80 */
-            if (bsd_type == target_freebsd) {
-                abi_ulong params = (abi_ulong) env->regs[R_ESP] +
-                    sizeof(int32_t);
-                int32_t syscall_nr = env->regs[R_EAX];
-                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-
-                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int32_t);
-                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int64_t);
-                }
-                get_user_s32(arg1, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg2, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg3, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg4, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg5, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg6, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg7, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg8, params);
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      syscall_nr,
-                                                      arg1,
-                                                      arg2,
-                                                      arg3,
-                                                      arg4,
-                                                      arg5,
-                                                      arg6,
-                                                      arg7,
-                                                      arg8);
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EBX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_EBP]);
-            }
-            if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
-                env->regs[R_EAX] = -env->regs[R_EAX];
-                env->eflags |= CC_C;
-            } else {
-                env->eflags &= ~CC_C;
-            }
-            break;
-
         case EXCP_SYSCALL:
             /* syscall from syscall instruction */
             if (bsd_type == target_freebsd) {
Signature · 1 line
2.33.1
#26 lore raw

PATCH 9/22 bsd-user/freebsd/os-syscall.c: Move syscall processing here

In reply to Warner Losh

While there is some commonality between *BSD syscall processing, there's a number of differences and the system call numbers and ABIs have been independent since the late 90s. Move FreeBSD's proessing here and delete it.

The upstream implementation is somewhat different than the current implementation. It will be much easier to upstream these from scratch, justifying the final result, rather than working out the diffs and justifying the changes. Also tweak a comment to qemu standard form.

---
 bsd-user/freebsd/os-syscall.c | 68 +++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 bsd-user/freebsd/os-syscall.c
bsd-user/freebsd/os-syscall.c new file +68 -0
new file mode 100644
index 00000000000..7e2bedb918d
@@ -0,0 +1,68 @@
+/*
+ *  BSD syscalls
+ *
+ *  Copyright (c) 2003-2008 Fabrice Bellard
+ *  Copyright (c) 2013-2014 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * We need the FreeBSD "legacy" definitions. Rust needs the FreeBSD 11 system
+ * calls, so we have to emulate that despite FreeBSD being EOL'd.
+ */
+#define _WANT_FREEBSD11_STAT
+#define _WANT_FREEBSD11_STATFS
+#define _WANT_FREEBSD11_DIRENT
+#define _WANT_KERNEL_ERRNO
+#define _WANT_SEMUN
+#include "qemu/osdep.h"
+#include "qemu/cutils.h"
+#include "qemu/path.h"
+#include <sys/syscall.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <utime.h>
+
+#include "qemu.h"
+#include "qemu-common.h"
+#include "signal-common.h"
+#include "user/syscall-trace.h"
+
+void target_set_brk(abi_ulong new_brk)
+{
+}
+
+bool is_error(abi_long ret)
+{
+
+    return (abi_ulong)ret >= (abi_ulong)(-4096);
+}
+
+/*
+ * do_syscall() should always have a single exit point at the end so that
+ * actions, such as logging of syscall results, can be performed.  All errnos
+ * that do_syscall() returns must be -TARGET_<errcode>.
+ */
+abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
+                            abi_long arg2, abi_long arg3, abi_long arg4,
+                            abi_long arg5, abi_long arg6, abi_long arg7,
+                            abi_long arg8)
+{
+    return 0;
+}
+
+void syscall_init(void)
+{
+}
Signature · 1 line
2.33.1
#29 lore raw

PATCH 10/22 bsd-user: Move system call building to os-syscall.c

In reply to Warner Losh
---
 bsd-user/freebsd/meson.build | 1 +
 bsd-user/meson.build         | 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)
bsd-user/freebsd/meson.build +1 -0
index 4b69cca7b90..f87c788e846 100644
@@ -1,3 +1,4 @@
 bsd_user_ss.add(files(
   'os-sys.c',
+  'os-syscall.c',
 ))
bsd-user/meson.build +0 -1
index 8380fa44c25..5243122fc56 100644
@@ -13,7 +13,6 @@ bsd_user_ss.add(files(
   'mmap.c',
   'signal.c',
   'strace.c',
-  'syscall.c',
   'uaccess.c',
 ))
 
Signature · 1 line
2.33.1
#6 lore raw

PATCH 3/22 bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall

In reply to Warner Losh

This doesn't build on openbsd at the moment, and this could should arguably be in bsd-user/*bsd/x86_64 somewhere. Until we refactor to support OpenBSD/NetBSD again, drop it here.

---
 bsd-user/x86_64/target_arch_cpu.h | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
bsd-user/x86_64/target_arch_cpu.h +8 -19
index 9dc52d5afc4..5be2f02416e 100644
@@ -126,25 +126,14 @@ static inline void target_cpu_loop(CPUX86State *env)
         switch (trapnr) {
         case EXCP_SYSCALL:
             /* syscall from syscall instruction */
-            if (bsd_type == target_freebsd) {
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[8],
-                                                      env->regs[9], 0, 0);
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDX],
-                                                      env->regs[10],
-                                                      env->regs[8],
-                                                      env->regs[9]);
-            }
+            env->regs[R_EAX] = do_freebsd_syscall(env,
+                                                  env->regs[R_EAX],
+                                                  env->regs[R_EDI],
+                                                  env->regs[R_ESI],
+                                                  env->regs[R_EDX],
+                                                  env->regs[R_ECX],
+                                                  env->regs[8],
+                                                  env->regs[9], 0, 0);
             env->eip = env->exception_next_eip;
             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
                 env->regs[R_EAX] = -env->regs[R_EAX];
Signature · 1 line
2.33.1
#10 lore raw

PATCH 4/22 bsd-user/i386/target_arch_cpu.h: Remove openbsd syscall

In reply to Warner Losh

This doesn't build on openbsd at the moment, and this could should arguably be in bsd-user/*bsd/i386 somewhere. Until we refactor to support OpenBSD/NetBSD again, drop it here.

---
 bsd-user/i386/target_arch_cpu.h | 84 +++++++++++++++------------------
 1 file changed, 37 insertions(+), 47 deletions(-)
bsd-user/i386/target_arch_cpu.h +37 -47
index 3cbf69d8af2..9da22202d48 100644
@@ -116,55 +116,45 @@ static inline void target_cpu_loop(CPUX86State *env)
         process_queued_cpu_work(cs);
 
         switch (trapnr) {
-        case 0x80:
+        case 0x80: {
             /* syscall from int $0x80 */
-            if (bsd_type == target_freebsd) {
-                abi_ulong params = (abi_ulong) env->regs[R_ESP] +
-                    sizeof(int32_t);
-                int32_t syscall_nr = env->regs[R_EAX];
-                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-
-                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int32_t);
-                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int64_t);
-                }
-                get_user_s32(arg1, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg2, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg3, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg4, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg5, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg6, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg7, params);
+            abi_ulong params = (abi_ulong) env->regs[R_ESP] +
+                sizeof(int32_t);
+            int32_t syscall_nr = env->regs[R_EAX];
+            int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
+
+            if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
+                get_user_s32(syscall_nr, params);
                 params += sizeof(int32_t);
-                get_user_s32(arg8, params);
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      syscall_nr,
-                                                      arg1,
-                                                      arg2,
-                                                      arg3,
-                                                      arg4,
-                                                      arg5,
-                                                      arg6,
-                                                      arg7,
-                                                      arg8);
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EBX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_EBP]);
+            } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
+                get_user_s32(syscall_nr, params);
+                params += sizeof(int64_t);
+            }
+            get_user_s32(arg1, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg2, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg3, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg4, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg5, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg6, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg7, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg8, params);
+            env->regs[R_EAX] = do_freebsd_syscall(env,
+                                                  syscall_nr,
+                                                  arg1,
+                                                  arg2,
+                                                  arg3,
+                                                  arg4,
+                                                  arg5,
+                                                  arg6,
+                                                  arg7,
+                                                  arg8);
             }
             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
                 env->regs[R_EAX] = -env->regs[R_EAX];
Signature · 1 line
2.33.1
#0 lore raw

cover bsd-user: Start upstreaming the system calls.

This series of patches does two things. First, it starts to give up on the idea that you can run FooBSD binaries on BarBSD. They are too different to make that happen any time soon, though I've kept the support for Net/OpenBSD, even though they haven't built. We'll need a lot of work to make that happen, though, and I need to simplify to get things upstream.

Second, this upstreams the first 9 system calls. It deletes the old implementation and starts fresh with a new one that pulls some things from the old, updates some old things, etc. It's easier to understand this way.

Finally, it starts to move some of the ifdef trees into target.h.

Warner Losh (22):
bsd-user/main.c: Drop syscall flavor arg -bsd bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call
on amd64
bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall bsd-user/i386/target_arch_cpu.h: Remove openbsd syscall bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target bsd-user/x86_64/target_arch_thread.h: Assume a FreeBSD target
bsd-user: Remove bsd_type
bsd-user/freebsd/os-syscall.c: Move syscall processing here
bsd-user: Move system call building to os-syscall.c bsd-user/sycall.c: Now obsolete, remove bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno bsd-user/bsd-file.h: Implementation details for the filesystem calls bsd-user/freebsd/os-syscall.c: lock_iovec bsd-user/freebsd/os-syscall.c: unlock_iovec bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate
bsd-user: introduce target.h
bsd-user: Define target_arg64
bsd-user: Add safe system call macros
bsd-user/bsd-file.h: Add implementations for read, pread, readv and
preadv
bsd-user/bsd-file.h: Meat of the write system calls bsd-user/freebsd/os-syscall.c: Implement exit

 bsd-user/arm/target.h                |  21 ++
 bsd-user/arm/target_arch_cpu.h       | 139 ++++----
 bsd-user/arm/target_arch_thread.h    |   4 +-
 bsd-user/bsd-file.h                  | 203 +++++++++++
 bsd-user/bsd-proc.h                  |  43 +++
 bsd-user/freebsd/meson.build         |   1 +
 bsd-user/freebsd/os-syscall.c        | 287 +++++++++++++++
 bsd-user/i386/target.h               |  21 ++
 bsd-user/i386/target_arch_cpu.h      |  84 ++---
 bsd-user/main.c                      |  14 -
 bsd-user/meson.build                 |   1 -
 bsd-user/qemu.h                      |  24 +-
 bsd-user/syscall.c                   | 516 ---------------------------
 bsd-user/syscall_defs.h              |  47 +++
 bsd-user/x86_64/target.h             |  21 ++
 bsd-user/x86_64/target_arch_cpu.h    |  85 +----
 bsd-user/x86_64/target_arch_thread.h |   4 +-
 17 files changed, 773 insertions(+), 742 deletions(-)
 create mode 100644 bsd-user/arm/target.h
 create mode 100644 bsd-user/bsd-file.h
 create mode 100644 bsd-user/bsd-proc.h
 create mode 100644 bsd-user/freebsd/os-syscall.c
 create mode 100644 bsd-user/i386/target.h
 delete mode 100644 bsd-user/syscall.c
 create mode 100644 bsd-user/x86_64/target.h
Signature · 1 line
2.33.1
#31 lore raw

PATCH 11/22 bsd-user/sycall.c: Now obsolete, remove

---
 bsd-user/syscall.c | 516 ---------------------------------------------
 1 file changed, 516 deletions(-)
 delete mode 100644 bsd-user/syscall.c
bsd-user/syscall.c deleted +0 -516
deleted file mode 100644
index d3322760f43..00000000000
@@ -1,516 +0,0 @@
-/*
- *  BSD syscalls
- *
- *  Copyright (c) 2003 - 2008 Fabrice Bellard
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, see <http://www.gnu.org/licenses/>.
- */
-#include "qemu/osdep.h"
-#include "qemu/cutils.h"
-#include "qemu/path.h"
-#include <sys/syscall.h>
-#include <sys/param.h>
-#include <sys/sysctl.h>
-#include <utime.h>
-
-#include "qemu.h"
-#include "qemu-common.h"
-#include "user/syscall-trace.h"
-
-//#define DEBUG
-
-static abi_ulong target_brk;
-static abi_ulong target_original_brk;
-
-abi_long get_errno(abi_long ret)
-{
-    if (ret == -1) {
-        /* XXX need to translate host -> target errnos here */
-        return -(errno);
-    }
-    return ret;
-}
-
-#define target_to_host_bitmask(x, tbl) (x)
-
-bool is_error(abi_long ret)
-{
-    return (abi_ulong)ret >= (abi_ulong)(-4096);
-}
-
-void target_set_brk(abi_ulong new_brk)
-{
-    target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
-}
-
-/* do_obreak() must return target errnos. */
-static abi_long do_obreak(abi_ulong new_brk)
-{
-    abi_ulong brk_page;
-    abi_long mapped_addr;
-    int new_alloc_size;
-
-    if (!new_brk)
-        return 0;
-    if (new_brk < target_original_brk)
-        return -TARGET_EINVAL;
-
-    brk_page = HOST_PAGE_ALIGN(target_brk);
-
-    /* If the new brk is less than this, set it and we're done... */
-    if (new_brk < brk_page) {
-        target_brk = new_brk;
-        return 0;
-    }
-
-    /* We need to allocate more memory after the brk... */
-    new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
-    mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
-                                        PROT_READ|PROT_WRITE,
-                                        MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0));
-
-    if (!is_error(mapped_addr))
-        target_brk = new_brk;
-    else
-        return mapped_addr;
-
-    return 0;
-}
-
-#ifdef __FreeBSD__
-/*
- * XXX this uses the undocumented oidfmt interface to find the kind of
- * a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
- * (this is mostly copied from 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.
- * XXX 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:
-        *(uint32_t *)holdp = tswap32(*(long *)holdp);
-        break;
-#else
-    case CTLTYPE_LONG:
-        *(uint64_t *)holdp = tswap64(*(long *)holdp);
-        break;
-    case CTLTYPE_ULONG:
-        *(uint64_t *)holdp = tswap64(*(unsigned long *)holdp);
-        break;
-#endif
-#ifdef CTLTYPE_U64
-    case CTLTYPE_S64:
-    case CTLTYPE_U64:
-#else
-    case CTLTYPE_QUAD:
-#endif
-        *(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
-        break;
-    case CTLTYPE_STRING:
-        break;
-    default:
-        /* XXX unhandled */
-        return -1;
-    }
-    return 0;
-}
-
-/* XXX this needs to be emulated on non-FreeBSD hosts... */
-static abi_long do_freebsd_sysctl(abi_ulong namep, int32_t namelen, abi_ulong oldp,
-                          abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
-{
-    abi_long ret;
-    void *hnamep, *holdp, *hnewp = NULL;
-    size_t holdlen;
-    abi_ulong oldlen = 0;
-    int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
-    uint32_t kind = 0;
-
-    if (oldlenp)
-        get_user_ual(oldlen, oldlenp);
-    if (!(hnamep = lock_user(VERIFY_READ, namep, namelen, 1)))
-        return -TARGET_EFAULT;
-    if (newp && !(hnewp = lock_user(VERIFY_READ, newp, newlen, 1)))
-        return -TARGET_EFAULT;
-    if (!(holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0)))
-        return -TARGET_EFAULT;
-    holdlen = oldlen;
-    for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++)
-       *q++ = tswap32(*p);
-    oidfmt(snamep, namelen, NULL, &kind);
-    /* XXX swap hnewp */
-    ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
-    if (!ret)
-        sysctl_oldcvt(holdp, holdlen, kind);
-    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;
-}
-#endif
-
-/* FIXME
- * lock_iovec()/unlock_iovec() have a return code of 0 for success where
- * other lock functions have a return code of 0 for failure.
- */
-static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
-                           int count, int copy)
-{
-    struct target_iovec *target_vec;
-    abi_ulong base;
-    int i;
-
-    target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
-    if (!target_vec)
-        return -TARGET_EFAULT;
-    for (i = 0;i < count; i++) {
-        base = tswapl(target_vec[i].iov_base);
-        vec[i].iov_len = tswapl(target_vec[i].iov_len);
-        if (vec[i].iov_len != 0) {
-            vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
-            /* Don't check lock_user return value. We must call writev even
-               if a element has invalid base address. */
-        } else {
-            /* zero length pointer is ignored */
-            vec[i].iov_base = NULL;
-        }
-    }
-    unlock_user (target_vec, target_addr, 0);
-    return 0;
-}
-
-static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
-                             int count, int copy)
-{
-    struct target_iovec *target_vec;
-    abi_ulong base;
-    int i;
-
-    target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
-    if (!target_vec)
-        return -TARGET_EFAULT;
-    for (i = 0;i < count; i++) {
-        if (target_vec[i].iov_base) {
-            base = tswapl(target_vec[i].iov_base);
-            unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
-        }
-    }
-    unlock_user (target_vec, target_addr, 0);
-
-    return 0;
-}
-
-/* do_syscall() should always have a single exit point at the end so
-   that actions, such as logging of syscall results, can be performed.
-   All errnos that do_syscall() returns must be -TARGET_<errcode>. */
-abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
-                            abi_long arg2, abi_long arg3, abi_long arg4,
-                            abi_long arg5, abi_long arg6, abi_long arg7,
-                            abi_long arg8)
-{
-    CPUState *cpu = env_cpu(cpu_env);
-    abi_long ret;
-    void *p;
-
-#ifdef DEBUG
-    gemu_log("freebsd syscall %d\n", num);
-#endif
-    record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
-
-    if (do_strace)
-        print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-
-    switch (num) {
-    case TARGET_FREEBSD_NR_exit:
-#ifdef CONFIG_GPROF
-        _mcleanup();
-#endif
-        gdb_exit(arg1);
-        qemu_plugin_user_exit();
-        /* XXX: should free thread stack and CPU env */
-        _exit(arg1);
-        ret = 0; /* avoid warning */
-        break;
-    case TARGET_FREEBSD_NR_read:
-        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
-            goto efault;
-        ret = get_errno(read(arg1, p, arg3));
-        unlock_user(p, arg2, ret);
-        break;
-    case TARGET_FREEBSD_NR_write:
-        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
-            goto efault;
-        ret = get_errno(write(arg1, p, arg3));
-        unlock_user(p, arg2, 0);
-        break;
-    case TARGET_FREEBSD_NR_writev:
-        {
-            int count = arg3;
-            struct iovec *vec;
-
-            vec = alloca(count * sizeof(struct iovec));
-            if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
-                goto efault;
-            ret = get_errno(writev(arg1, vec, count));
-            unlock_iovec(vec, arg2, count, 0);
-        }
-        break;
-    case TARGET_FREEBSD_NR_open:
-        if (!(p = lock_user_string(arg1)))
-            goto efault;
-        ret = get_errno(open(path(p),
-                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
-                             arg3));
-        unlock_user(p, arg1, 0);
-        break;
-    case TARGET_FREEBSD_NR_mmap:
-        ret = get_errno(target_mmap(arg1, arg2, arg3,
-                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
-                                    arg5,
-                                    arg6));
-        break;
-    case TARGET_FREEBSD_NR_mprotect:
-        ret = get_errno(target_mprotect(arg1, arg2, arg3));
-        break;
-    case TARGET_FREEBSD_NR_break:
-        ret = do_obreak(arg1);
-        break;
-#ifdef __FreeBSD__
-    case TARGET_FREEBSD_NR___sysctl:
-        ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
-        break;
-#endif
-    case TARGET_FREEBSD_NR_sysarch:
-        ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
-        break;
-    case TARGET_FREEBSD_NR_syscall:
-    case TARGET_FREEBSD_NR___syscall:
-        ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,arg7,arg8,0);
-        break;
-    default:
-        ret = get_errno(syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
-        break;
-    }
- fail:
-#ifdef DEBUG
-    gemu_log(" = %ld\n", ret);
-#endif
-    if (do_strace)
-        print_freebsd_syscall_ret(num, ret);
-
-    record_syscall_return(cpu, num, ret);
-    return ret;
- efault:
-    ret = -TARGET_EFAULT;
-    goto fail;
-}
-
-abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
-                           abi_long arg2, abi_long arg3, abi_long arg4,
-                           abi_long arg5, abi_long arg6)
-{
-    CPUState *cpu = env_cpu(cpu_env);
-    abi_long ret;
-    void *p;
-
-#ifdef DEBUG
-    gemu_log("netbsd syscall %d\n", num);
-#endif
-
-    record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
-
-    if (do_strace)
-        print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-
-    switch (num) {
-    case TARGET_NETBSD_NR_exit:
-#ifdef CONFIG_GPROF
-        _mcleanup();
-#endif
-        gdb_exit(arg1);
-        qemu_plugin_user_exit();
-        /* XXX: should free thread stack and CPU env */
-        _exit(arg1);
-        ret = 0; /* avoid warning */
-        break;
-    case TARGET_NETBSD_NR_read:
-        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
-            goto efault;
-        ret = get_errno(read(arg1, p, arg3));
-        unlock_user(p, arg2, ret);
-        break;
-    case TARGET_NETBSD_NR_write:
-        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
-            goto efault;
-        ret = get_errno(write(arg1, p, arg3));
-        unlock_user(p, arg2, 0);
-        break;
-    case TARGET_NETBSD_NR_open:
-        if (!(p = lock_user_string(arg1)))
-            goto efault;
-        ret = get_errno(open(path(p),
-                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
-                             arg3));
-        unlock_user(p, arg1, 0);
-        break;
-    case TARGET_NETBSD_NR_mmap:
-        ret = get_errno(target_mmap(arg1, arg2, arg3,
-                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
-                                    arg5,
-                                    arg6));
-        break;
-    case TARGET_NETBSD_NR_mprotect:
-        ret = get_errno(target_mprotect(arg1, arg2, arg3));
-        break;
-    case TARGET_NETBSD_NR_syscall:
-    case TARGET_NETBSD_NR___syscall:
-        ret = do_netbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
-        break;
-    default:
-        ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-        break;
-    }
- fail:
-#ifdef DEBUG
-    gemu_log(" = %ld\n", ret);
-#endif
-    if (do_strace)
-        print_netbsd_syscall_ret(num, ret);
-
-    record_syscall_return(cpu, num, ret);
-    return ret;
- efault:
-    ret = -TARGET_EFAULT;
-    goto fail;
-}
-
-abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
-                            abi_long arg2, abi_long arg3, abi_long arg4,
-                            abi_long arg5, abi_long arg6)
-{
-    CPUState *cpu = env_cpu(cpu_env);
-    abi_long ret;
-    void *p;
-
-#ifdef DEBUG
-    gemu_log("openbsd syscall %d\n", num);
-#endif
-
-    record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
-
-    if (do_strace)
-        print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-
-    switch (num) {
-    case TARGET_OPENBSD_NR_exit:
-#ifdef CONFIG_GPROF
-        _mcleanup();
-#endif
-        gdb_exit(arg1);
-        qemu_plugin_user_exit();
-        /* XXX: should free thread stack and CPU env */
-        _exit(arg1);
-        ret = 0; /* avoid warning */
-        break;
-    case TARGET_OPENBSD_NR_read:
-        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
-            goto efault;
-        ret = get_errno(read(arg1, p, arg3));
-        unlock_user(p, arg2, ret);
-        break;
-    case TARGET_OPENBSD_NR_write:
-        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
-            goto efault;
-        ret = get_errno(write(arg1, p, arg3));
-        unlock_user(p, arg2, 0);
-        break;
-    case TARGET_OPENBSD_NR_open:
-        if (!(p = lock_user_string(arg1)))
-            goto efault;
-        ret = get_errno(open(path(p),
-                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
-                             arg3));
-        unlock_user(p, arg1, 0);
-        break;
-    case TARGET_OPENBSD_NR_mmap:
-        ret = get_errno(target_mmap(arg1, arg2, arg3,
-                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
-                                    arg5,
-                                    arg6));
-        break;
-    case TARGET_OPENBSD_NR_mprotect:
-        ret = get_errno(target_mprotect(arg1, arg2, arg3));
-        break;
-    case TARGET_OPENBSD_NR_syscall:
-    case TARGET_OPENBSD_NR___syscall:
-        ret = do_openbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
-        break;
-    default:
-        ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-        break;
-    }
- fail:
-#ifdef DEBUG
-    gemu_log(" = %ld\n", ret);
-#endif
-    if (do_strace)
-        print_openbsd_syscall_ret(num, ret);
-
-    record_syscall_return(cpu, num, ret);
-    return ret;
- efault:
-    ret = -TARGET_EFAULT;
-    goto fail;
-}
-
-void syscall_init(void)
-{
-}
Signature · 1 line
2.33.1
#38 lore raw

PATCH 13/22 bsd-user/bsd-file.h: Implementation details for the filesystem calls

In reply to Warner Losh

An include file that pulls in all the definitions needed for the file related system calls. This also includes the host definitions to implement the system calls and some helper routines to lock/unlock different aspects of the system call arguments.

---
 bsd-user/bsd-file.h           | 39 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  2 ++
 2 files changed, 41 insertions(+)
 create mode 100644 bsd-user/bsd-file.h
bsd-user/bsd-file.h new file +39 -0
new file mode 100644
index 00000000000..2f743db38e1
@@ -0,0 +1,39 @@
+/*
+ *  file related system call shims and definitions
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef BSD_FILE_H_
+#define BSD_FILE_H_
+
+#include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/uio.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "qemu/path.h"
+
+extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,
+        int copy);
+extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count,
+        int copy);
+
+#endif /* !BSD_FILE_H_ */
bsd-user/freebsd/os-syscall.c +2 -0
index 2e84cf350b1..060134a9ecd 100644
@@ -40,6 +40,8 @@
 #include "signal-common.h"
 #include "user/syscall-trace.h"
 
+#include "bsd-file.h"
+
 void target_set_brk(abi_ulong new_brk)
 {
 }
Signature · 1 line
2.33.1
#34 lore raw

PATCH 12/22 bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno

In reply to Warner Losh

Add the helper functions get_errno and host_to_target_errno. get_errno returns either the system call results, or the -errno when system call indicates failure by returning -1. Host_to_target_errno returns errno (since on FreeBSD they are the same on all architectures) along with a comment about why it's the identity.

---
 bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
 bsd-user/qemu.h               |  3 ++-
 2 files changed, 25 insertions(+), 1 deletion(-)
bsd-user/freebsd/os-syscall.c +23 -0
index 7e2bedb918d..2e84cf350b1 100644
@@ -44,6 +44,29 @@ void target_set_brk(abi_ulong new_brk)
 {
 }
 
+/*
+ * errno conversion.
+ */
+abi_long get_errno(abi_long ret)
+{
+
+    if (ret == -1) {
+        return -host_to_target_errno(errno);
+    } else {
+        return ret;
+    }
+}
+
+int host_to_target_errno(int err)
+{
+    /*
+     * All the BSDs have the property that the error numbers are uniform across
+     * all architectures for a given BSD, though they may vary between different
+     * BSDs.
+     */
+    return err;
+}
+
 bool is_error(abi_long ret)
 {
 
bsd-user/qemu.h +2 -1
index e5742bd6c03..56042ddbc5d 100644
@@ -246,9 +246,10 @@ extern unsigned long target_dflssiz;
 extern unsigned long target_maxssiz;
 extern unsigned long target_sgrowsiz;
 
-/* syscall.c */
+/* os-syscall.c */
 abi_long get_errno(abi_long ret);
 bool is_error(abi_long ret);
+int host_to_target_errno(int err);
 
 /* os-sys.c */
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
Signature · 1 line
2.33.1
#54 lore raw

PATCH 18/22 bsd-user: Define target_arg64

In reply to Warner Losh

target_arg64 is a generic way to extract 64-bits from a pair of arguments. On 32-bit platforms, it returns them joined together as appropriate. On 64-bit platforms, it returns the first arg because it's already 64-bits.

---
 bsd-user/qemu.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
bsd-user/qemu.h +13 -0
index a9efa807b78..af272c2a802 100644
@@ -462,6 +462,19 @@ static inline void *lock_user_string(abi_ulong guest_addr)
 #define unlock_user_struct(host_ptr, guest_addr, copy)          \
     unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
 
+static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
+{
+#if TARGET_ABI_BITS == 32
+#ifdef TARGET_WORDS_BIGENDIAN
+    return ((uint64_t)word0 << 32) | word1;
+#else
+    return ((uint64_t)word1 << 32) | word0;
+#endif
+#else /* TARGET_ABI_BITS != 32 */
+    return word0;
+#endif /* TARGET_ABI_BITS != 32 */
+}
+
 #include <pthread.h>
 
 #include "user/safe-syscall.h"
Signature · 1 line
2.33.1
#51 lore raw

PATCH 17/22 bsd-user: introduce target.h

In reply to Warner Losh

Create target.h. This file is intended to be simple and describe basic things about the architecture. If something is a basic feature of the architecture, it belongs here. Should we need something that's per-BSD there will be a target-os.h that will live in the per-bsd directories.

Define regpairs_aligned to reflect whether or not registers are 'paired' for 64-bit arguments or not. This will be false for all 64-bit targets, and will be true on those architectures that pair (currently just armv7 and powerpc on FreeBSD 14.x).

---
 bsd-user/arm/target.h    | 21 +++++++++++++++++++++
 bsd-user/i386/target.h   | 21 +++++++++++++++++++++
 bsd-user/qemu.h          |  1 +
 bsd-user/x86_64/target.h | 21 +++++++++++++++++++++
 4 files changed, 64 insertions(+)
 create mode 100644 bsd-user/arm/target.h
 create mode 100644 bsd-user/i386/target.h
 create mode 100644 bsd-user/x86_64/target.h
bsd-user/arm/target.h new file +21 -0
new file mode 100644
index 00000000000..1f7ee49bfb4
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all i386 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TARGET_H
+#define TARGET_H
+
+/*
+ * arm EABI 'lumps' the registers for 64-bit args.
+ */
+static inline int regpairs_aligned(void *cpu_env)
+{
+    return 1;
+}
+
+#endif /* ! TARGET_H */
+
bsd-user/i386/target.h new file +21 -0
new file mode 100644
index 00000000000..b0ab477d683
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all i386 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TARGET_ARCH_H
+#define TARGET_ARCH_H
+
+/*
+ * i386 doesn't 'lump' the registers for 64-bit args.
+ */
+static inline int regpairs_aligned(void *cpu_env)
+{
+    return 0;
+}
+
+#endif /* ! TARGET_ARCH_H */
+
bsd-user/qemu.h +1 -0
index 56042ddbc5d..a9efa807b78 100644
@@ -35,6 +35,7 @@ extern char **environ;
 #include "target_syscall.h"
 #include "target_os_vmparam.h"
 #include "target_os_signal.h"
+#include "target.h"
 #include "exec/gdbstub.h"
 
 /*
bsd-user/x86_64/target.h new file +21 -0
new file mode 100644
index 00000000000..6d3aef8fc49
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all x86_64 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TARGET_H
+#define TARGET_H
+
+/*
+ * x86 doesn't 'lump' the registers for 64-bit args, all args are 64 bits.
+ */
+static inline int regpairs_aligned(void *cpu_env)
+{
+    return 0;
+}
+
+#endif /* ! TARGET_H */
+
Signature · 1 line
2.33.1
#49 lore raw

PATCH 16/22 bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate

In reply to Warner Losh

Add in the tracing and this system call not implemented boilerplate.

---
 bsd-user/freebsd/os-syscall.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
bsd-user/freebsd/os-syscall.c +26 -1
index d49945f0fcc..fcfa6221182 100644
@@ -200,7 +200,32 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
                             abi_long arg5, abi_long arg6, abi_long arg7,
                             abi_long arg8)
 {
-    return 0;
+    CPUState *cpu = env_cpu(cpu_env);
+    abi_long ret;
+
+#ifdef DEBUG
+    gemu_log("freebsd syscall %d\n", num);
+#endif
+    trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
+    if (do_strace) {
+        print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
+    }
+
+    switch (num) {
+    default:
+        gemu_log("qemu: unsupported syscall: %d\n", num);
+        ret = -TARGET_ENOSYS;
+        break;
+    }
+
+#ifdef DEBUG
+    gemu_log(" = %ld\n", ret);
+#endif
+    if (do_strace) {
+        print_freebsd_syscall_ret(num, ret);
+    }
+    trace_guest_user_syscall_ret(cpu, num, ret);
+    return ret;
 }
 
 void syscall_init(void)
Signature · 1 line
2.33.1
#62 lore raw

PATCH 21/22 bsd-user/bsd-file.h: Meat of the write system calls

In reply to Warner Losh

Implement write, writev, pwrite and pwritev and connect them to the system call dispatch routine.

---
 bsd-user/bsd-file.h           | 85 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 23 ++++++++++
 2 files changed, 108 insertions(+)
bsd-user/bsd-file.h +85 -0
index 5934cbd5612..2047256a555 100644
@@ -41,6 +41,11 @@ ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset);
 ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt);
 ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
 
+ssize_t safe_write(int fd, void *buf, size_t nbytes);
+ssize_t safe_pwrite(int fd, void *buf, size_t nbytes, off_t offset);
+ssize_t safe_writev(int fd, const struct iovec *iov, int iovcnt);
+ssize_t safe_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
+
 /* read(2) */
 static inline abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3)
 {
@@ -115,4 +120,84 @@ static inline abi_long do_bsd_preadv(void *cpu_env, abi_long arg1,
     return ret;
 }
 
+/* write(2) */
+static inline abi_long do_bsd_write(abi_long arg1, abi_long arg2, abi_long arg3)
+{
+    abi_long nbytes, ret;
+    void *p;
+
+    /* nbytes < 0 implies that it was larger than SIZE_MAX. */
+    nbytes = arg3;
+    if (nbytes < 0) {
+        return -TARGET_EINVAL;
+    }
+    p = lock_user(VERIFY_READ, arg2, nbytes, 1);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(safe_write(arg1, p, arg3));
+    unlock_user(p, arg2, 0);
+
+    return ret;
+}
+
+/* pwrite(2) */
+static inline abi_long do_bsd_pwrite(void *cpu_env, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user(VERIFY_READ, arg2, arg3, 1);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    if (regpairs_aligned(cpu_env) != 0) {
+        arg4 = arg5;
+        arg5 = arg6;
+    }
+    ret = get_errno(safe_pwrite(arg1, p, arg3, target_arg64(arg4, arg5)));
+    unlock_user(p, arg2, 0);
+
+    return ret;
+}
+
+/* writev(2) */
+static inline abi_long do_bsd_writev(abi_long arg1, abi_long arg2,
+        abi_long arg3)
+{
+    abi_long ret;
+    struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
+
+    if (vec != NULL) {
+        ret = get_errno(safe_writev(arg1, vec, arg3));
+        unlock_iovec(vec, arg2, arg3, 0);
+    } else {
+        ret = -host_to_target_errno(errno);
+    }
+
+    return ret;
+}
+
+/* pwritev(2) */
+static inline abi_long do_bsd_pwritev(void *cpu_env, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
+{
+    abi_long ret;
+    struct iovec *vec = lock_iovec(VERIFY_READ, arg2, arg3, 1);
+
+    if (vec != NULL) {
+        if (regpairs_aligned(cpu_env) != 0) {
+            arg4 = arg5;
+            arg5 = arg6;
+        }
+        ret = get_errno(safe_pwritev(arg1, vec, arg3, target_arg64(arg4, arg5)));
+        unlock_iovec(vec, arg2, arg3, 0);
+    } else {
+        ret = -host_to_target_errno(errno);
+    }
+
+    return ret;
+}
+
 #endif /* !BSD_FILE_H_ */
bsd-user/freebsd/os-syscall.c +23 -0
index dda79af53de..f52c9e3c306 100644
@@ -50,6 +50,13 @@ safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt);
 safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
     off_t, offset);
 
+safe_syscall3(ssize_t, write, int, fd, void *, buf, size_t, nbytes);
+safe_syscall4(ssize_t, pwrite, int, fd, void *, buf, size_t, nbytes, off_t,
+    offset);
+safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt);
+safe_syscall4(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
+    off_t, offset);
+
 void target_set_brk(abi_ulong new_brk)
 {
 }
@@ -236,6 +243,22 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_readv(arg1, arg2, arg3);
         break;
 
+    case TARGET_FREEBSD_NR_write: /* write(2) */
+        ret = do_bsd_write(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_pwrite: /* pwrite(2) */
+        ret = do_bsd_pwrite(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
+        break;
+
+    case TARGET_FREEBSD_NR_writev: /* writev(2) */
+        ret = do_bsd_writev(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_pwritev: /* pwritev(2) */
+        ret = do_bsd_pwritev(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
+        break;
+
     default:
         gemu_log("qemu: unsupported syscall: %d\n", num);
         ret = -TARGET_ENOSYS;
Signature · 1 line
2.33.1
#64 lore raw

PATCH 22/22 bsd-user/freebsd/os-syscall.c: Implement exit

In reply to Warner Losh

Implement the exit system call. Bring in bsd-proc.h to contain all the process system call implementation and helper routines.

---
 bsd-user/bsd-proc.h           | 43 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  7 ++++++
 2 files changed, 50 insertions(+)
 create mode 100644 bsd-user/bsd-proc.h
bsd-user/bsd-proc.h new file +43 -0
new file mode 100644
index 00000000000..8f0b6990d14
@@ -0,0 +1,43 @@
+/*
+ *  process related system call shims and definitions
+ *
+ *  Copyright (c) 2013-2014 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef BSD_PROC_H_
+#define BSD_PROC_H_
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <unistd.h>
+
+/* exit(2) */
+static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
+{
+#ifdef TARGET_GPROF
+    _mcleanup();
+#endif
+    gdb_exit(arg1);
+    qemu_plugin_user_exit();
+    /* XXX: should free thread stack and CPU env here  */
+    _exit(arg1);
+
+    return 0;
+}
+
+#endif /* !BSD_PROC_H_ */
bsd-user/freebsd/os-syscall.c +7 -0
index f52c9e3c306..f66b6a1b1f5 100644
@@ -41,6 +41,7 @@
 #include "user/syscall-trace.h"
 
 #include "bsd-file.h"
+#include "bsd-proc.h"
 
 /* I/O */
 safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes);
@@ -227,6 +228,12 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
     }
 
     switch (num) {
+        /*
+         * process system calls
+         */
+    case TARGET_FREEBSD_NR_exit: /* exit(2) */
+        ret = do_bsd_exit(cpu_env, arg1);
+        break;
 
         /*
          * File system calls.
Signature · 1 line
2.33.1
Reviewed-by on v1 1/22
#2 lore raw

Re: [PATCH 01/22] bsd-user/main.c: Drop syscall flavor arg -bsd

In reply to Warner Losh
Quoted 9 lines · Warner Losh

We've not realistically been able to actually run any bsd program on any other bsd program. They are too diverged to do this easily. The current code is setup to do it, but implementing it is hard. Stop pretending that we can do this.

---
  bsd-user/main.c | 12 ------------
  1 file changed, 12 deletions(-)

r~

Reviewed-by on v1 9/22
#27 lore raw

Re: [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here

In reply to Warner Losh
Quoted 6 lines · Warner Losh

While there is some commonality between *BSD syscall processing, there's a number of differences and the system call numbers and ABIs have been independent since the late 90s. Move FreeBSD's proessing here and delete it.

"processing"

Quoted 31 lines

The upstream implementation is somewhat different than the current implementation. It will be much easier to upstream these from scratch, justifying the final result, rather than working out the diffs and justifying the changes. Also tweak a comment to qemu standard form.

---
 bsd-user/freebsd/os-syscall.c | 68 +++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 bsd-user/freebsd/os-syscall.c
bsd-user/freebsd/os-syscall.c new file +14 -0
new file mode 100644
index 00000000000..7e2bedb918d
@@ -0,0 +1,68 @@
+/*
+ *  BSD syscalls
+ *
+ *  Copyright (c) 2003-2008 Fabrice Bellard
+ *  Copyright (c) 2013-2014 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

bsd-user/freebsd/os-syscall.c line 24

Quoted 10 lines
bsd-user/freebsd/os-syscall.c new file +10 -0
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * We need the FreeBSD "legacy" definitions. Rust needs the FreeBSD 11 system
+ * calls, so we have to emulate that despite FreeBSD being EOL'd.
+ */

... FreeBSD 11 being EOL'd.

Quoted 45 lines
bsd-user/freebsd/os-syscall.c new file +44 -1
+#define _WANT_FREEBSD11_STAT
+#define _WANT_FREEBSD11_STATFS
+#define _WANT_FREEBSD11_DIRENT
+#define _WANT_KERNEL_ERRNO
+#define _WANT_SEMUN
+#include "qemu/osdep.h"
+#include "qemu/cutils.h"
+#include "qemu/path.h"
+#include <sys/syscall.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <utime.h>
+
+#include "qemu.h"
+#include "qemu-common.h"
+#include "signal-common.h"
+#include "user/syscall-trace.h"
+
+void target_set_brk(abi_ulong new_brk)
+{
+}
+
+bool is_error(abi_long ret)
+{
+
+    return (abi_ulong)ret >= (abi_ulong)(-4096);
+}
+
+/*
+ * do_syscall() should always have a single exit point at the end so that
+ * actions, such as logging of syscall results, can be performed.  All errnos
+ * that do_syscall() returns must be -TARGET_<errcode>.
+ */
+abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
+                            abi_long arg2, abi_long arg3, abi_long arg4,
+                            abi_long arg5, abi_long arg6, abi_long arg7,
+                            abi_long arg8)
+{
+    return 0;
+}
+
+void syscall_init(void)
+{
+}
--
Quoted 2 lines

2.33.1

Small typo + omission, otherwise:

Reviewed-by on v1 2/22
#4 lore raw

Re: [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64

In reply to Warner Losh
Quoted 9 lines · Warner Losh

Although initial versions of NetBSD did use int $80, it was replaced by syscall before any releases. OpenBSD and FreeBSD always did syscall.

---
 bsd-user/x86_64/target_arch_cpu.h | 58 -------------------------------
 1 file changed, 58 deletions(-)
Quoted 72 lines
bsd-user/x86_64/target_arch_cpu.h +0 -59
index 0a9c0f08946..9dc52d5afc4 100644
@@ -124,64 +124,6 @@ static inline void target_cpu_loop(CPUX86State *env)
         process_queued_cpu_work(cs);
 
         switch (trapnr) {
-        case 0x80:
-            /* syscall from int $0x80 */
-            if (bsd_type == target_freebsd) {
-                abi_ulong params = (abi_ulong) env->regs[R_ESP] +
-                    sizeof(int32_t);
-                int32_t syscall_nr = env->regs[R_EAX];
-                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-
-                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int32_t);
-                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int64_t);
-                }
-                get_user_s32(arg1, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg2, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg3, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg4, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg5, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg6, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg7, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg8, params);
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      syscall_nr,
-                                                      arg1,
-                                                      arg2,
-                                                      arg3,
-                                                      arg4,
-                                                      arg5,
-                                                      arg6,
-                                                      arg7,
-                                                      arg8);
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EBX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_EBP]);
-            }
-            if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
-                env->regs[R_EAX] = -env->regs[R_EAX];
-                env->eflags |= CC_C;
-            } else {
-                env->eflags &= ~CC_C;
-            }
-            break;
-
         case EXCP_SYSCALL:
             /* syscall from syscall instruction */
             if (bsd_type == target_freebsd) {
--

2.33.1

Reviewed-by on v1 4/22
#11 lore raw

Re: [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: Remove openbsd syscall

In reply to Warner Losh
Quoted 3 lines · Warner Losh

This doesn't build on openbsd at the moment, and this could should arguably be in bsd-user/*bsd/i386 somewhere. Until

could or should, let's pick one and drop the other. :-)

Quoted 7 lines

we refactor to support OpenBSD/NetBSD again, drop it here.

---
 bsd-user/i386/target_arch_cpu.h | 84 +++++++++++++++------------------
 1 file changed, 37 insertions(+), 47 deletions(-)
Quoted 100 lines
bsd-user/i386/target_arch_cpu.h +37 -48
index 3cbf69d8af2..9da22202d48 100644
@@ -116,55 +116,45 @@ static inline void target_cpu_loop(CPUX86State *env)
         process_queued_cpu_work(cs);
 
         switch (trapnr) {
-        case 0x80:
+        case 0x80: {
             /* syscall from int $0x80 */
-            if (bsd_type == target_freebsd) {
-                abi_ulong params = (abi_ulong) env->regs[R_ESP] +
-                    sizeof(int32_t);
-                int32_t syscall_nr = env->regs[R_EAX];
-                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-
-                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int32_t);
-                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
-                    get_user_s32(syscall_nr, params);
-                    params += sizeof(int64_t);
-                }
-                get_user_s32(arg1, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg2, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg3, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg4, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg5, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg6, params);
-                params += sizeof(int32_t);
-                get_user_s32(arg7, params);
+            abi_ulong params = (abi_ulong) env->regs[R_ESP] +
+                sizeof(int32_t);
+            int32_t syscall_nr = env->regs[R_EAX];
+            int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
+
+            if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
+                get_user_s32(syscall_nr, params);
                 params += sizeof(int32_t);
-                get_user_s32(arg8, params);
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      syscall_nr,
-                                                      arg1,
-                                                      arg2,
-                                                      arg3,
-                                                      arg4,
-                                                      arg5,
-                                                      arg6,
-                                                      arg7,
-                                                      arg8);
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EBX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_EBP]);
+            } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
+                get_user_s32(syscall_nr, params);
+                params += sizeof(int64_t);
+            }
+            get_user_s32(arg1, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg2, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg3, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg4, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg5, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg6, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg7, params);
+            params += sizeof(int32_t);
+            get_user_s32(arg8, params);
+            env->regs[R_EAX] = do_freebsd_syscall(env,
+                                                  syscall_nr,
+                                                  arg1,
+                                                  arg2,
+                                                  arg3,
+                                                  arg4,
+                                                  arg5,
+                                                  arg6,
+                                                  arg7,
+                                                  arg8);
             }
             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
                 env->regs[R_EAX] = -env->regs[R_EAX];
--

2.33.1

Reviewed-by on v1 17/22
#52 lore raw

Re: [PATCH 17/22] bsd-user: introduce target.h

In reply to Warner Losh
Quoted 12 lines · Warner Losh

Create target.h. This file is intended to be simple and describe basic things about the architecture. If something is a basic feature of the architecture, it belongs here. Should we need something that's per-BSD there will be a target-os.h that will live in the per-bsd directories.

Define regpairs_aligned to reflect whether or not registers are 'paired' for 64-bit arguments or not. This will be false for all 64-bit targets, and will be true on those architectures that pair (currently just armv7 and powerpc on FreeBSD 14.x).

Quoted 10 lines
---
 bsd-user/arm/target.h    | 21 +++++++++++++++++++++
 bsd-user/i386/target.h   | 21 +++++++++++++++++++++
 bsd-user/qemu.h          |  1 +
 bsd-user/x86_64/target.h | 21 +++++++++++++++++++++
 4 files changed, 64 insertions(+)
 create mode 100644 bsd-user/arm/target.h
 create mode 100644 bsd-user/i386/target.h
 create mode 100644 bsd-user/x86_64/target.h
Quoted 96 lines
bsd-user/arm/target.h new file +21 -0
new file mode 100644
index 00000000000..1f7ee49bfb4
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all i386 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TARGET_H
+#define TARGET_H
+
+/*
+ * arm EABI 'lumps' the registers for 64-bit args.
+ */
+static inline int regpairs_aligned(void *cpu_env)
+{
+    return 1;
+}
+
+#endif /* ! TARGET_H */
+
bsd-user/i386/target.h new file +21 -0
new file mode 100644
index 00000000000..b0ab477d683
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all i386 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TARGET_ARCH_H
+#define TARGET_ARCH_H
+
+/*
+ * i386 doesn't 'lump' the registers for 64-bit args.
+ */
+static inline int regpairs_aligned(void *cpu_env)
+{
+    return 0;
+}
+
+#endif /* ! TARGET_ARCH_H */
+
bsd-user/qemu.h +1 -0
index 56042ddbc5d..a9efa807b78 100644
@@ -35,6 +35,7 @@ extern char **environ;
 #include "target_syscall.h"
 #include "target_os_vmparam.h"
 #include "target_os_signal.h"
+#include "target.h"
 #include "exec/gdbstub.h"
 
 /*
bsd-user/x86_64/target.h new file +21 -1
new file mode 100644
index 00000000000..6d3aef8fc49
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all x86_64 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TARGET_H
+#define TARGET_H
+
+/*
+ * x86 doesn't 'lump' the registers for 64-bit args, all args are 64 bits.
+ */
+static inline int regpairs_aligned(void *cpu_env)
+{
+    return 0;
+}
+
+#endif /* ! TARGET_H */
+
--

2.33.1

Reviewed-by on v1 6/22
#18 lore raw

Re: [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target

In reply to Warner Losh
Quoted 7 lines · Warner Losh

Since we can't run on anything else, assume for the moment that this is a FreeBSD target. In the future, we'll need to handle this properly via some include file in bsd-user/*bsd/arm/mumble.h. There's a number of other diffs that would be needed to make things work on OtherBSD, so it doesn't make sense to preseve this one detail today.

Quoted 5 lines
---
 bsd-user/arm/target_arch_thread.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 18 lines
bsd-user/arm/target_arch_thread.h +1 -4
index 11c7f765838..fcafca2408c 100644
@@ -62,9 +62,7 @@ static inline void target_thread_init(struct target_pt_regs *regs,
     }
     regs->ARM_pc = infop->entry & 0xfffffffe;
     regs->ARM_sp = stack;
-    if (bsd_type == target_freebsd) {
-        regs->ARM_lr = infop->entry & 0xfffffffe;
-    }
+    regs->ARM_lr = infop->entry & 0xfffffffe;
     /*
      * FreeBSD kernel passes the ps_strings pointer in r0. This is used by some
      * programs to set status messages that we see in ps. bsd-user doesn't
--

2.33.1

Reviewed-by on v1 3/22
#7 lore raw

Re: [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall

In reply to Warner Losh
Quoted 10 lines · Warner Losh

This doesn't build on openbsd at the moment, and this could should arguably be in bsd-user/*bsd/x86_64 somewhere. Until we refactor to support OpenBSD/NetBSD again, drop it here.

---
 bsd-user/x86_64/target_arch_cpu.h | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

As a general comment, I'd like to reach out to the others at some point and gauge interest/ability to participate, but I definitely agree that it would be better to drop !FreeBSD for now to simplify upcoming improvements to the common core. I'm not aware of any other forks that have tried to maintain bsd-user on their platforms.

Quoted 41 lines
bsd-user/x86_64/target_arch_cpu.h +8 -20
index 9dc52d5afc4..5be2f02416e 100644
@@ -126,25 +126,14 @@ static inline void target_cpu_loop(CPUX86State *env)
         switch (trapnr) {
         case EXCP_SYSCALL:
             /* syscall from syscall instruction */
-            if (bsd_type == target_freebsd) {
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[8],
-                                                      env->regs[9], 0, 0);
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDX],
-                                                      env->regs[10],
-                                                      env->regs[8],
-                                                      env->regs[9]);
-            }
+            env->regs[R_EAX] = do_freebsd_syscall(env,
+                                                  env->regs[R_EAX],
+                                                  env->regs[R_EDI],
+                                                  env->regs[R_ESI],
+                                                  env->regs[R_EDX],
+                                                  env->regs[R_ECX],
+                                                  env->regs[8],
+                                                  env->regs[9], 0, 0);
             env->eip = env->exception_next_eip;
             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
                 env->regs[R_EAX] = -env->regs[R_EAX];
--

2.33.1

Reviewed-by on v1 5/22
#14 lore raw

Re: [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls

In reply to Warner Losh
Quoted 163 lines · Warner Losh

Since we don't build on OpenBSD, only do FreeBSD system calls here. In the future, we'll need to move this to some place like bsd-user/freebsd/arm/mumble.h, but until then just leave this inline. This reflects changes to the upstream.

---
 bsd-user/arm/target_arch_cpu.h | 139 ++++++++++++++++-----------------
 1 file changed, 66 insertions(+), 73 deletions(-)
bsd-user/arm/target_arch_cpu.h +62 -71
index b087db48fa4..afb7814a8d1 100644
@@ -40,7 +40,6 @@ static inline void target_cpu_init(CPUARMState *env,
 static inline void target_cpu_loop(CPUARMState *env)
 {
     int trapnr, si_signo, si_code;
-    unsigned int n;
     CPUState *cs = env_cpu(env);
 
     for (;;) {
@@ -66,82 +65,76 @@ static inline void target_cpu_loop(CPUARMState *env)
             break;
         case EXCP_SWI:
             {
-                n = env->regs[7];
-                if (bsd_type == target_freebsd) {
-                    int ret;
-                    abi_ulong params = get_sp_from_cpustate(env);
-                    int32_t syscall_nr = n;
-                    int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
+                int ret;
+                abi_ulong params = get_sp_from_cpustate(env);
+                int32_t syscall_nr = env->regs[7];
+                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
 
-                    /* See arm/arm/syscall.c cpu_fetch_syscall_args() */
-                    if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
-                        syscall_nr = env->regs[0];
-                        arg1 = env->regs[1];
-                        arg2 = env->regs[2];
-                        arg3 = env->regs[3];
-                        get_user_s32(arg4, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg7, params);
-                        arg8 = 0;
-                    } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
-                        syscall_nr = env->regs[0];
-                        arg1 = env->regs[2];
-                        arg2 = env->regs[3];
-                        get_user_s32(arg3, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg4, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        arg7 = 0;
-                        arg8 = 0;
-                    } else {
-                        arg1 = env->regs[0];
-                        arg2 = env->regs[1];
-                        arg3 = env->regs[2];
-                        arg4 = env->regs[3];
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg7, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg8, params);
-                    }
-                    ret = do_freebsd_syscall(env, syscall_nr, arg1, arg2, arg3,
-                            arg4, arg5, arg6, arg7, arg8);
+                /* See arm/arm/syscall.c cpu_fetch_syscall_args() */
+                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
+                    syscall_nr = env->regs[0];
+                    arg1 = env->regs[1];
+                    arg2 = env->regs[2];
+                    arg3 = env->regs[3];
+                    get_user_s32(arg4, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg7, params);
+                    arg8 = 0;
+                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
+                    syscall_nr = env->regs[0];
+                    arg1 = env->regs[2];
+                    arg2 = env->regs[3];
+                    get_user_s32(arg3, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg4, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    arg7 = 0;
+                    arg8 = 0;
+                } else {
+                    arg1 = env->regs[0];
+                    arg2 = env->regs[1];
+                    arg3 = env->regs[2];
+                    arg4 = env->regs[3];
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg7, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg8, params);
+                }
+                ret = do_freebsd_syscall(env, syscall_nr, arg1, arg2, arg3,
+                                         arg4, arg5, arg6, arg7, arg8);
+                /*
+                 * Compare to arm/arm/vm_machdep.c
+                 * cpu_set_syscall_retval()
+                 */
+                if (-TARGET_EJUSTRETURN == ret) {
                     /*
-                     * Compare to arm/arm/vm_machdep.c
-                     * cpu_set_syscall_retval()
+                     * Returning from a successful sigreturn syscall.
+                     * Avoid clobbering register state.
                      */
-                    if (-TARGET_EJUSTRETURN == ret) {
-                        /*
-                         * Returning from a successful sigreturn syscall.
-                         * Avoid clobbering register state.
-                         */
-                        break;
-                    }
-                    if (-TARGET_ERESTART == ret) {
-                        env->regs[15] -= env->thumb ? 2 : 4;
-                        break;
-                    }
-                    if ((unsigned int)ret >= (unsigned int)(-515)) {
-                        ret = -ret;
-                        cpsr_write(env, CPSR_C, CPSR_C, CPSRWriteByInstr);
-                        env->regs[0] = ret;
-                    } else {
-                        cpsr_write(env, 0, CPSR_C, CPSRWriteByInstr);
-                        env->regs[0] = ret; /* XXX need to handle lseek()? */
-                        /* env->regs[1] = 0; */
-                    }
+                    break;
+                }
+                if (-TARGET_ERESTART == ret) {
+                    env->regs[15] -= env->thumb ? 2 : 4;
+                    break;
+                }
+                if ((unsigned int)ret >= (unsigned int)(-515)) {
+                    ret = -ret;
+                    cpsr_write(env, CPSR_C, CPSR_C, CPSRWriteByInstr);

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env) · line 140

Quoted 10 lines
bsd-user/arm/target_arch_cpu.h +4 -2
+                    env->regs[0] = ret;
                 } else {
-                    fprintf(stderr, "qemu: bsd_type (= %d) syscall "
-                            "not supported\n", bsd_type);
+                    cpsr_write(env, 0, CPSR_C, CPSRWriteByInstr);
+                    env->regs[0] = ret; /* XXX need to handle lseek()? */
+                    /* env->regs[1] = 0; */
                 }
             }
             break;

We should probably fix the lseek() situation sooner rather than later, but:

Reviewed-by on v1 11/22
#32 lore raw

Re: [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove

In reply to Warner Losh
Quoted 7 lines · Warner Losh
---
 bsd-user/syscall.c | 516 ---------------------------------------------
 1 file changed, 516 deletions(-)
 delete mode 100644 bsd-user/syscall.c
Quoted 525 lines
bsd-user/syscall.c deleted +0 -517
deleted file mode 100644
index d3322760f43..00000000000
@@ -1,516 +0,0 @@
-/*
- *  BSD syscalls
- *
- *  Copyright (c) 2003 - 2008 Fabrice Bellard
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, see <http://www.gnu.org/licenses/>.
- */
-#include "qemu/osdep.h"
-#include "qemu/cutils.h"
-#include "qemu/path.h"
-#include <sys/syscall.h>
-#include <sys/param.h>
-#include <sys/sysctl.h>
-#include <utime.h>
-
-#include "qemu.h"
-#include "qemu-common.h"
-#include "user/syscall-trace.h"
-
-//#define DEBUG
-
-static abi_ulong target_brk;
-static abi_ulong target_original_brk;
-
-abi_long get_errno(abi_long ret)
-{
-    if (ret == -1) {
-        /* XXX need to translate host -> target errnos here */
-        return -(errno);
-    }
-    return ret;
-}
-
-#define target_to_host_bitmask(x, tbl) (x)
-
-bool is_error(abi_long ret)
-{
-    return (abi_ulong)ret >= (abi_ulong)(-4096);
-}
-
-void target_set_brk(abi_ulong new_brk)
-{
-    target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
-}
-
-/* do_obreak() must return target errnos. */
-static abi_long do_obreak(abi_ulong new_brk)
-{
-    abi_ulong brk_page;
-    abi_long mapped_addr;
-    int new_alloc_size;
-
-    if (!new_brk)
-        return 0;
-    if (new_brk < target_original_brk)
-        return -TARGET_EINVAL;
-
-    brk_page = HOST_PAGE_ALIGN(target_brk);
-
-    /* If the new brk is less than this, set it and we're done... */
-    if (new_brk < brk_page) {
-        target_brk = new_brk;
-        return 0;
-    }
-
-    /* We need to allocate more memory after the brk... */
-    new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
-    mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
-                                        PROT_READ|PROT_WRITE,
-                                        MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0));
-
-    if (!is_error(mapped_addr))
-        target_brk = new_brk;
-    else
-        return mapped_addr;
-
-    return 0;
-}
-
-#ifdef __FreeBSD__
-/*
- * XXX this uses the undocumented oidfmt interface to find the kind of
- * a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
- * (this is mostly copied from 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.
- * XXX 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:
-        *(uint32_t *)holdp = tswap32(*(long *)holdp);
-        break;
-#else
-    case CTLTYPE_LONG:
-        *(uint64_t *)holdp = tswap64(*(long *)holdp);
-        break;
-    case CTLTYPE_ULONG:
-        *(uint64_t *)holdp = tswap64(*(unsigned long *)holdp);
-        break;
-#endif
-#ifdef CTLTYPE_U64
-    case CTLTYPE_S64:
-    case CTLTYPE_U64:
-#else
-    case CTLTYPE_QUAD:
-#endif
-        *(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
-        break;
-    case CTLTYPE_STRING:
-        break;
-    default:
-        /* XXX unhandled */
-        return -1;
-    }
-    return 0;
-}
-
-/* XXX this needs to be emulated on non-FreeBSD hosts... */
-static abi_long do_freebsd_sysctl(abi_ulong namep, int32_t namelen, abi_ulong oldp,
-                          abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
-{
-    abi_long ret;
-    void *hnamep, *holdp, *hnewp = NULL;
-    size_t holdlen;
-    abi_ulong oldlen = 0;
-    int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
-    uint32_t kind = 0;
-
-    if (oldlenp)
-        get_user_ual(oldlen, oldlenp);
-    if (!(hnamep = lock_user(VERIFY_READ, namep, namelen, 1)))
-        return -TARGET_EFAULT;
-    if (newp && !(hnewp = lock_user(VERIFY_READ, newp, newlen, 1)))
-        return -TARGET_EFAULT;
-    if (!(holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0)))
-        return -TARGET_EFAULT;
-    holdlen = oldlen;
-    for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++)
-       *q++ = tswap32(*p);
-    oidfmt(snamep, namelen, NULL, &kind);
-    /* XXX swap hnewp */
-    ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
-    if (!ret)
-        sysctl_oldcvt(holdp, holdlen, kind);
-    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;
-}
-#endif
-
-/* FIXME
- * lock_iovec()/unlock_iovec() have a return code of 0 for success where
- * other lock functions have a return code of 0 for failure.
- */
-static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
-                           int count, int copy)
-{
-    struct target_iovec *target_vec;
-    abi_ulong base;
-    int i;
-
-    target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
-    if (!target_vec)
-        return -TARGET_EFAULT;
-    for (i = 0;i < count; i++) {
-        base = tswapl(target_vec[i].iov_base);
-        vec[i].iov_len = tswapl(target_vec[i].iov_len);
-        if (vec[i].iov_len != 0) {
-            vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
-            /* Don't check lock_user return value. We must call writev even
-               if a element has invalid base address. */
-        } else {
-            /* zero length pointer is ignored */
-            vec[i].iov_base = NULL;
-        }
-    }
-    unlock_user (target_vec, target_addr, 0);
-    return 0;
-}
-
-static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
-                             int count, int copy)
-{
-    struct target_iovec *target_vec;
-    abi_ulong base;
-    int i;
-
-    target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
-    if (!target_vec)
-        return -TARGET_EFAULT;
-    for (i = 0;i < count; i++) {
-        if (target_vec[i].iov_base) {
-            base = tswapl(target_vec[i].iov_base);
-            unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
-        }
-    }
-    unlock_user (target_vec, target_addr, 0);
-
-    return 0;
-}
-
-/* do_syscall() should always have a single exit point at the end so
-   that actions, such as logging of syscall results, can be performed.
-   All errnos that do_syscall() returns must be -TARGET_<errcode>. */
-abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
-                            abi_long arg2, abi_long arg3, abi_long arg4,
-                            abi_long arg5, abi_long arg6, abi_long arg7,
-                            abi_long arg8)
-{
-    CPUState *cpu = env_cpu(cpu_env);
-    abi_long ret;
-    void *p;
-
-#ifdef DEBUG
-    gemu_log("freebsd syscall %d\n", num);
-#endif
-    record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
-
-    if (do_strace)
-        print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-
-    switch (num) {
-    case TARGET_FREEBSD_NR_exit:
-#ifdef CONFIG_GPROF
-        _mcleanup();
-#endif
-        gdb_exit(arg1);
-        qemu_plugin_user_exit();
-        /* XXX: should free thread stack and CPU env */
-        _exit(arg1);
-        ret = 0; /* avoid warning */
-        break;
-    case TARGET_FREEBSD_NR_read:
-        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
-            goto efault;
-        ret = get_errno(read(arg1, p, arg3));
-        unlock_user(p, arg2, ret);
-        break;
-    case TARGET_FREEBSD_NR_write:
-        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
-            goto efault;
-        ret = get_errno(write(arg1, p, arg3));
-        unlock_user(p, arg2, 0);
-        break;
-    case TARGET_FREEBSD_NR_writev:
-        {
-            int count = arg3;
-            struct iovec *vec;
-
-            vec = alloca(count * sizeof(struct iovec));
-            if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
-                goto efault;
-            ret = get_errno(writev(arg1, vec, count));
-            unlock_iovec(vec, arg2, count, 0);
-        }
-        break;
-    case TARGET_FREEBSD_NR_open:
-        if (!(p = lock_user_string(arg1)))
-            goto efault;
-        ret = get_errno(open(path(p),
-                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
-                             arg3));
-        unlock_user(p, arg1, 0);
-        break;
-    case TARGET_FREEBSD_NR_mmap:
-        ret = get_errno(target_mmap(arg1, arg2, arg3,
-                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
-                                    arg5,
-                                    arg6));
-        break;
-    case TARGET_FREEBSD_NR_mprotect:
-        ret = get_errno(target_mprotect(arg1, arg2, arg3));
-        break;
-    case TARGET_FREEBSD_NR_break:
-        ret = do_obreak(arg1);
-        break;
-#ifdef __FreeBSD__
-    case TARGET_FREEBSD_NR___sysctl:
-        ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
-        break;
-#endif
-    case TARGET_FREEBSD_NR_sysarch:
-        ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
-        break;
-    case TARGET_FREEBSD_NR_syscall:
-    case TARGET_FREEBSD_NR___syscall:
-        ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,arg7,arg8,0);
-        break;
-    default:
-        ret = get_errno(syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
-        break;
-    }
- fail:
-#ifdef DEBUG
-    gemu_log(" = %ld\n", ret);
-#endif
-    if (do_strace)
-        print_freebsd_syscall_ret(num, ret);
-
-    record_syscall_return(cpu, num, ret);
-    return ret;
- efault:
-    ret = -TARGET_EFAULT;
-    goto fail;
-}
-
-abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
-                           abi_long arg2, abi_long arg3, abi_long arg4,
-                           abi_long arg5, abi_long arg6)
-{
-    CPUState *cpu = env_cpu(cpu_env);
-    abi_long ret;
-    void *p;
-
-#ifdef DEBUG
-    gemu_log("netbsd syscall %d\n", num);
-#endif
-
-    record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
-
-    if (do_strace)
-        print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-
-    switch (num) {
-    case TARGET_NETBSD_NR_exit:
-#ifdef CONFIG_GPROF
-        _mcleanup();
-#endif
-        gdb_exit(arg1);
-        qemu_plugin_user_exit();
-        /* XXX: should free thread stack and CPU env */
-        _exit(arg1);
-        ret = 0; /* avoid warning */
-        break;
-    case TARGET_NETBSD_NR_read:
-        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
-            goto efault;
-        ret = get_errno(read(arg1, p, arg3));
-        unlock_user(p, arg2, ret);
-        break;
-    case TARGET_NETBSD_NR_write:
-        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
-            goto efault;
-        ret = get_errno(write(arg1, p, arg3));
-        unlock_user(p, arg2, 0);
-        break;
-    case TARGET_NETBSD_NR_open:
-        if (!(p = lock_user_string(arg1)))
-            goto efault;
-        ret = get_errno(open(path(p),
-                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
-                             arg3));
-        unlock_user(p, arg1, 0);
-        break;
-    case TARGET_NETBSD_NR_mmap:
-        ret = get_errno(target_mmap(arg1, arg2, arg3,
-                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
-                                    arg5,
-                                    arg6));
-        break;
-    case TARGET_NETBSD_NR_mprotect:
-        ret = get_errno(target_mprotect(arg1, arg2, arg3));
-        break;
-    case TARGET_NETBSD_NR_syscall:
-    case TARGET_NETBSD_NR___syscall:
-        ret = do_netbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
-        break;
-    default:
-        ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-        break;
-    }
- fail:
-#ifdef DEBUG
-    gemu_log(" = %ld\n", ret);
-#endif
-    if (do_strace)
-        print_netbsd_syscall_ret(num, ret);
-
-    record_syscall_return(cpu, num, ret);
-    return ret;
- efault:
-    ret = -TARGET_EFAULT;
-    goto fail;
-}
-
-abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
-                            abi_long arg2, abi_long arg3, abi_long arg4,
-                            abi_long arg5, abi_long arg6)
-{
-    CPUState *cpu = env_cpu(cpu_env);
-    abi_long ret;
-    void *p;
-
-#ifdef DEBUG
-    gemu_log("openbsd syscall %d\n", num);
-#endif
-
-    record_syscall_start(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
-
-    if (do_strace)
-        print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-
-    switch (num) {
-    case TARGET_OPENBSD_NR_exit:
-#ifdef CONFIG_GPROF
-        _mcleanup();
-#endif
-        gdb_exit(arg1);
-        qemu_plugin_user_exit();
-        /* XXX: should free thread stack and CPU env */
-        _exit(arg1);
-        ret = 0; /* avoid warning */
-        break;
-    case TARGET_OPENBSD_NR_read:
-        if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
-            goto efault;
-        ret = get_errno(read(arg1, p, arg3));
-        unlock_user(p, arg2, ret);
-        break;
-    case TARGET_OPENBSD_NR_write:
-        if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
-            goto efault;
-        ret = get_errno(write(arg1, p, arg3));
-        unlock_user(p, arg2, 0);
-        break;
-    case TARGET_OPENBSD_NR_open:
-        if (!(p = lock_user_string(arg1)))
-            goto efault;
-        ret = get_errno(open(path(p),
-                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
-                             arg3));
-        unlock_user(p, arg1, 0);
-        break;
-    case TARGET_OPENBSD_NR_mmap:
-        ret = get_errno(target_mmap(arg1, arg2, arg3,
-                                    target_to_host_bitmask(arg4, mmap_flags_tbl),
-                                    arg5,
-                                    arg6));
-        break;
-    case TARGET_OPENBSD_NR_mprotect:
-        ret = get_errno(target_mprotect(arg1, arg2, arg3));
-        break;
-    case TARGET_OPENBSD_NR_syscall:
-    case TARGET_OPENBSD_NR___syscall:
-        ret = do_openbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
-        break;
-    default:
-        ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
-        break;
-    }
- fail:
-#ifdef DEBUG
-    gemu_log(" = %ld\n", ret);
-#endif
-    if (do_strace)
-        print_openbsd_syscall_ret(num, ret);
-
-    record_syscall_return(cpu, num, ret);
-    return ret;
- efault:
-    ret = -TARGET_EFAULT;
-    goto fail;
-}
-
-void syscall_init(void)
-{
-}
--

2.33.1

Reviewed-by on v1 2/22
#5 lore raw

Re: [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64

In reply to Warner Losh
Quoted 7 lines · Warner Losh

Although initial versions of NetBSD did use int $80, it was replaced by syscall before any releases. OpenBSD and FreeBSD always did syscall.

---
  bsd-user/x86_64/target_arch_cpu.h | 58 -------------------------------
  1 file changed, 58 deletions(-)

r~

Reviewed-by on v1 8/22
#24 lore raw

Re: [PATCH 08/22] bsd-user: Remove bsd_type

In reply to Warner Losh
Quoted 8 lines · Warner Losh

Remove keeping track of which type of bsd we're running on. It's no longer referenced in the code. Building bsd-user on NetBSD or OpenBSD isn't possible, let alone running that code. Stop pretending that we can do the cross BSD thing since there's been a large divergence since 2000 that makes this nearly impossible between FreeBSD and {Net,Open}BSD and at least quite difficult between NetBSD and OpenBSD.

Quoted 6 lines
---
 bsd-user/main.c | 2 --
 bsd-user/qemu.h | 7 -------
 2 files changed, 9 deletions(-)
Quoted 41 lines
bsd-user/main.c +0 -2
index bddb830e99b..88d347d05eb 100644
@@ -96,7 +96,6 @@ unsigned long reserved_va;
 
 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
-enum BSDType bsd_type;
 char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
 
 unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
@@ -284,7 +283,6 @@ int main(int argc, char **argv)
     const char *gdbstub = NULL;
     char **target_environ, **wrk;
     envlist_t *envlist = NULL;
-    bsd_type = HOST_DEFAULT_BSD_TYPE;
     char *argv0 = NULL;
 
     adjust_ssize();
bsd-user/qemu.h +0 -8
index 02921ac8b3b..e5742bd6c03 100644
@@ -29,13 +29,6 @@
 
 extern char **environ;
 
-enum BSDType {
-    target_freebsd,
-    target_netbsd,
-    target_openbsd,
-};
-extern enum BSDType bsd_type;
-
 #include "exec/user/thunk.h"
 #include "target_arch.h"
 #include "syscall_defs.h"
--

2.33.1

Reviewed-by on v1 6/22
#19 lore raw

Re: [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target

In reply to Warner Losh
Quoted 10 lines · Warner Losh

Since we can't run on anything else, assume for the moment that this is a FreeBSD target. In the future, we'll need to handle this properly via some include file in bsd-user/*bsd/arm/mumble.h. There's a number of other diffs that would be needed to make things work on OtherBSD, so it doesn't make sense to preseve this one detail today.

---
  bsd-user/arm/target_arch_thread.h | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)

r~

Reviewed-by on v1 18/22
#55 lore raw

Re: [PATCH 18/22] bsd-user: Define target_arg64

In reply to Warner Losh
Quoted 6 lines · Warner Losh

target_arg64 is a generic way to extract 64-bits from a pair of arguments. On 32-bit platforms, it returns them joined together as appropriate. On 64-bit platforms, it returns the first arg because it's already 64-bits.

Quoted 6 lines
---
 bsd-user/qemu.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Quoted 27 lines
bsd-user/qemu.h +13 -1
index a9efa807b78..af272c2a802 100644
@@ -462,6 +462,19 @@ static inline void *lock_user_string(abi_ulong guest_addr)
 #define unlock_user_struct(host_ptr, guest_addr, copy)          \
     unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
 
+static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
+{
+#if TARGET_ABI_BITS == 32
+#ifdef TARGET_WORDS_BIGENDIAN
+    return ((uint64_t)word0 << 32) | word1;
+#else
+    return ((uint64_t)word1 << 32) | word0;
+#endif
+#else /* TARGET_ABI_BITS != 32 */
+    return word0;
+#endif /* TARGET_ABI_BITS != 32 */
+}
+
 #include <pthread.h>
 
 #include "user/safe-syscall.h"
--

2.33.1

Reviewed-by on v1 12/22
#35 lore raw

Re: [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno

In reply to Warner Losh
Quoted 7 lines · Warner Losh

Add the helper functions get_errno and host_to_target_errno. get_errno returns either the system call results, or the -errno when system call indicates failure by returning -1. Host_to_target_errno returns errno (since on FreeBSD they are the same on all architectures) along with a comment about why it's the identity.

Quoted 6 lines
---
 bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
 bsd-user/qemu.h               |  3 ++-
 2 files changed, 25 insertions(+), 1 deletion(-)
Quoted 53 lines
bsd-user/freebsd/os-syscall.c +23 -0
index 7e2bedb918d..2e84cf350b1 100644
@@ -44,6 +44,29 @@ void target_set_brk(abi_ulong new_brk)
 {
 }
 
+/*
+ * errno conversion.
+ */
+abi_long get_errno(abi_long ret)
+{
+
+    if (ret == -1) {
+        return -host_to_target_errno(errno);
+    } else {
+        return ret;
+    }
+}
+
+int host_to_target_errno(int err)
+{
+    /*
+     * All the BSDs have the property that the error numbers are uniform across
+     * all architectures for a given BSD, though they may vary between different
+     * BSDs.
+     */
+    return err;
+}
+
 bool is_error(abi_long ret)
 {
 
bsd-user/qemu.h +2 -2
index e5742bd6c03..56042ddbc5d 100644
@@ -246,9 +246,10 @@ extern unsigned long target_dflssiz;
 extern unsigned long target_maxssiz;
 extern unsigned long target_sgrowsiz;
 
-/* syscall.c */
+/* os-syscall.c */
 abi_long get_errno(abi_long ret);
 bool is_error(abi_long ret);
+int host_to_target_errno(int err);
 
 /* os-sys.c */
 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
--

2.33.1

Reviewed-by on v1 15/22
#47 lore raw

Re: [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec

In reply to Warner Losh
Quoted 8 lines · Warner Losh

Releases the references to the iovec created by lock_iovec.

---
 bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
Quoted 37 lines
bsd-user/freebsd/os-syscall.c +23 -1
index c21759ae7ce..d49945f0fcc 100644
@@ -167,6 +167,29 @@ struct iovec *lock_iovec(int type, abi_ulong target_addr,
     return NULL;
 }
 
+void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
+        int count, int copy)
+{
+    struct target_iovec *target_vec;
+    int i;
+
+    target_vec = lock_user(VERIFY_READ, target_addr,
+                           count * sizeof(struct target_iovec), 1);
+    if (target_vec) {
+        for (i = 0; i < count; i++) {
+            abi_ulong base = tswapal(target_vec[i].iov_base);
+            abi_long len = tswapal(target_vec[i].iov_len);
+            if (len < 0) {
+                break;
+            }
+            unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
+        }
+        unlock_user(target_vec, target_addr, 0);
+    }
+
+    free(vec);
+}
+
 /*
  * do_syscall() should always have a single exit point at the end so that
  * actions, such as logging of syscall results, can be performed.  All errnos
--

2.33.1

Reviewed-by on v1 14/22
#44 lore raw

Re: [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec

In reply to Warner Losh
Quoted 5 lines · Warner Losh

lock_iovec will lock an I/O vec and the memory to which it referrs and create a iovec in the host space that referrs to it, with full error unwinding.

s/referrs/refers/ twice

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

Two typos, otherwise seems to LGTM:

Quoted 106 lines
bsd-user/freebsd/os-syscall.c +92 -1
index 060134a9ecd..c21759ae7ce 100644
@@ -75,6 +75,98 @@ bool is_error(abi_long ret)
     return (abi_ulong)ret >= (abi_ulong)(-4096);
 }
 
+struct iovec *lock_iovec(int type, abi_ulong target_addr,
+        int count, int copy)
+{
+    struct target_iovec *target_vec;
+    struct iovec *vec;
+    abi_ulong total_len, max_len;
+    int i;
+    int err = 0;
+    bool bad_address = false;
+
+    if (count == 0) {
+        errno = 0;
+        return NULL;
+    }
+    if (count < 0 || count > IOV_MAX) {
+        errno = EINVAL;
+        return NULL;
+    }
+
+    vec = calloc(count, sizeof(struct iovec));
+    if (vec == NULL) {
+        errno = ENOMEM;
+        return NULL;
+    }
+
+    target_vec = lock_user(VERIFY_READ, target_addr,
+                           count * sizeof(struct target_iovec), 1);
+    if (target_vec == NULL) {
+        err = EFAULT;
+        goto fail2;
+    }
+
+    /*
+     * ??? If host page size > target page size, this will result in a value
+     * larger than what we can actually support.
+     */
+    max_len = 0x7fffffff & TARGET_PAGE_MASK;
+    total_len = 0;
+
+    for (i = 0; i < count; i++) {
+        abi_ulong base = tswapal(target_vec[i].iov_base);
+        abi_long len = tswapal(target_vec[i].iov_len);
+
+        if (len < 0) {
+            err = EINVAL;
+            goto fail;
+        } else if (len == 0) {
+            /* Zero length pointer is ignored.  */
+            vec[i].iov_base = 0;
+        } else {
+            vec[i].iov_base = lock_user(type, base, len, copy);
+            /*
+             * If the first buffer pointer is bad, this is a fault.  But
+             * subsequent bad buffers will result in a partial write; this is
+             * realized by filling the vector with null pointers and zero
+             * lengths.
+             */
+            if (!vec[i].iov_base) {
+                if (i == 0) {
+                    err = EFAULT;
+                    goto fail;
+                } else {
+                    bad_address = true;
+                }
+            }
+            if (bad_address) {
+                len = 0;
+            }
+            if (len > max_len - total_len) {
+                len = max_len - total_len;
+            }
+        }
+        vec[i].iov_len = len;
+        total_len += len;
+    }
+
+    unlock_user(target_vec, target_addr, 0);
+    return vec;
+
+ fail:
+    while (--i >= 0) {
+        if (tswapal(target_vec[i].iov_len) > 0) {
+            unlock_user(vec[i].iov_base, tswapal(target_vec[i].iov_base), 0);
+        }
+    }
+    unlock_user(target_vec, target_addr, 0);
+ fail2:
+    free(vec);
+    errno = err;
+    return NULL;
+}
+
 /*
  * do_syscall() should always have a single exit point at the end so that
  * actions, such as logging of syscall results, can be performed.  All errnos
--

2.33.1

Reviewed-by on v1 7/22
#21 lore raw

Re: [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: Assume a FreeBSD target

In reply to Warner Losh
Quoted 7 lines · Warner Losh

Since we can't run on anything else, assume for the moment that this is a FreeBSD target. In the future, we'll need to handle this properly via some include file in bsd-user/*bsd/x86_64/mumble.h. There's a number of other diffs that would be needed to make things work on OtherBSD, so it doesn't make sense to preseve this one detail today.

Quoted 5 lines
---
 bsd-user/x86_64/target_arch_thread.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 18 lines
bsd-user/x86_64/target_arch_thread.h +1 -4
index d105e43fd35..b745d7ffeb7 100644
@@ -32,9 +32,7 @@ static inline void target_thread_init(struct target_pt_regs *regs,
     regs->rax = 0;
     regs->rsp = infop->start_stack;
     regs->rip = infop->entry;
-    if (bsd_type == target_freebsd) {
-        regs->rdi = infop->start_stack;
-    }
+    regs->rdi = infop->start_stack;
 }
 
 #endif /* !_TARGET_ARCH_THREAD_H_ */
--

2.33.1

Reviewed-by on v1 13/22
#39 lore raw

Re: [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls

In reply to Warner Losh
Quoted 6 lines · Warner Losh

An include file that pulls in all the definitions needed for the file related system calls. This also includes the host definitions to implement the system calls and some helper routines to lock/unlock different aspects of the system call arguments.

Quoted 7 lines
---
 bsd-user/bsd-file.h           | 39 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  2 ++
 2 files changed, 41 insertions(+)
 create mode 100644 bsd-user/bsd-file.h
Quoted 61 lines
bsd-user/bsd-file.h new file +39 -0
new file mode 100644
index 00000000000..2f743db38e1
@@ -0,0 +1,39 @@
+/*
+ *  file related system call shims and definitions
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef BSD_FILE_H_
+#define BSD_FILE_H_
+
+#include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/uio.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "qemu/path.h"
+
+extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,
+        int copy);
+extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count,
+        int copy);
+
+#endif /* !BSD_FILE_H_ */
bsd-user/freebsd/os-syscall.c +2 -1
index 2e84cf350b1..060134a9ecd 100644
@@ -40,6 +40,8 @@
 #include "signal-common.h"
 #include "user/syscall-trace.h"
 
+#include "bsd-file.h"
+
 void target_set_brk(abi_ulong new_brk)
 {
 }
--

2.33.1

Reviewed-by on v1 22/22
#65 lore raw

Re: [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit

In reply to Warner Losh
Quoted 4 lines · Warner Losh

Implement the exit system call. Bring in bsd-proc.h to contain all the process system call implementation and helper routines.

Quoted 8 lines
---
 bsd-user/bsd-proc.h           | 43 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  7 ++++++
 2 files changed, 50 insertions(+)
 create mode 100644 bsd-user/bsd-proc.h
Quoted 77 lines
bsd-user/bsd-proc.h new file +43 -0
new file mode 100644
index 00000000000..8f0b6990d14
@@ -0,0 +1,43 @@
+/*
+ *  process related system call shims and definitions
+ *
+ *  Copyright (c) 2013-2014 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef BSD_PROC_H_
+#define BSD_PROC_H_
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <unistd.h>
+
+/* exit(2) */
+static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
+{
+#ifdef TARGET_GPROF
+    _mcleanup();
+#endif
+    gdb_exit(arg1);
+    qemu_plugin_user_exit();
+    /* XXX: should free thread stack and CPU env here  */
+    _exit(arg1);
+
+    return 0;
+}
+
+#endif /* !BSD_PROC_H_ */
bsd-user/freebsd/os-syscall.c +7 -1
index f52c9e3c306..f66b6a1b1f5 100644
@@ -41,6 +41,7 @@
 #include "user/syscall-trace.h"
 
 #include "bsd-file.h"
+#include "bsd-proc.h"
 
 /* I/O */
 safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes);
@@ -227,6 +228,12 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
     }
 
     switch (num) {
+        /*
+         * process system calls
+         */
+    case TARGET_FREEBSD_NR_exit: /* exit(2) */
+        ret = do_bsd_exit(cpu_env, arg1);
+        break;
 
         /*
          * File system calls.
--

2.33.1

Reviewed-by on v1 7/22
#22 lore raw

Re: [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: Assume a FreeBSD target

In reply to Warner Losh
Quoted 10 lines · Warner Losh

Since we can't run on anything else, assume for the moment that this is a FreeBSD target. In the future, we'll need to handle this properly via some include file in bsd-user/*bsd/x86_64/mumble.h. There's a number of other diffs that would be needed to make things work on OtherBSD, so it doesn't make sense to preseve this one detail today.

---
  bsd-user/x86_64/target_arch_thread.h | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)

r~

Reviewed-by on v1 8/22
#25 lore raw

Re: [PATCH 08/22] bsd-user: Remove bsd_type

In reply to Warner Losh
Quoted 7 lines · Warner Losh

Remove keeping track of which type of bsd we're running on. It's no longer referenced in the code. Building bsd-user on NetBSD or OpenBSD isn't possible, let alone running that code. Stop pretending that we can do the cross BSD thing since there's been a large divergence since 2000 that makes this nearly impossible between FreeBSD and {Net,Open}BSD and at least quite difficult between NetBSD and OpenBSD.

Quoted 5 lines
---
  bsd-user/main.c | 2 --
  bsd-user/qemu.h | 7 -------
  2 files changed, 9 deletions(-)

r~

Reviewed-by on v1 5/22
#16 lore raw

Re: [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls

In reply to Warner Losh
Quoted 9 lines · Warner Losh

Since we don't build on OpenBSD, only do FreeBSD system calls here. In the future, we'll need to move this to some place like bsd-user/freebsd/arm/mumble.h, but until then just leave this inline. This reflects changes to the upstream.

---
  bsd-user/arm/target_arch_cpu.h | 139 ++++++++++++++++-----------------
  1 file changed, 66 insertions(+), 73 deletions(-)

r~

Reviewed-by on v1 12/22
#36 lore raw

Re: [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno

In reply to Warner Losh

bsd-user/freebsd/os-syscall.c void target_set_brk(abi_ulong new_brk) · line 53

Quoted 7 lines · Warner Losh
bsd-user/freebsd/os-syscall.c +7 -0
+/*
+ * errno conversion.
+ */
+abi_long get_errno(abi_long ret)
+{
+
+    if (ret == -1) {

Watch the extra blank lines.

r~

Reviewed-by on v1 3/22
#9 lore raw

Re: [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall

In reply to Warner Losh
Quoted 8 lines · Warner Losh

This doesn't build on openbsd at the moment, and this could should arguably be in bsd-user/*bsd/x86_64 somewhere. Until we refactor to support OpenBSD/NetBSD again, drop it here.

---
  bsd-user/x86_64/target_arch_cpu.h | 27 ++++++++-------------------
  1 file changed, 8 insertions(+), 19 deletions(-)

r~

Reviewed-by on v1 4/22
#12 lore raw

Re: [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: Remove openbsd syscall

In reply to Warner Losh
Quoted 8 lines · Warner Losh

This doesn't build on openbsd at the moment, and this could should arguably be in bsd-user/*bsd/i386 somewhere. Until we refactor to support OpenBSD/NetBSD again, drop it here.

---
  bsd-user/i386/target_arch_cpu.h | 84 +++++++++++++++------------------
  1 file changed, 37 insertions(+), 47 deletions(-)

r~

Reviewed-by on v1 9/22
#28 lore raw

Re: [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here

In reply to Warner Losh
Quoted 10 lines · Warner Losh

While there is some commonality between *BSD syscall processing, there's a number of differences and the system call numbers and ABIs have been independent since the late 90s. Move FreeBSD's proessing here and delete it.

The upstream implementation is somewhat different than the current implementation. It will be much easier to upstream these from scratch, justifying the final result, rather than working out the diffs and justifying the changes. Also tweak a comment to qemu standard form.

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

create mode 100644 bsd-user/freebsd/os-syscall.c

r~

#40 lore raw

Re: [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls

In reply to Warner Losh

bsd-user/bsd-file.h line 30

Quoted 11 lines · Warner Losh
bsd-user/bsd-file.h new file +11 -0
+#ifndef BSD_FILE_H_
+#define BSD_FILE_H_
+
+#include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/uio.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>

Many of these should be done by "qemu/osdep.h" already. Otherwise I question putting them into this header, as opposed to as needed by other syscall handling c files.

r~

#48 lore raw

Re: [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec

In reply to Warner Losh
Quoted 23 lines · Warner Losh

Releases the references to the iovec created by lock_iovec.

---
  bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
  1 file changed, 23 insertions(+)
bsd-user/freebsd/os-syscall.c +8 -0
index c21759ae7ce..d49945f0fcc 100644
@@ -167,6 +167,29 @@ struct iovec *lock_iovec(int type, abi_ulong target_addr,
      return NULL;
  }
 
+void unlock_iovec(struct iovec *vec, abi_ulong target_addr,
+        int count, int copy)
+{
+    struct target_iovec *target_vec;
+    int i;
+
+    target_vec = lock_user(VERIFY_READ, target_addr,
+                           count * sizeof(struct target_iovec), 1);

bsd-user/freebsd/os-syscall.c struct iovec *lock_iovec(int type, abi_ulong target_addr, · line 187

Quoted 10 lines
bsd-user/freebsd/os-syscall.c +10 -0
+    if (target_vec) {
+        for (i = 0; i < count; i++) {
+            abi_ulong base = tswapal(target_vec[i].iov_base);
+            abi_long len = tswapal(target_vec[i].iov_len);
+            if (len < 0) {
+                break;
+            }
+            unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
+        }
+        unlock_user(target_vec, target_addr, 0);

Not quite right. Any vec[i].iov_len == 0 ought to be skipped, as per the previous patch, where we force those values for bad_address and target_vec[i].iov_len == 0.

r~

Reviewed-by on v1 21/22
#63 lore raw

Re: [PATCH 21/22] bsd-user/bsd-file.h: Meat of the write system calls

In reply to Warner Losh
Quoted 10 lines · Warner Losh

Implement write, writev, pwrite and pwritev and connect them to the system call dispatch routine.

---
  bsd-user/bsd-file.h           | 85 +++++++++++++++++++++++++++++++++++
  bsd-user/freebsd/os-syscall.c | 23 ++++++++++
  2 files changed, 108 insertions(+)

r~

#37 lore raw

Re: [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno

In reply to Richard Henderson
Quoted 11 lines · Richard Henderson

bsd-user/freebsd/os-syscall.c void target_set_brk(abi_ulong new_brk) · line 53

Quoted 7 lines · Warner Losh
bsd-user/freebsd/os-syscall.c +7 -0
+/*
+ * errno conversion.
+ */
+abi_long get_errno(abi_long ret)
+{
+
+    if (ret == -1) {

Watch the extra blank lines.

Will do. It's a FreeBSD fetish that has infected this code... :)

Quoted 2 lines

Thanks!

Warner

Quoted 4 lines

r~

Reviewed-by on v1 19/22
#58 lore raw

Re: [PATCH 19/22] bsd-user: Add safe system call macros

In reply to Warner Losh
Quoted 8 lines · Warner Losh

Add a series of macros to create system call macros that go via the safe_syscall path.

---
  bsd-user/syscall_defs.h | 47 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 47 insertions(+)

r~

#15 lore raw

Re: [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls

In reply to Kyle Evans
Quoted 177 lines · Kyle Evans
Quoted 6 lines · Warner Losh

Since we don't build on OpenBSD, only do FreeBSD system calls here. In the future, we'll need to move this to some place like bsd-user/freebsd/arm/mumble.h, but until then just leave this inline. This reflects changes to the upstream.

Review

Quoted 6 lines
---
 bsd-user/arm/target_arch_cpu.h | 139 ++++++++++++++++-----------------
 1 file changed, 66 insertions(+), 73 deletions(-)
(quoted diff) +0 -0

b/bsd-user/arm/target_arch_cpu.h

Quoted 12 lines

index b087db48fa4..afb7814a8d1 100644

bsd-user/arm/target_arch_cpu.h +0 -1
@@ -40,7 +40,6 @@ static inline void target_cpu_init(CPUARMState *env,
 static inline void target_cpu_loop(CPUARMState *env)
 {
     int trapnr, si_signo, si_code;
-    unsigned int n;
     CPUState *cs = env_cpu(env);
 
     for (;;) {

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env) · line 74

Quoted 9 lines
bsd-user/arm/target_arch_cpu.h +0 -6
@@ -66,82 +65,76 @@ static inline void target_cpu_loop(CPUARMState *env)
             break;
         case EXCP_SWI:
             {
-                n = env->regs[7];
-                if (bsd_type == target_freebsd) {
-                    int ret;
-                    abi_ulong params = get_sp_from_cpustate(env);
-                    int32_t syscall_nr = n;
-                    int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7,

arg8;

Quoted 10 lines
bsd-user/arm/target_arch_cpu.h +4 -5
+                int ret;
+                abi_ulong params = get_sp_from_cpustate(env);
+                int32_t syscall_nr = env->regs[7];
+                int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
 
-                    /* See arm/arm/syscall.c cpu_fetch_syscall_args() */
-                    if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
-                        syscall_nr = env->regs[0];
-                        arg1 = env->regs[1];
-                        arg2 = env->regs[2];

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env)

Quoted 10 lines
bsd-user/arm/target_arch_cpu.h +0 -10
-                        arg3 = env->regs[3];
-                        get_user_s32(arg4, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg7, params);
-                        arg8 = 0;
-                    } else if (syscall_nr ==

TARGET_FREEBSD_NR___syscall) {

Quoted 16 lines
bsd-user/arm/target_arch_cpu.h +0 -16
-                        syscall_nr = env->regs[0];
-                        arg1 = env->regs[2];
-                        arg2 = env->regs[3];
-                        get_user_s32(arg3, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg4, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        arg7 = 0;
-                        arg8 = 0;
-                    } else {
-                        arg1 = env->regs[0];
-                        arg2 = env->regs[1];
-                        arg3 = env->regs[2];

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env)

Quoted 10 lines
bsd-user/arm/target_arch_cpu.h +0 -10
-                        arg4 = env->regs[3];
-                        get_user_s32(arg5, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg6, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg7, params);
-                        params += sizeof(int32_t);
-                        get_user_s32(arg8, params);
-                    }
-                    ret = do_freebsd_syscall(env, syscall_nr, arg1,

arg2, arg3,

Quoted 32 lines
bsd-user/arm/target_arch_cpu.h +31 -1
-                            arg4, arg5, arg6, arg7, arg8);
+                /* See arm/arm/syscall.c cpu_fetch_syscall_args() */
+                if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
+                    syscall_nr = env->regs[0];
+                    arg1 = env->regs[1];
+                    arg2 = env->regs[2];
+                    arg3 = env->regs[3];
+                    get_user_s32(arg4, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg7, params);
+                    arg8 = 0;
+                } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
+                    syscall_nr = env->regs[0];
+                    arg1 = env->regs[2];
+                    arg2 = env->regs[3];
+                    get_user_s32(arg3, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg4, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    arg7 = 0;
+                    arg8 = 0;
+                } else {
+                    arg1 = env->regs[0];
+                    arg2 = env->regs[1];
+                    arg3 = env->regs[2];

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env)

Quoted 10 lines
bsd-user/arm/target_arch_cpu.h +10 -0
+                    arg4 = env->regs[3];
+                    get_user_s32(arg5, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg6, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg7, params);
+                    params += sizeof(int32_t);
+                    get_user_s32(arg8, params);
+                }
+                ret = do_freebsd_syscall(env, syscall_nr, arg1, arg2,

arg3,

Quoted 5 lines
bsd-user/arm/target_arch_cpu.h +5 -0
+                                         arg4, arg5, arg6, arg7, arg8);
+                /*
+                 * Compare to arm/arm/vm_machdep.c
+                 * cpu_set_syscall_retval()
+                 */

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env)

Quoted 10 lines
bsd-user/arm/target_arch_cpu.h +3 -5
+                if (-TARGET_EJUSTRETURN == ret) {
                     /*
-                     * Compare to arm/arm/vm_machdep.c
-                     * cpu_set_syscall_retval()
+                     * Returning from a successful sigreturn syscall.
+                     * Avoid clobbering register state.
                      */
-                    if (-TARGET_EJUSTRETURN == ret) {
-                        /*
-                         * Returning from a successful sigreturn

syscall.

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env)

Quoted 11 lines
bsd-user/arm/target_arch_cpu.h +0 -11
-                         * Avoid clobbering register state.
-                         */
-                        break;
-                    }
-                    if (-TARGET_ERESTART == ret) {
-                        env->regs[15] -= env->thumb ? 2 : 4;
-                        break;
-                    }
-                    if ((unsigned int)ret >= (unsigned int)(-515)) {
-                        ret = -ret;
-                        cpsr_write(env, CPSR_C, CPSR_C,

CPSRWriteByInstr);

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env)

Quoted 4 lines
bsd-user/arm/target_arch_cpu.h +0 -4
-                        env->regs[0] = ret;
-                    } else {
-                        cpsr_write(env, 0, CPSR_C, CPSRWriteByInstr);
-                        env->regs[0] = ret; /* XXX need to handle

lseek()? */

Quoted 7 lines
bsd-user/arm/target_arch_cpu.h +5 -2
-                        /* env->regs[1] = 0; */
-                    }
+                    break;
+                }
+                if (-TARGET_ERESTART == ret) {
+                    env->regs[15] -= env->thumb ? 2 : 4;
+                    break;

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env)

Quoted 10 lines
bsd-user/arm/target_arch_cpu.h +7 -2
+                }
+                if ((unsigned int)ret >= (unsigned int)(-515)) {
+                    ret = -ret;
+                    cpsr_write(env, CPSR_C, CPSR_C, CPSRWriteByInstr);
+                    env->regs[0] = ret;
                 } else {
-                    fprintf(stderr, "qemu: bsd_type (= %d) syscall "
-                            "not supported\n", bsd_type);
+                    cpsr_write(env, 0, CPSR_C, CPSRWriteByInstr);
+                    env->regs[0] = ret; /* XXX need to handle lseek()?
Quoted 9 lines

*/

bsd-user/arm/target_arch_cpu.h static inline void target_cpu_loop(CPUARMState *env) · line 140

Quoted 5 lines
bsd-user/arm/target_arch_cpu.h +1 -0
+                    /* env->regs[1] = 0; */
                 }
             }
             break;

We should probably fix the lseek() situation sooner rather than later, but:

True, but that problem is in the fork as well... I've created a github issue for it:
https://github.com/qemu-bsd-user/qemu-bsd-user/issues/28
but I am otherwise going to ignore it for now.

Warner

Quoted 2 lines
#45 lore raw

Re: [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec

In reply to Warner Losh
Quoted 27 lines · Warner Losh

lock_iovec will lock an I/O vec and the memory to which it referrs and create a iovec in the host space that referrs to it, with full error unwinding.

---
  bsd-user/freebsd/os-syscall.c | 92 +++++++++++++++++++++++++++++++++++
  1 file changed, 92 insertions(+)
bsd-user/freebsd/os-syscall.c +10 -0
index 060134a9ecd..c21759ae7ce 100644
@@ -75,6 +75,98 @@ bool is_error(abi_long ret)
      return (abi_ulong)ret >= (abi_ulong)(-4096);
  }
 
+struct iovec *lock_iovec(int type, abi_ulong target_addr,
+        int count, int copy)
+{
+    struct target_iovec *target_vec;
+    struct iovec *vec;
+    abi_ulong total_len, max_len;
+    int i;
+    int err = 0;
+    bool bad_address = false;
+

bsd-user/freebsd/os-syscall.c bool is_error(abi_long ret) · line 97

Quoted 10 lines
bsd-user/freebsd/os-syscall.c +10 -0
+    if (count == 0) {
+        errno = 0;
+        return NULL;
+    }
+    if (count < 0 || count > IOV_MAX) {
+        errno = EINVAL;
+        return NULL;
+    }
+
+    vec = calloc(count, sizeof(struct iovec));

g_try_new0.

You may want to use g_autofree to simplify error handling, which then requires you use

    return g_steal_pointer(&vec);

on the success path.

Quoted 38 lines
bsd-user/freebsd/os-syscall.c +38 -0
+    if (vec == NULL) {
+        errno = ENOMEM;
+        return NULL;
+    }
+
+    target_vec = lock_user(VERIFY_READ, target_addr,
+                           count * sizeof(struct target_iovec), 1);
+    if (target_vec == NULL) {
+        err = EFAULT;
+        goto fail2;
+    }
+
+    /*
+     * ??? If host page size > target page size, this will result in a value
+     * larger than what we can actually support.
+     */
+    max_len = 0x7fffffff & TARGET_PAGE_MASK;
+    total_len = 0;
+
+    for (i = 0; i < count; i++) {
+        abi_ulong base = tswapal(target_vec[i].iov_base);
+        abi_long len = tswapal(target_vec[i].iov_len);
+
+        if (len < 0) {
+            err = EINVAL;
+            goto fail;
+        } else if (len == 0) {
+            /* Zero length pointer is ignored.  */
+            vec[i].iov_base = 0;
+        } else {
+            vec[i].iov_base = lock_user(type, base, len, copy);
+            /*
+             * If the first buffer pointer is bad, this is a fault.  But
+             * subsequent bad buffers will result in a partial write; this is
+             * realized by filling the vector with null pointers and zero
+             * lengths.
+             */
+            if (!vec[i].iov_base) {

bsd-user/freebsd/os-syscall.c bool is_error(abi_long ret) · line 145

Quoted 10 lines
bsd-user/freebsd/os-syscall.c +10 -0
+                if (i == 0) {
+                    err = EFAULT;
+                    goto fail;
+                } else {
+                    bad_address = true;
+                }
+            }
+            if (bad_address) {
+                len = 0;
+            }

Surely this bad_address check should happen earlier, before we attempt the lock above? E.g.

    else if (len == 0 || bad_address)

r~

Reviewed-by on v1 20/22
#60 lore raw

Re: [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv

In reply to Warner Losh
Quoted 45 lines · Warner Losh

Implement do_bsd_{read,pread,readv,preadv}. Connect them to the system call table.

---
  bsd-user/bsd-file.h           | 79 +++++++++++++++++++++++++++++++++++
  bsd-user/freebsd/os-syscall.c | 24 +++++++++++
  2 files changed, 103 insertions(+)
bsd-user/bsd-file.h +26 -0
index 2f743db38e1..5934cbd5612 100644
@@ -36,4 +36,83 @@ extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,
  extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int count,
          int copy);
 
+ssize_t safe_read(int fd, void *buf, size_t nbytes);
+ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset);
+ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt);
+ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
+
+/* read(2) */
+static inline abi_long do_bsd_read(abi_long arg1, abi_long arg2, abi_long arg3)
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(safe_read(arg1, p, arg3));
+    unlock_user(p, arg2, ret);
+
+    return ret;
+}
+
+/* pread(2) */
+static inline abi_long do_bsd_pread(void *cpu_env, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
+{
+    abi_long ret;

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count, · line 74

Quoted 10 lines
bsd-user/bsd-file.h +10 -0
+    void *p;
+
+    p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    if (regpairs_aligned(cpu_env) != 0) {
+        arg4 = arg5;
+        arg5 = arg6;
+    }

This would be clearer if you had started labeling from arg0.

But either way,

r~

Quoted 86 lines
bsd-user/bsd-file.h +43 -0
+    ret = get_errno(safe_pread(arg1, p, arg3, target_arg64(arg4, arg5)));
+    unlock_user(p, arg2, ret);
+
+    return ret;
+}
+
+/* readv(2) */
+static inline abi_long do_bsd_readv(abi_long arg1, abi_long arg2, abi_long arg3)
+{
+    abi_long ret;
+    struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
+
+    if (vec != NULL) {
+        ret = get_errno(safe_readv(arg1, vec, arg3));
+        unlock_iovec(vec, arg2, arg3, 1);
+    } else {
+        ret = -host_to_target_errno(errno);
+    }
+
+    return ret;
+}
+
+/* preadv(2) */
+static inline abi_long do_bsd_preadv(void *cpu_env, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
+{
+    abi_long ret;
+    struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 1);
+
+    if (vec != NULL) {
+        if (regpairs_aligned(cpu_env) != 0) {
+            arg4 = arg5;
+            arg5 = arg6;
+        }
+        ret = get_errno(safe_preadv(arg1, vec, arg3, target_arg64(arg4, arg5)));
+        unlock_iovec(vec, arg2, arg3, 0);
+    } else {
+        ret = -host_to_target_errno(errno);
+    }
+
+    return ret;
+}
+
  #endif /* !BSD_FILE_H_ */
bsd-user/freebsd/os-syscall.c +24 -0
index fcfa6221182..dda79af53de 100644
@@ -42,6 +42,14 @@
 
  #include "bsd-file.h"
 
+/* I/O */
+safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes);
+safe_syscall4(ssize_t, pread, int, fd, void *, buf, size_t, nbytes, off_t,
+    offset);
+safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int, iovcnt);
+safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int, iovcnt,
+    off_t, offset);
+
  void target_set_brk(abi_ulong new_brk)
  {
  }
@@ -212,6 +220,22 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
      }
 
      switch (num) {
+
+        /*
+         * File system calls.
+         */
+    case TARGET_FREEBSD_NR_read: /* read(2) */
+        ret = do_bsd_read(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_pread: /* pread(2) */
+        ret = do_bsd_pread(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
+        break;
+
+    case TARGET_FREEBSD_NR_readv: /* readv(2) */
+        ret = do_bsd_readv(arg1, arg2, arg3);
+        break;
+
      default:
          gemu_log("qemu: unsupported syscall: %d\n", num);
          ret = -TARGET_ENOSYS;
Reviewed-by on v1 22/22
#66 lore raw

Re: [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit

In reply to Warner Losh
Quoted 10 lines · Warner Losh

Implement the exit system call. Bring in bsd-proc.h to contain all the process system call implementation and helper routines.

---
  bsd-user/bsd-proc.h           | 43 +++++++++++++++++++++++++++++++++++
  bsd-user/freebsd/os-syscall.c |  7 ++++++
  2 files changed, 50 insertions(+)

create mode 100644 bsd-user/bsd-proc.h

r~

#50 lore raw

Re: [PATCH 16/22] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate

In reply to Warner Losh
Quoted 33 lines · Warner Losh

Add in the tracing and this system call not implemented boilerplate.

---
  bsd-user/freebsd/os-syscall.c | 27 ++++++++++++++++++++++++++-
  1 file changed, 26 insertions(+), 1 deletion(-)
bsd-user/freebsd/os-syscall.c +17 -1
index d49945f0fcc..fcfa6221182 100644
@@ -200,7 +200,32 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
                              abi_long arg5, abi_long arg6, abi_long arg7,
                              abi_long arg8)
  {
-    return 0;
+    CPUState *cpu = env_cpu(cpu_env);
+    abi_long ret;
+
+#ifdef DEBUG
+    gemu_log("freebsd syscall %d\n", num);
+#endif
+    trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
+    if (do_strace) {
+        print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
+    }
+
+    switch (num) {
+    default:
+        gemu_log("qemu: unsupported syscall: %d\n", num);
+        ret = -TARGET_ENOSYS;
+        break;
+    }

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

Quoted 10 lines
bsd-user/freebsd/os-syscall.c +9 -0
+
+#ifdef DEBUG
+    gemu_log(" = %ld\n", ret);
+#endif
+    if (do_strace) {
+        print_freebsd_syscall_ret(num, ret);
+    }
+    trace_guest_user_syscall_ret(cpu, num, ret);
+    return ret;
  }

I think it's simpler to put the logging in a wrapper function, so that one can return from the switch rather than be required to break.

r~

Reviewed-by on v1 17/22
#53 lore raw

Re: [PATCH 17/22] bsd-user: introduce target.h

In reply to Warner Losh
Quoted 32 lines · Warner Losh

Create target.h. This file is intended to be simple and describe basic things about the architecture. If something is a basic feature of the architecture, it belongs here. Should we need something that's per-BSD there will be a target-os.h that will live in the per-bsd directories.

Define regpairs_aligned to reflect whether or not registers are 'paired' for 64-bit arguments or not. This will be false for all 64-bit targets, and will be true on those architectures that pair (currently just armv7 and powerpc on FreeBSD 14.x).

---
  bsd-user/arm/target.h    | 21 +++++++++++++++++++++
  bsd-user/i386/target.h   | 21 +++++++++++++++++++++
  bsd-user/qemu.h          |  1 +
  bsd-user/x86_64/target.h | 21 +++++++++++++++++++++
  4 files changed, 64 insertions(+)

create mode 100644 bsd-user/arm/target.h
create mode 100644 bsd-user/i386/target.h
create mode 100644 bsd-user/x86_64/target.h

bsd-user/arm/target.h new file +5 -0
new file mode 100644
index 00000000000..1f7ee49bfb4
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all i386 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <[email protected]>
+ *

bsd-user/arm/target.h line 15

Quoted 10 lines
bsd-user/arm/target.h new file +10 -0
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TARGET_H
+#define TARGET_H
+
+/*
+ * arm EABI 'lumps' the registers for 64-bit args.
+ */
+static inline int regpairs_aligned(void *cpu_env)

Nit: could use bool. Otherwise,

r~

Reviewed-by on v1 18/22
#56 lore raw

Re: [PATCH 18/22] bsd-user: Define target_arg64

In reply to Warner Losh
Quoted 10 lines · Warner Losh

target_arg64 is a generic way to extract 64-bits from a pair of arguments. On 32-bit platforms, it returns them joined together as appropriate. On 64-bit platforms, it returns the first arg because it's already 64-bits.

---
  bsd-user/qemu.h | 13 +++++++++++++
  1 file changed, 13 insertions(+)

r~

#61 lore raw

Re: [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv

In reply to Richard Henderson
Quoted 49 lines · Richard Henderson

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong

Quoted 16 lines · Warner Losh

Implement do_bsd_{read,pread,readv,preadv}. Connect them to the system call table.

---
  bsd-user/bsd-file.h           | 79 +++++++++++++++++++++++++++++++++++
  bsd-user/freebsd/os-syscall.c | 24 +++++++++++
  2 files changed, 103 insertions(+)
bsd-user/bsd-file.h +0 -0
index 2f743db38e1..5934cbd5612 100644
@@ -36,4 +36,83 @@ extern struct iovec *lock_iovec(int type, abi_ulong

target_addr, int count,

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong · line 36

Quoted 1 line
bsd-user/bsd-file.h +0 -0
  extern void unlock_iovec(struct iovec *vec, abi_ulong target_addr, int

count,

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,

Quoted 6 lines
bsd-user/bsd-file.h +4 -0
          int copy);
 
+ssize_t safe_read(int fd, void *buf, size_t nbytes);
+ssize_t safe_pread(int fd, void *buf, size_t nbytes, off_t offset);
+ssize_t safe_readv(int fd, const struct iovec *iov, int iovcnt);
+ssize_t safe_preadv(int fd, const struct iovec *iov, int iovcnt, off_t

offset);

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,

Quoted 3 lines
bsd-user/bsd-file.h +3 -0
+
+/* read(2) */
+static inline abi_long do_bsd_read(abi_long arg1, abi_long arg2,

abi_long arg3)

Quoted 7 lines
bsd-user/bsd-file.h +7 -0
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
+    if (p == NULL) {
+        return -TARGET_EFAULT;

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,

Quoted 10 lines
bsd-user/bsd-file.h +10 -0
+    }
+    ret = get_errno(safe_read(arg1, p, arg3));
+    unlock_user(p, arg2, ret);
+
+    return ret;
+}
+
+/* pread(2) */
+static inline abi_long do_bsd_pread(void *cpu_env, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,

abi_long arg6)

Quoted 15 lines

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count, · line 74

Quoted 12 lines
bsd-user/bsd-file.h +12 -0
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user(VERIFY_WRITE, arg2, arg3, 0);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    if (regpairs_aligned(cpu_env) != 0) {
+        arg4 = arg5;
+        arg5 = arg6;
+    }

This would be clearer if you had started labeling from arg0.

There's a number of other changes this would force, so I'll defer it...

Warner

Quoted 103 lines

But either way,

r~

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong · line 75

Quoted 1 line
bsd-user/bsd-file.h +1 -0
+    ret = get_errno(safe_pread(arg1, p, arg3, target_arg64(arg4,

arg5)));

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,

Quoted 7 lines
bsd-user/bsd-file.h +7 -0
+    unlock_user(p, arg2, ret);
+
+    return ret;
+}
+
+/* readv(2) */
+static inline abi_long do_bsd_readv(abi_long arg1, abi_long arg2,

abi_long arg3)

Quoted 7 lines
bsd-user/bsd-file.h +7 -0
+{
+    abi_long ret;
+    struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 0);
+
+    if (vec != NULL) {
+        ret = get_errno(safe_readv(arg1, vec, arg3));
+        unlock_iovec(vec, arg2, arg3, 1);

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,

Quoted 10 lines
bsd-user/bsd-file.h +10 -0
+    } else {
+        ret = -host_to_target_errno(errno);
+    }
+
+    return ret;
+}
+
+/* preadv(2) */
+static inline abi_long do_bsd_preadv(void *cpu_env, abi_long arg1,
+    abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,

abi_long arg6)

bsd-user/bsd-file.h extern struct iovec *lock_iovec(int type, abi_ulong target_addr, int count,

Quoted 10 lines
bsd-user/bsd-file.h +10 -0
+{
+    abi_long ret;
+    struct iovec *vec = lock_iovec(VERIFY_WRITE, arg2, arg3, 1);
+
+    if (vec != NULL) {
+        if (regpairs_aligned(cpu_env) != 0) {
+            arg4 = arg5;
+            arg5 = arg6;
+        }
+        ret = get_errno(safe_preadv(arg1, vec, arg3, target_arg64(arg4,

arg5)));

Review

Quoted 10 lines
bsd-user/bsd-file.h +8 -0
+        unlock_iovec(vec, arg2, arg3, 0);
+    } else {
+        ret = -host_to_target_errno(errno);
+    }
+
+    return ret;
+}
+
  #endif /* !BSD_FILE_H_ */
(quoted diff) +0 -0

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

bsd-user/freebsd/os-syscall.c line 47

Quoted 10 lines

index fcfa6221182..dda79af53de 100644

bsd-user/freebsd/os-syscall.c +3 -0
@@ -42,6 +42,14 @@
 
  #include "bsd-file.h"
 
+/* I/O */
+safe_syscall3(ssize_t, read, int, fd, void *, buf, size_t, nbytes);
+safe_syscall4(ssize_t, pread, int, fd, void *, buf, size_t, nbytes,

off_t,

bsd-user/freebsd/os-syscall.c line 49

Quoted 2 lines
bsd-user/freebsd/os-syscall.c +2 -0
+    offset);
+safe_syscall3(ssize_t, readv, int, fd, const struct iovec *, iov, int,

iovcnt);

bsd-user/freebsd/os-syscall.c line 50

Quoted 1 line
bsd-user/freebsd/os-syscall.c +1 -0
+safe_syscall4(ssize_t, preadv, int, fd, const struct iovec *, iov, int,

iovcnt,

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

Quoted 6 lines
bsd-user/freebsd/os-syscall.c +2 -0
+    off_t, offset);
+
  void target_set_brk(abi_ulong new_brk)
  {
  }
@@ -212,6 +220,22 @@ abi_long do_freebsd_syscall(void *cpu_env, int num,

abi_long arg1,

Quoted 22 lines
bsd-user/freebsd/os-syscall.c +16 -0
      }
 
      switch (num) {
+
+        /*
+         * File system calls.
+         */
+    case TARGET_FREEBSD_NR_read: /* read(2) */
+        ret = do_bsd_read(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_pread: /* pread(2) */
+        ret = do_bsd_pread(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
+        break;
+
+    case TARGET_FREEBSD_NR_readv: /* readv(2) */
+        ret = do_bsd_readv(arg1, arg2, arg3);
+        break;
+
      default:
          gemu_log("qemu: unsupported syscall: %d\n", num);
          ret = -TARGET_ENOSYS;
#8 lore raw

Re: [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall

In reply to Kyle Evans
Quoted 12 lines · Kyle Evans
Quoted 10 lines · Warner Losh

This doesn't build on openbsd at the moment, and this could should arguably be in bsd-user/*bsd/x86_64 somewhere. Until we refactor to support OpenBSD/NetBSD again, drop it here.

---
 bsd-user/x86_64/target_arch_cpu.h | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
Quoted 8 lines

As a general comment, I'd like to reach out to the others at some point and gauge interest/ability to participate, but I definitely agree that it would be better to drop !FreeBSD for now to simplify upcoming improvements to the common core. I'm not aware of any other forks that have tried to maintain bsd-user on their platforms.

I did this exercise about 2 years ago now. Neither NetBSD nor OpenBSD communities were using bsd-user because of its super primitive state. There was one person in NetBSD land who was thinking about updating things, and I pointed them at the bsd-user fork, but then heard nothing more from them.

I plan on generally dropping not FreeBSD, but in a way that's easy to add back later. I plan on trying to have a reasonable split where it already exists, but I don't plan on rewriting the current code too much when FreeBSD specific things are noted during the review process (like in the signals series of patches). I do commit in the future to working with others that want to do the heavy lifting of making this work with ${OTHER}BSD, should they show up. There are a few more things in the bsd-user fork that I know are FreeBSD specific that aren't in FreeBSD specific files, and I plan on upstreaming them as is given the risk reshufflings represent. To that end, when I do move things to freebsd/, I've also move the netbsd/openbsd code to their respective directories as well, but since that code has been decaying for some number of years now, it will be at best the least imperfect starting point we can have.

I also had a bit of a plan to see if people want to update to ${OTHER}BSD once upstreaming is complete. I worry, a bit, that trying to accomodate them during the upstreaming process will slow down the upstreaming process. However, if someone shows up with code, enthusiasm and a clue, I'm happy to reevaluate.

Warner

Quoted 45 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/bsd-user/x86_64/target_arch_cpu.h

bsd-user/x86_64/target_arch_cpu.h static inline void target_cpu_loop(CPUX86State

Quoted 4 lines

index 9dc52d5afc4..5be2f02416e 100644

bsd-user/x86_64/target_arch_cpu.h +0 -0
@@ -126,25 +126,14 @@ static inline void target_cpu_loop(CPUX86State

*env)

bsd-user/x86_64/target_arch_cpu.h static inline void target_cpu_loop(CPUX86State *env)

Quoted 12 lines
bsd-user/x86_64/target_arch_cpu.h +0 -9
         switch (trapnr) {
         case EXCP_SYSCALL:
             /* syscall from syscall instruction */
-            if (bsd_type == target_freebsd) {
-                env->regs[R_EAX] = do_freebsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDX],
-                                                      env->regs[R_ECX],
-                                                      env->regs[8],
-                                                      env->regs[9], 0,

0);

Quoted 24 lines
bsd-user/x86_64/target_arch_cpu.h +8 -11
-            } else { /* if (bsd_type == target_openbsd) */
-                env->regs[R_EAX] = do_openbsd_syscall(env,
-                                                      env->regs[R_EAX],
-                                                      env->regs[R_EDI],
-                                                      env->regs[R_ESI],
-                                                      env->regs[R_EDX],
-                                                      env->regs[10],
-                                                      env->regs[8],
-                                                      env->regs[9]);
-            }
+            env->regs[R_EAX] = do_freebsd_syscall(env,
+                                                  env->regs[R_EAX],
+                                                  env->regs[R_EDI],
+                                                  env->regs[R_ESI],
+                                                  env->regs[R_EDX],
+                                                  env->regs[R_ECX],
+                                                  env->regs[8],
+                                                  env->regs[9], 0, 0);
             env->eip = env->exception_next_eip;
             if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
                 env->regs[R_EAX] = -env->regs[R_EAX];
--

2.33.1

#41 lore raw

Re: [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls

In reply to Richard Henderson
Quoted 18 lines · Richard Henderson

bsd-user/bsd-file.h line 30

Quoted 11 lines · Warner Losh
bsd-user/bsd-file.h new file +11 -0
+#ifndef BSD_FILE_H_
+#define BSD_FILE_H_
+
+#include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/uio.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>

Many of these should be done by "qemu/osdep.h" already. Otherwise I question putting them
into this header, as opposed to as needed by other syscall handling c files.

I can remove the ones that are done by qemu/osdep.h easily enough. That's changed over time
and these used to be required. I'm hesitant to remove the others since that's starting to get
into restructuring the code we have working upstream. On the other hand, the ordering of these
files make it such that these are often included just once, so moving to a bsd-file.h that's just the
functions and bsd-file.c that's the definition and relying on LTO to optimize. That would make things
less fragile than they are now. So I'm torn since part of floating these patches is to do a small sliver
to get feedback... I may need to sleep on this to figure out how to weigh the 'cleaner code' vs 'risk
of introducing regressions during refactoring'.

Warner

#42 lore raw

Re: [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls

In reply to Richard Henderson
Quoted 18 lines · Richard Henderson

bsd-user/bsd-file.h line 30

Quoted 11 lines · Warner Losh
bsd-user/bsd-file.h new file +11 -0
+#ifndef BSD_FILE_H_
+#define BSD_FILE_H_
+
+#include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/uio.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>

Many of these should be done by "qemu/osdep.h" already. Otherwise I question putting them
into this header, as opposed to as needed by other syscall handling c files.

Indeed. None of these are needed here. I've removed them.