All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 00/22] bsd-user: Start upstreaming the system calls.
@ 2022-02-01 11:14 Warner Losh
  2022-02-01 11:14 ` [PATCH 01/22] bsd-user/main.c: Drop syscall flavor arg -bsd Warner Losh
                   ` (21 more replies)
  0 siblings, 22 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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

-- 
2.33.1



^ permalink raw reply	[flat|nested] 67+ messages in thread

* [PATCH 01/22] bsd-user/main.c: Drop syscall flavor arg -bsd
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 15:54   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64 Warner Losh
                   ` (20 subsequent siblings)
  21 siblings, 1 reply; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/main.c | 12 ------------
 1 file changed, 12 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index f1d58e905e7..bddb830e99b 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -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")) {
-- 
2.33.1



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
  2022-02-01 11:14 ` [PATCH 01/22] bsd-user/main.c: Drop syscall flavor arg -bsd Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 15:57   ` Richard Henderson
  2022-02-01 16:19   ` Kyle Evans
  2022-02-01 11:14 ` [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall Warner Losh
                   ` (19 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/x86_64/target_arch_cpu.h | 58 -------------------------------
 1 file changed, 58 deletions(-)

diff --git a/bsd-user/x86_64/target_arch_cpu.h b/bsd-user/x86_64/target_arch_cpu.h
index 0a9c0f08946..9dc52d5afc4 100644
--- a/bsd-user/x86_64/target_arch_cpu.h
+++ b/bsd-user/x86_64/target_arch_cpu.h
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
  2022-02-01 11:14 ` [PATCH 01/22] bsd-user/main.c: Drop syscall flavor arg -bsd Warner Losh
  2022-02-01 11:14 ` [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64 Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:22   ` Kyle Evans
  2022-02-01 17:27   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: " Warner Losh
                   ` (18 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/x86_64/target_arch_cpu.h | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

diff --git a/bsd-user/x86_64/target_arch_cpu.h b/bsd-user/x86_64/target_arch_cpu.h
index 9dc52d5afc4..5be2f02416e 100644
--- a/bsd-user/x86_64/target_arch_cpu.h
+++ b/bsd-user/x86_64/target_arch_cpu.h
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: Remove openbsd syscall
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (2 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:24   ` Kyle Evans
  2022-02-01 17:29   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls Warner Losh
                   ` (17 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/i386/target_arch_cpu.h | 84 +++++++++++++++------------------
 1 file changed, 37 insertions(+), 47 deletions(-)

diff --git a/bsd-user/i386/target_arch_cpu.h b/bsd-user/i386/target_arch_cpu.h
index 3cbf69d8af2..9da22202d48 100644
--- a/bsd-user/i386/target_arch_cpu.h
+++ b/bsd-user/i386/target_arch_cpu.h
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (3 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: " Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:32   ` Kyle Evans
  2022-02-01 17:31   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target Warner Losh
                   ` (16 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/arm/target_arch_cpu.h | 139 ++++++++++++++++-----------------
 1 file changed, 66 insertions(+), 73 deletions(-)

diff --git a/bsd-user/arm/target_arch_cpu.h b/bsd-user/arm/target_arch_cpu.h
index b087db48fa4..afb7814a8d1 100644
--- a/bsd-user/arm/target_arch_cpu.h
+++ b/bsd-user/arm/target_arch_cpu.h
@@ -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;
-- 
2.33.1



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (4 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:33   ` Kyle Evans
  2022-02-01 17:34   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: " Warner Losh
                   ` (15 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/arm/target_arch_thread.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/bsd-user/arm/target_arch_thread.h b/bsd-user/arm/target_arch_thread.h
index 11c7f765838..fcafca2408c 100644
--- a/bsd-user/arm/target_arch_thread.h
+++ b/bsd-user/arm/target_arch_thread.h
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: Assume a FreeBSD target
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (5 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:36   ` Kyle Evans
  2022-02-01 17:34   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 08/22] bsd-user: Remove bsd_type Warner Losh
                   ` (14 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/x86_64/target_arch_thread.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/bsd-user/x86_64/target_arch_thread.h b/bsd-user/x86_64/target_arch_thread.h
index d105e43fd35..b745d7ffeb7 100644
--- a/bsd-user/x86_64/target_arch_thread.h
+++ b/bsd-user/x86_64/target_arch_thread.h
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 08/22] bsd-user: Remove bsd_type
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (6 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: " Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:34   ` Kyle Evans
  2022-02-01 17:35   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here Warner Losh
                   ` (13 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/main.c | 2 --
 bsd-user/qemu.h | 7 -------
 2 files changed, 9 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index bddb830e99b..88d347d05eb 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -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();
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 02921ac8b3b..e5742bd6c03 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (7 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 08/22] bsd-user: Remove bsd_type Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:18   ` Kyle Evans
  2022-02-01 17:37   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 10/22] bsd-user: Move system call building to os-syscall.c Warner Losh
                   ` (12 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/os-syscall.c | 68 +++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 bsd-user/freebsd/os-syscall.c

diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
new file mode 100644
index 00000000000..7e2bedb918d
--- /dev/null
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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)
+{
+}
-- 
2.33.1



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 10/22] bsd-user: Move system call building to os-syscall.c
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (8 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 17:38   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove Warner Losh
                   ` (11 subsequent siblings)
  21 siblings, 1 reply; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/meson.build | 1 +
 bsd-user/meson.build         | 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/bsd-user/freebsd/meson.build b/bsd-user/freebsd/meson.build
index 4b69cca7b90..f87c788e846 100644
--- a/bsd-user/freebsd/meson.build
+++ b/bsd-user/freebsd/meson.build
@@ -1,3 +1,4 @@
 bsd_user_ss.add(files(
   'os-sys.c',
+  'os-syscall.c',
 ))
diff --git a/bsd-user/meson.build b/bsd-user/meson.build
index 8380fa44c25..5243122fc56 100644
--- a/bsd-user/meson.build
+++ b/bsd-user/meson.build
@@ -13,7 +13,6 @@ bsd_user_ss.add(files(
   'mmap.c',
   'signal.c',
   'strace.c',
-  'syscall.c',
   'uaccess.c',
 ))
 
-- 
2.33.1



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (9 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 10/22] bsd-user: Move system call building to os-syscall.c Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:35   ` Kyle Evans
  2022-02-01 17:38   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno Warner Losh
                   ` (10 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/syscall.c | 516 ---------------------------------------------
 1 file changed, 516 deletions(-)
 delete mode 100644 bsd-user/syscall.c

diff --git a/bsd-user/syscall.c b/bsd-user/syscall.c
deleted file mode 100644
index d3322760f43..00000000000
--- a/bsd-user/syscall.c
+++ /dev/null
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (10 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:35   ` Kyle Evans
  2022-02-01 17:39   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls Warner Losh
                   ` (9 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
 bsd-user/qemu.h               |  3 ++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7e2bedb918d..2e84cf350b1 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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)
 {
 
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index e5742bd6c03..56042ddbc5d 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (11 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:47   ` Kyle Evans
  2022-02-01 17:43   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec Warner Losh
                   ` (8 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 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

diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h
new file mode 100644
index 00000000000..2f743db38e1
--- /dev/null
+++ b/bsd-user/bsd-file.h
@@ -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_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 2e84cf350b1..060134a9ecd 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (12 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:46   ` Kyle Evans
  2022-02-01 21:16   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec Warner Losh
                   ` (7 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/os-syscall.c | 92 +++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)

diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 060134a9ecd..c21759ae7ce 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (13 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:37   ` Kyle Evans
  2022-02-01 21:29   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 16/22] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate Warner Losh
                   ` (6 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

Releases the references to the iovec created by lock_iovec.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index c21759ae7ce..d49945f0fcc 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 16/22] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (14 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 21:31   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 17/22] bsd-user: introduce target.h Warner Losh
                   ` (5 subsequent siblings)
  21 siblings, 1 reply; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/os-syscall.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index d49945f0fcc..fcfa6221182 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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)
-- 
2.33.1



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 17/22] bsd-user: introduce target.h
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (15 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 16/22] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:39   ` Kyle Evans
  2022-02-01 21:32   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 18/22] bsd-user: Define target_arg64 Warner Losh
                   ` (4 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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).

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 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

diff --git a/bsd-user/arm/target.h b/bsd-user/arm/target.h
new file mode 100644
index 00000000000..1f7ee49bfb4
--- /dev/null
+++ b/bsd-user/arm/target.h
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all i386 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <imp@bsdimp.com>
+ *
+ * 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 */
+
diff --git a/bsd-user/i386/target.h b/bsd-user/i386/target.h
new file mode 100644
index 00000000000..b0ab477d683
--- /dev/null
+++ b/bsd-user/i386/target.h
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all i386 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <imp@bsdimp.com>
+ *
+ * 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 */
+
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 56042ddbc5d..a9efa807b78 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -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"
 
 /*
diff --git a/bsd-user/x86_64/target.h b/bsd-user/x86_64/target.h
new file mode 100644
index 00000000000..6d3aef8fc49
--- /dev/null
+++ b/bsd-user/x86_64/target.h
@@ -0,0 +1,21 @@
+/*
+ * Intel general target stuff that's common to all x86_64 details
+ *
+ * Copyright (c) 2022 M. Warner Losh <imp@bsdimp.com>
+ *
+ * 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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 18/22] bsd-user: Define target_arg64
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (16 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 17/22] bsd-user: introduce target.h Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:41   ` Kyle Evans
  2022-02-01 21:33   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 19/22] bsd-user: Add safe system call macros Warner Losh
                   ` (3 subsequent siblings)
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, Richard Henderson,
	def, jrtc27, Warner Losh, Brad Smith

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.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/qemu.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index a9efa807b78..af272c2a802 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 19/22] bsd-user: Add safe system call macros
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (17 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 18/22] bsd-user: Define target_arg64 Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 21:33   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv Warner Losh
                   ` (2 subsequent siblings)
  21 siblings, 1 reply; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson, def,
	jrtc27, Warner Losh, Brad Smith

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

Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/syscall_defs.h | 47 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index 62b472b990b..c3bf14f38f4 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -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_ */
-- 
2.33.1



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (18 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 19/22] bsd-user: Add safe system call macros Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 21:37   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 21/22] bsd-user/bsd-file.h: Meat of the write system calls Warner Losh
  2022-02-01 11:14 ` [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit Warner Losh
  21 siblings, 1 reply; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, Richard Henderson,
	def, jrtc27, Warner Losh, Brad Smith

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

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/bsd-file.h           | 79 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 24 +++++++++++
 2 files changed, 103 insertions(+)

diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h
index 2f743db38e1..5934cbd5612 100644
--- a/bsd-user/bsd-file.h
+++ b/bsd-user/bsd-file.h
@@ -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_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index fcfa6221182..dda79af53de 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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;
-- 
2.33.1



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 21/22] bsd-user/bsd-file.h: Meat of the write system calls
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (19 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 21:38   ` Richard Henderson
  2022-02-01 11:14 ` [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit Warner Losh
  21 siblings, 1 reply; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, Richard Henderson,
	def, jrtc27, Warner Losh, Brad Smith

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

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/bsd-file.h           | 85 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 23 ++++++++++
 2 files changed, 108 insertions(+)

diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h
index 5934cbd5612..2047256a555 100644
--- a/bsd-user/bsd-file.h
+++ b/bsd-user/bsd-file.h
@@ -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_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index dda79af53de..f52c9e3c306 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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;
-- 
2.33.1



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit
  2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
                   ` (20 preceding siblings ...)
  2022-02-01 11:14 ` [PATCH 21/22] bsd-user/bsd-file.h: Meat of the write system calls Warner Losh
@ 2022-02-01 11:14 ` Warner Losh
  2022-02-01 16:48   ` Kyle Evans
  2022-02-01 21:39   ` Richard Henderson
  21 siblings, 2 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 11:14 UTC (permalink / raw)
  To: qemu-devel
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, Richard Henderson,
	def, jrtc27, Warner Losh, Brad Smith

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

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 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

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
new file mode 100644
index 00000000000..8f0b6990d14
--- /dev/null
+++ b/bsd-user/bsd-proc.h
@@ -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_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index f52c9e3c306..f66b6a1b1f5 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -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



^ permalink raw reply related	[flat|nested] 67+ messages in thread

* Re: [PATCH 01/22] bsd-user/main.c: Drop syscall flavor arg -bsd
  2022-02-01 11:14 ` [PATCH 01/22] bsd-user/main.c: Drop syscall flavor arg -bsd Warner Losh
@ 2022-02-01 15:54   ` Richard Henderson
  0 siblings, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 15:54 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/main.c | 12 ------------
>   1 file changed, 12 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64
  2022-02-01 11:14 ` [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64 Warner Losh
@ 2022-02-01 15:57   ` Richard Henderson
  2022-02-01 16:19   ` Kyle Evans
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 15:57 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Although initial versions of NetBSD did use int $80, it was replaced by
> syscall before any releases. OpenBSD and FreeBSD always did syscall.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/x86_64/target_arch_cpu.h | 58 -------------------------------
>   1 file changed, 58 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here
  2022-02-01 11:14 ` [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here Warner Losh
@ 2022-02-01 16:18   ` Kyle Evans
  2022-02-01 17:37   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:18 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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"

> 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.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/freebsd/os-syscall.c | 68 +++++++++++++++++++++++++++++++++++
>  1 file changed, 68 insertions(+)
>  create mode 100644 bsd-user/freebsd/os-syscall.c
>
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> new file mode 100644
> index 00000000000..7e2bedb918d
> --- /dev/null
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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.
> + */

... FreeBSD 11 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)
> +{
> +}
> --
> 2.33.1
>

Small typo + omission, otherwise:

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64
  2022-02-01 11:14 ` [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64 Warner Losh
  2022-02-01 15:57   ` Richard Henderson
@ 2022-02-01 16:19   ` Kyle Evans
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:19 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:14 AM Warner Losh <imp@bsdimp.com> wrote:
>
> Although initial versions of NetBSD did use int $80, it was replaced by
> syscall before any releases. OpenBSD and FreeBSD always did syscall.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/x86_64/target_arch_cpu.h | 58 -------------------------------
>  1 file changed, 58 deletions(-)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/x86_64/target_arch_cpu.h b/bsd-user/x86_64/target_arch_cpu.h
> index 0a9c0f08946..9dc52d5afc4 100644
> --- a/bsd-user/x86_64/target_arch_cpu.h
> +++ b/bsd-user/x86_64/target_arch_cpu.h
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall
  2022-02-01 11:14 ` [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall Warner Losh
@ 2022-02-01 16:22   ` Kyle Evans
  2022-02-01 23:13     ` Warner Losh
  2022-02-01 17:27   ` Richard Henderson
  1 sibling, 1 reply; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:22 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:14 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/x86_64/target_arch_cpu.h | 27 ++++++++-------------------
>  1 file changed, 8 insertions(+), 19 deletions(-)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

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.

> diff --git a/bsd-user/x86_64/target_arch_cpu.h b/bsd-user/x86_64/target_arch_cpu.h
> index 9dc52d5afc4..5be2f02416e 100644
> --- a/bsd-user/x86_64/target_arch_cpu.h
> +++ b/bsd-user/x86_64/target_arch_cpu.h
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: Remove openbsd syscall
  2022-02-01 11:14 ` [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: " Warner Losh
@ 2022-02-01 16:24   ` Kyle Evans
  2022-02-01 17:29   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:24 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:14 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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. :-)

> we refactor to support OpenBSD/NetBSD again, drop it here.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/i386/target_arch_cpu.h | 84 +++++++++++++++------------------
>  1 file changed, 37 insertions(+), 47 deletions(-)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/i386/target_arch_cpu.h b/bsd-user/i386/target_arch_cpu.h
> index 3cbf69d8af2..9da22202d48 100644
> --- a/bsd-user/i386/target_arch_cpu.h
> +++ b/bsd-user/i386/target_arch_cpu.h
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls
  2022-02-01 11:14 ` [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls Warner Losh
@ 2022-02-01 16:32   ` Kyle Evans
  2022-02-01 23:19     ` Warner Losh
  2022-02-01 17:31   ` Richard Henderson
  1 sibling, 1 reply; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:32 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:14 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/arm/target_arch_cpu.h | 139 ++++++++++++++++-----------------
>  1 file changed, 66 insertions(+), 73 deletions(-)
>
> diff --git a/bsd-user/arm/target_arch_cpu.h b/bsd-user/arm/target_arch_cpu.h
> index b087db48fa4..afb7814a8d1 100644
> --- a/bsd-user/arm/target_arch_cpu.h
> +++ b/bsd-user/arm/target_arch_cpu.h
> @@ -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;
>

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

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target
  2022-02-01 11:14 ` [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target Warner Losh
@ 2022-02-01 16:33   ` Kyle Evans
  2022-02-01 17:34   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:33 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:14 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/arm/target_arch_thread.h | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/arm/target_arch_thread.h b/bsd-user/arm/target_arch_thread.h
> index 11c7f765838..fcafca2408c 100644
> --- a/bsd-user/arm/target_arch_thread.h
> +++ b/bsd-user/arm/target_arch_thread.h
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 08/22] bsd-user: Remove bsd_type
  2022-02-01 11:14 ` [PATCH 08/22] bsd-user: Remove bsd_type Warner Losh
@ 2022-02-01 16:34   ` Kyle Evans
  2022-02-01 17:35   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:34 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:14 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/main.c | 2 --
>  bsd-user/qemu.h | 7 -------
>  2 files changed, 9 deletions(-)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/main.c b/bsd-user/main.c
> index bddb830e99b..88d347d05eb 100644
> --- a/bsd-user/main.c
> +++ b/bsd-user/main.c
> @@ -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();
> diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
> index 02921ac8b3b..e5742bd6c03 100644
> --- a/bsd-user/qemu.h
> +++ b/bsd-user/qemu.h
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove
  2022-02-01 11:14 ` [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove Warner Losh
@ 2022-02-01 16:35   ` Kyle Evans
  2022-02-01 17:38   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:35 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/syscall.c | 516 ---------------------------------------------
>  1 file changed, 516 deletions(-)
>  delete mode 100644 bsd-user/syscall.c
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/syscall.c b/bsd-user/syscall.c
> deleted file mode 100644
> index d3322760f43..00000000000
> --- a/bsd-user/syscall.c
> +++ /dev/null
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno
  2022-02-01 11:14 ` [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno Warner Losh
@ 2022-02-01 16:35   ` Kyle Evans
  2022-02-01 17:39   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:35 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
>  bsd-user/qemu.h               |  3 ++-
>  2 files changed, 25 insertions(+), 1 deletion(-)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index 7e2bedb918d..2e84cf350b1 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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)
>  {
>
> diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
> index e5742bd6c03..56042ddbc5d 100644
> --- a/bsd-user/qemu.h
> +++ b/bsd-user/qemu.h
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: Assume a FreeBSD target
  2022-02-01 11:14 ` [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: " Warner Losh
@ 2022-02-01 16:36   ` Kyle Evans
  2022-02-01 17:34   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:36 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/x86_64/target_arch_thread.h | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/x86_64/target_arch_thread.h b/bsd-user/x86_64/target_arch_thread.h
> index d105e43fd35..b745d7ffeb7 100644
> --- a/bsd-user/x86_64/target_arch_thread.h
> +++ b/bsd-user/x86_64/target_arch_thread.h
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec
  2022-02-01 11:14 ` [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec Warner Losh
@ 2022-02-01 16:37   ` Kyle Evans
  2022-02-01 21:29   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:37 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> Releases the references to the iovec created by lock_iovec.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
>  1 file changed, 23 insertions(+)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index c21759ae7ce..d49945f0fcc 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 17/22] bsd-user: introduce target.h
  2022-02-01 11:14 ` [PATCH 17/22] bsd-user: introduce target.h Warner Losh
@ 2022-02-01 16:39   ` Kyle Evans
  2022-02-01 21:32   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:39 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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).
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  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
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/arm/target.h b/bsd-user/arm/target.h
> new file mode 100644
> index 00000000000..1f7ee49bfb4
> --- /dev/null
> +++ b/bsd-user/arm/target.h
> @@ -0,0 +1,21 @@
> +/*
> + * Intel general target stuff that's common to all i386 details
> + *
> + * Copyright (c) 2022 M. Warner Losh <imp@bsdimp.com>
> + *
> + * 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 */
> +
> diff --git a/bsd-user/i386/target.h b/bsd-user/i386/target.h
> new file mode 100644
> index 00000000000..b0ab477d683
> --- /dev/null
> +++ b/bsd-user/i386/target.h
> @@ -0,0 +1,21 @@
> +/*
> + * Intel general target stuff that's common to all i386 details
> + *
> + * Copyright (c) 2022 M. Warner Losh <imp@bsdimp.com>
> + *
> + * 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 */
> +
> diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
> index 56042ddbc5d..a9efa807b78 100644
> --- a/bsd-user/qemu.h
> +++ b/bsd-user/qemu.h
> @@ -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"
>
>  /*
> diff --git a/bsd-user/x86_64/target.h b/bsd-user/x86_64/target.h
> new file mode 100644
> index 00000000000..6d3aef8fc49
> --- /dev/null
> +++ b/bsd-user/x86_64/target.h
> @@ -0,0 +1,21 @@
> +/*
> + * Intel general target stuff that's common to all x86_64 details
> + *
> + * Copyright (c) 2022 M. Warner Losh <imp@bsdimp.com>
> + *
> + * 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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 18/22] bsd-user: Define target_arg64
  2022-02-01 11:14 ` [PATCH 18/22] bsd-user: Define target_arg64 Warner Losh
@ 2022-02-01 16:41   ` Kyle Evans
  2022-02-01 21:33   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:41 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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.
>
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/qemu.h | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
> index a9efa807b78..af272c2a802 100644
> --- a/bsd-user/qemu.h
> +++ b/bsd-user/qemu.h
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec
  2022-02-01 11:14 ` [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec Warner Losh
@ 2022-02-01 16:46   ` Kyle Evans
  2022-02-01 21:16   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:46 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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

> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  bsd-user/freebsd/os-syscall.c | 92 +++++++++++++++++++++++++++++++++++
>  1 file changed, 92 insertions(+)
>

Two typos, otherwise seems to LGTM:

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index 060134a9ecd..c21759ae7ce 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls
  2022-02-01 11:14 ` [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls Warner Losh
@ 2022-02-01 16:47   ` Kyle Evans
  2022-02-01 17:43   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:47 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> 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.
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  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
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h
> new file mode 100644
> index 00000000000..2f743db38e1
> --- /dev/null
> +++ b/bsd-user/bsd-file.h
> @@ -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_ */
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index 2e84cf350b1..060134a9ecd 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit
  2022-02-01 11:14 ` [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit Warner Losh
@ 2022-02-01 16:48   ` Kyle Evans
  2022-02-01 21:39   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Kyle Evans @ 2022-02-01 16:48 UTC (permalink / raw)
  To: Warner Losh
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, Richard Henderson,
	QEMU Developers, def, Jessica Clarke, Brad Smith

On Tue, Feb 1, 2022 at 5:15 AM Warner Losh <imp@bsdimp.com> wrote:
>
> Implement the exit system call. Bring in bsd-proc.h to contain all the
> process system call implementation and helper routines.
>
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>  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
>

Reviewed-by: Kyle Evans <kevans@FreeBSD.org>

> diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
> new file mode 100644
> index 00000000000..8f0b6990d14
> --- /dev/null
> +++ b/bsd-user/bsd-proc.h
> @@ -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_ */
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index f52c9e3c306..f66b6a1b1f5 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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
>


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall
  2022-02-01 11:14 ` [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall Warner Losh
  2022-02-01 16:22   ` Kyle Evans
@ 2022-02-01 17:27   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:27 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/x86_64/target_arch_cpu.h | 27 ++++++++-------------------
>   1 file changed, 8 insertions(+), 19 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: Remove openbsd syscall
  2022-02-01 11:14 ` [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: " Warner Losh
  2022-02-01 16:24   ` Kyle Evans
@ 2022-02-01 17:29   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:29 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/i386/target_arch_cpu.h | 84 +++++++++++++++------------------
>   1 file changed, 37 insertions(+), 47 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls
  2022-02-01 11:14 ` [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls Warner Losh
  2022-02-01 16:32   ` Kyle Evans
@ 2022-02-01 17:31   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:31 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/arm/target_arch_cpu.h | 139 ++++++++++++++++-----------------
>   1 file changed, 66 insertions(+), 73 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target
  2022-02-01 11:14 ` [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target Warner Losh
  2022-02-01 16:33   ` Kyle Evans
@ 2022-02-01 17:34   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:34 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/arm/target_arch_thread.h | 4 +---
>   1 file changed, 1 insertion(+), 3 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: Assume a FreeBSD target
  2022-02-01 11:14 ` [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: " Warner Losh
  2022-02-01 16:36   ` Kyle Evans
@ 2022-02-01 17:34   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:34 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/x86_64/target_arch_thread.h | 4 +---
>   1 file changed, 1 insertion(+), 3 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 08/22] bsd-user: Remove bsd_type
  2022-02-01 11:14 ` [PATCH 08/22] bsd-user: Remove bsd_type Warner Losh
  2022-02-01 16:34   ` Kyle Evans
@ 2022-02-01 17:35   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:35 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/main.c | 2 --
>   bsd-user/qemu.h | 7 -------
>   2 files changed, 9 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here
  2022-02-01 11:14 ` [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here Warner Losh
  2022-02-01 16:18   ` Kyle Evans
@ 2022-02-01 17:37   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:37 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/freebsd/os-syscall.c | 68 +++++++++++++++++++++++++++++++++++
>   1 file changed, 68 insertions(+)
>   create mode 100644 bsd-user/freebsd/os-syscall.c

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove
  2022-02-01 11:14 ` [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove Warner Losh
  2022-02-01 16:35   ` Kyle Evans
@ 2022-02-01 17:38   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:38 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/syscall.c | 516 ---------------------------------------------
>   1 file changed, 516 deletions(-)
>   delete mode 100644 bsd-user/syscall.c

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 10/22] bsd-user: Move system call building to os-syscall.c
  2022-02-01 11:14 ` [PATCH 10/22] bsd-user: Move system call building to os-syscall.c Warner Losh
@ 2022-02-01 17:38   ` Richard Henderson
  0 siblings, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:38 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/freebsd/meson.build | 1 +
>   bsd-user/meson.build         | 1 -
>   2 files changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno
  2022-02-01 11:14 ` [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno Warner Losh
  2022-02-01 16:35   ` Kyle Evans
@ 2022-02-01 17:39   ` Richard Henderson
  2022-02-01 23:27     ` Warner Losh
  1 sibling, 1 reply; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:39 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> +/*
> + * errno conversion.
> + */
> +abi_long get_errno(abi_long ret)
> +{
> +
> +    if (ret == -1) {

Watch the extra blank lines.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls
  2022-02-01 11:14 ` [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls Warner Losh
  2022-02-01 16:47   ` Kyle Evans
@ 2022-02-01 17:43   ` Richard Henderson
  2022-02-01 23:55     ` Warner Losh
  2022-02-26 16:24     ` Warner Losh
  1 sibling, 2 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 17:43 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> +#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~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec
  2022-02-01 11:14 ` [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec Warner Losh
  2022-02-01 16:46   ` Kyle Evans
@ 2022-02-01 21:16   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:16 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   bsd-user/freebsd/os-syscall.c | 92 +++++++++++++++++++++++++++++++++++
>   1 file changed, 92 insertions(+)
> 
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index 060134a9ecd..c21759ae7ce 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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));

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.

> +    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;
> +            }

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

     else if (len == 0 || bad_address)


r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec
  2022-02-01 11:14 ` [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec Warner Losh
  2022-02-01 16:37   ` Kyle Evans
@ 2022-02-01 21:29   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:29 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Releases the references to the iovec created by lock_iovec.
> 
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
>   1 file changed, 23 insertions(+)
> 
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index c21759ae7ce..d49945f0fcc 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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);

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~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 16/22] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate
  2022-02-01 11:14 ` [PATCH 16/22] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate Warner Losh
@ 2022-02-01 21:31   ` Richard Henderson
  0 siblings, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:31 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Add in the tracing and this system call not implemented boilerplate.
> 
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   bsd-user/freebsd/os-syscall.c | 27 ++++++++++++++++++++++++++-
>   1 file changed, 26 insertions(+), 1 deletion(-)
> 
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index d49945f0fcc..fcfa6221182 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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;
>   }

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~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 17/22] bsd-user: introduce target.h
  2022-02-01 11:14 ` [PATCH 17/22] bsd-user: introduce target.h Warner Losh
  2022-02-01 16:39   ` Kyle Evans
@ 2022-02-01 21:32   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:32 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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).
> 
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   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
> 
> diff --git a/bsd-user/arm/target.h b/bsd-user/arm/target.h
> new file mode 100644
> index 00000000000..1f7ee49bfb4
> --- /dev/null
> +++ b/bsd-user/arm/target.h
> @@ -0,0 +1,21 @@
> +/*
> + * Intel general target stuff that's common to all i386 details
> + *
> + * Copyright (c) 2022 M. Warner Losh <imp@bsdimp.com>
> + *
> + * 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,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 18/22] bsd-user: Define target_arg64
  2022-02-01 11:14 ` [PATCH 18/22] bsd-user: Define target_arg64 Warner Losh
  2022-02-01 16:41   ` Kyle Evans
@ 2022-02-01 21:33   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:33 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> 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.
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/qemu.h | 13 +++++++++++++
>   1 file changed, 13 insertions(+)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 19/22] bsd-user: Add safe system call macros
  2022-02-01 11:14 ` [PATCH 19/22] bsd-user: Add safe system call macros Warner Losh
@ 2022-02-01 21:33   ` Richard Henderson
  0 siblings, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:33 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Add a series of macros to create system call macros that go via the
> safe_syscall path.
> 
> Signed-off-by: Kyle Evans<kevans@FreeBSD.org>
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/syscall_defs.h | 47 +++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 47 insertions(+)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read,  pread, readv and preadv
  2022-02-01 11:14 ` [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv Warner Losh
@ 2022-02-01 21:37   ` Richard Henderson
  2022-02-02  3:02     ` Warner Losh
  0 siblings, 1 reply; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:37 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Implement do_bsd_{read,pread,readv,preadv}. Connect them to the system
> call table.
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   bsd-user/bsd-file.h           | 79 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c | 24 +++++++++++
>   2 files changed, 103 insertions(+)
> 
> diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h
> index 2f743db38e1..5934cbd5612 100644
> --- a/bsd-user/bsd-file.h
> +++ b/bsd-user/bsd-file.h
> @@ -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;
> +    }

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

But either way,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~

> +    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_ */
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index fcfa6221182..dda79af53de 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -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;



^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 21/22] bsd-user/bsd-file.h: Meat of the write system calls
  2022-02-01 11:14 ` [PATCH 21/22] bsd-user/bsd-file.h: Meat of the write system calls Warner Losh
@ 2022-02-01 21:38   ` Richard Henderson
  0 siblings, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:38 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Implement write, writev, pwrite and pwritev and connect them to the
> system call dispatch routine.
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Kyle Evans<kevans@FreeBSD.org>
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   bsd-user/bsd-file.h           | 85 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c | 23 ++++++++++
>   2 files changed, 108 insertions(+)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit
  2022-02-01 11:14 ` [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit Warner Losh
  2022-02-01 16:48   ` Kyle Evans
@ 2022-02-01 21:39   ` Richard Henderson
  1 sibling, 0 replies; 67+ messages in thread
From: Richard Henderson @ 2022-02-01 21:39 UTC (permalink / raw)
  To: Warner Losh, qemu-devel
  Cc: Peter Maydell, Stacey Son, arrowd, Kyle Evans, def, jrtc27, Brad Smith

On 2/1/22 22:14, Warner Losh wrote:
> Implement the exit system call. Bring in bsd-proc.h to contain all the
> process system call implementation and helper routines.
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Warner Losh<imp@bsdimp.com>
> ---
>   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

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~


^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall
  2022-02-01 16:22   ` Kyle Evans
@ 2022-02-01 23:13     ` Warner Losh
  0 siblings, 0 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 23:13 UTC (permalink / raw)
  To: Kyle Evans
  Cc: Peter Maydell, Gleb Popov, Richard Henderson, QEMU Developers,
	Konrad Witaszczyk, Jessica Clarke, Brad Smith

[-- Attachment #1: Type: text/plain, Size: 4988 bytes --]

On Tue, Feb 1, 2022 at 9:23 AM Kyle Evans <kevans@freebsd.org> wrote:

> On Tue, Feb 1, 2022 at 5:14 AM Warner Losh <imp@bsdimp.com> wrote:
> >
> > 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.
> >
> > Signed-off-by: Warner Losh <imp@bsdimp.com>
> > ---
> >  bsd-user/x86_64/target_arch_cpu.h | 27 ++++++++-------------------
> >  1 file changed, 8 insertions(+), 19 deletions(-)
> >
>
> Reviewed-by: Kyle Evans <kevans@FreeBSD.org>
>
> 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


> > diff --git a/bsd-user/x86_64/target_arch_cpu.h
> b/bsd-user/x86_64/target_arch_cpu.h
> > index 9dc52d5afc4..5be2f02416e 100644
> > --- a/bsd-user/x86_64/target_arch_cpu.h
> > +++ b/bsd-user/x86_64/target_arch_cpu.h
> > @@ -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
> >
>

[-- Attachment #2: Type: text/html, Size: 6961 bytes --]

^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls
  2022-02-01 16:32   ` Kyle Evans
@ 2022-02-01 23:19     ` Warner Losh
  0 siblings, 0 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 23:19 UTC (permalink / raw)
  To: Kyle Evans
  Cc: Peter Maydell, Gleb Popov, Richard Henderson, QEMU Developers,
	Konrad Witaszczyk, Jessica Clarke, Brad Smith

[-- Attachment #1: Type: text/plain, Size: 9022 bytes --]

On Tue, Feb 1, 2022 at 9:32 AM Kyle Evans <kevans@freebsd.org> wrote:

> On Tue, Feb 1, 2022 at 5:14 AM Warner Losh <imp@bsdimp.com> wrote:
> >
> > 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.
> >
> > Signed-off-by: Warner Losh <imp@bsdimp.com>
> > ---
> >  bsd-user/arm/target_arch_cpu.h | 139 ++++++++++++++++-----------------
> >  1 file changed, 66 insertions(+), 73 deletions(-)
> >
> > diff --git a/bsd-user/arm/target_arch_cpu.h
> b/bsd-user/arm/target_arch_cpu.h
> > index b087db48fa4..afb7814a8d1 100644
> > --- a/bsd-user/arm/target_arch_cpu.h
> > +++ b/bsd-user/arm/target_arch_cpu.h
> > @@ -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;
> >
>
> 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


> Reviewed-by: Kyle Evans <kevans@FreeBSD.org>
>

[-- Attachment #2: Type: text/html, Size: 12459 bytes --]

^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno
  2022-02-01 17:39   ` Richard Henderson
@ 2022-02-01 23:27     ` Warner Losh
  0 siblings, 0 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 23:27 UTC (permalink / raw)
  To: Richard Henderson
  Cc: Peter Maydell, Gleb Popov, Kyle Evans, QEMU Developers,
	Konrad Witaszczyk, Jessica Clarke, Brad Smith

[-- Attachment #1: Type: text/plain, Size: 448 bytes --]

On Tue, Feb 1, 2022 at 10:40 AM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 2/1/22 22:14, Warner Losh wrote:
> > +/*
> > + * 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... :)


> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>

Thanks!

Warner


>
>
> r~
>

[-- Attachment #2: Type: text/html, Size: 1263 bytes --]

^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls
  2022-02-01 17:43   ` Richard Henderson
@ 2022-02-01 23:55     ` Warner Losh
  2022-02-26 16:24     ` Warner Losh
  1 sibling, 0 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-01 23:55 UTC (permalink / raw)
  To: Richard Henderson
  Cc: Peter Maydell, Gleb Popov, Kyle Evans, QEMU Developers,
	Konrad Witaszczyk, Jessica Clarke, Brad Smith

[-- Attachment #1: Type: text/plain, Size: 1317 bytes --]

On Tue, Feb 1, 2022 at 10:43 AM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 2/1/22 22:14, Warner Losh wrote:
> > +#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

[-- Attachment #2: Type: text/html, Size: 1922 bytes --]

^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read,  pread, readv and preadv
  2022-02-01 21:37   ` Richard Henderson
@ 2022-02-02  3:02     ` Warner Losh
  0 siblings, 0 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-02  3:02 UTC (permalink / raw)
  To: Richard Henderson
  Cc: Peter Maydell, Stacey Son, Gleb Popov, Kyle Evans,
	QEMU Developers, Konrad Witaszczyk, Jessica Clarke, Brad Smith

[-- Attachment #1: Type: text/plain, Size: 5160 bytes --]

On Tue, Feb 1, 2022 at 2:37 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 2/1/22 22:14, Warner Losh wrote:
> > Implement do_bsd_{read,pread,readv,preadv}. Connect them to the system
> > call table.
> >
> > Signed-off-by: Stacey Son <sson@FreeBSD.org>
> > Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
> > Signed-off-by: Warner Losh <imp@bsdimp.com>
> > ---
> >   bsd-user/bsd-file.h           | 79 +++++++++++++++++++++++++++++++++++
> >   bsd-user/freebsd/os-syscall.c | 24 +++++++++++
> >   2 files changed, 103 insertions(+)
> >
> > diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h
> > index 2f743db38e1..5934cbd5612 100644
> > --- a/bsd-user/bsd-file.h
> > +++ b/bsd-user/bsd-file.h
> > @@ -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;
> > +    }
>
> 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


> But either way,
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>
>
> r~
>
> > +    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_ */
> > diff --git a/bsd-user/freebsd/os-syscall.c
> b/bsd-user/freebsd/os-syscall.c
> > index fcfa6221182..dda79af53de 100644
> > --- a/bsd-user/freebsd/os-syscall.c
> > +++ b/bsd-user/freebsd/os-syscall.c
> > @@ -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;
>
>

[-- Attachment #2: Type: text/html, Size: 6948 bytes --]

^ permalink raw reply	[flat|nested] 67+ messages in thread

* Re: [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls
  2022-02-01 17:43   ` Richard Henderson
  2022-02-01 23:55     ` Warner Losh
@ 2022-02-26 16:24     ` Warner Losh
  1 sibling, 0 replies; 67+ messages in thread
From: Warner Losh @ 2022-02-26 16:24 UTC (permalink / raw)
  To: Richard Henderson
  Cc: Peter Maydell, Gleb Popov, Kyle Evans, QEMU Developers,
	Konrad Witaszczyk, Jessica Clarke, Brad Smith

[-- Attachment #1: Type: text/plain, Size: 626 bytes --]

On Tue, Feb 1, 2022 at 10:43 AM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 2/1/22 22:14, Warner Losh wrote:
> > +#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.

[-- Attachment #2: Type: text/html, Size: 1101 bytes --]

^ permalink raw reply	[flat|nested] 67+ messages in thread

end of thread, other threads:[~2022-02-26 16:26 UTC | newest]

Thread overview: 67+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-01 11:14 [PATCH 00/22] bsd-user: Start upstreaming the system calls Warner Losh
2022-02-01 11:14 ` [PATCH 01/22] bsd-user/main.c: Drop syscall flavor arg -bsd Warner Losh
2022-02-01 15:54   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 02/22] bsd-user/x86_64/target_arch_cpu.h: int $80 never was a BSD system call on amd64 Warner Losh
2022-02-01 15:57   ` Richard Henderson
2022-02-01 16:19   ` Kyle Evans
2022-02-01 11:14 ` [PATCH 03/22] bsd-user/x86_64/target_arch_cpu.h: Remove openbsd syscall Warner Losh
2022-02-01 16:22   ` Kyle Evans
2022-02-01 23:13     ` Warner Losh
2022-02-01 17:27   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 04/22] bsd-user/i386/target_arch_cpu.h: " Warner Losh
2022-02-01 16:24   ` Kyle Evans
2022-02-01 17:29   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 05/22] bsd-user/arm/target_arch_cpu.h: Only support FreeBSD sys calls Warner Losh
2022-02-01 16:32   ` Kyle Evans
2022-02-01 23:19     ` Warner Losh
2022-02-01 17:31   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 06/22] bsd-user/arm/target_arch_thread.h: Assume a FreeBSD target Warner Losh
2022-02-01 16:33   ` Kyle Evans
2022-02-01 17:34   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 07/22] bsd-user/x86_64/target_arch_thread.h: " Warner Losh
2022-02-01 16:36   ` Kyle Evans
2022-02-01 17:34   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 08/22] bsd-user: Remove bsd_type Warner Losh
2022-02-01 16:34   ` Kyle Evans
2022-02-01 17:35   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 09/22] bsd-user/freebsd/os-syscall.c: Move syscall processing here Warner Losh
2022-02-01 16:18   ` Kyle Evans
2022-02-01 17:37   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 10/22] bsd-user: Move system call building to os-syscall.c Warner Losh
2022-02-01 17:38   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 11/22] bsd-user/sycall.c: Now obsolete, remove Warner Losh
2022-02-01 16:35   ` Kyle Evans
2022-02-01 17:38   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 12/22] bsd-user/freebsd/os-syscall.c: Add get_errno and host_to_target_errno Warner Losh
2022-02-01 16:35   ` Kyle Evans
2022-02-01 17:39   ` Richard Henderson
2022-02-01 23:27     ` Warner Losh
2022-02-01 11:14 ` [PATCH 13/22] bsd-user/bsd-file.h: Implementation details for the filesystem calls Warner Losh
2022-02-01 16:47   ` Kyle Evans
2022-02-01 17:43   ` Richard Henderson
2022-02-01 23:55     ` Warner Losh
2022-02-26 16:24     ` Warner Losh
2022-02-01 11:14 ` [PATCH 14/22] bsd-user/freebsd/os-syscall.c: lock_iovec Warner Losh
2022-02-01 16:46   ` Kyle Evans
2022-02-01 21:16   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 15/22] bsd-user/freebsd/os-syscall.c: unlock_iovec Warner Losh
2022-02-01 16:37   ` Kyle Evans
2022-02-01 21:29   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 16/22] bsd-user/freebsd/os-syscall.c: Tracing and error boilerplate Warner Losh
2022-02-01 21:31   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 17/22] bsd-user: introduce target.h Warner Losh
2022-02-01 16:39   ` Kyle Evans
2022-02-01 21:32   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 18/22] bsd-user: Define target_arg64 Warner Losh
2022-02-01 16:41   ` Kyle Evans
2022-02-01 21:33   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 19/22] bsd-user: Add safe system call macros Warner Losh
2022-02-01 21:33   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 20/22] bsd-user/bsd-file.h: Add implementations for read, pread, readv and preadv Warner Losh
2022-02-01 21:37   ` Richard Henderson
2022-02-02  3:02     ` Warner Losh
2022-02-01 11:14 ` [PATCH 21/22] bsd-user/bsd-file.h: Meat of the write system calls Warner Losh
2022-02-01 21:38   ` Richard Henderson
2022-02-01 11:14 ` [PATCH 22/22] bsd-user/freebsd/os-syscall.c: Implement exit Warner Losh
2022-02-01 16:48   ` Kyle Evans
2022-02-01 21:39   ` Richard Henderson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.