qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/32] bsd-user: Implement freebsd process related system calls.
@ 2023-08-27 15:57 Karim Taha
  2023-08-27 15:57 ` [PATCH 01/32] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics Karim Taha
                   ` (31 more replies)
  0 siblings, 32 replies; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Karim Taha



Karim Taha (1):
  bsd-user: Add freebsd/os-proc.c to meson.build

Kyle Evans (2):
  bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics.
  bsd-user: Get number of cpus.

Stacey Son (28):
  bsd-user: Implement procctl(2) system call.
  bsd-user: Implement host_to_target_siginfo.
  bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h.
  bsd-user: add extern declarations for bsd-proc.c conversion functions
  bsd-user: Implement target_to_host_resource conversion function
  bsd-user: Implement target_to_host_rlim and host_to_target_rlim
    conversion.
  bsd-user: Implement host_to_target_rusage and host_to_target_wrusage.
  bsd-user: Implement host_to_target_waitstatus conversion.
  bsd-user: Implement getgroups(2) and setgroups(2) system calls.
  bsd-user: Implement umask(2), setlogin(2) and getlogin(2)
  bsd-user: Implement getrusage(2).
  bsd-user: Implement getrlimit(2) and setrlimit(2)
  bsd-user: Implement several get/set system calls:
  bsd-user: Implement get/set[resuid/resgid/sid] and issetugid.
  bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2).
  bsd-user: Implement getpriority(2) and setpriority(2).
  bsd-user: Implement get_filename_from_fd.
  bsd-user: Implement freebsd_exec_common, used in implementing
    execve/fexecve.
  bsd-user: Implement t2h procctl control request commands and h2t
    reaper status struct conversion.
  bsd-user: Implement h2t reaper_pidinfo and h2t/t2h reaper_kill structs
    conversion functions.
  bsd-user: Implement procctl(2) system call.
  bsd-user: Implement execve(2) and fexecve(2) system calls.
  bsd-user: Implement wait4(2) and wait6(2) system calls.
  bsd-user: Implement setloginclass(2) and getloginclass(2) system
    calls.
  bsd-user: Implement pdgetpid(2) and the undocumented setugid.
  bsd-user: Implement fork(2) and vfork(2) system calls.
  bsd-user: Implement rfork(2) system call.
  bsd-user: Implement pdfork(2) system call.

Warner Losh (1):
  bsd-user: Add bsd-proc.c to meson.build

 bsd-user/bsd-proc.c           | 226 ++++++++++++++++
 bsd-user/bsd-proc.h           | 382 +++++++++++++++++++++++++++
 bsd-user/freebsd/meson.build  |   1 +
 bsd-user/freebsd/os-proc.c    | 467 ++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-proc.h    | 283 ++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 204 +++++++++++++++
 bsd-user/main.c               |   2 +-
 bsd-user/meson.build          |   6 +
 bsd-user/qemu-bsd.h           |  38 +++
 bsd-user/qemu.h               |   7 +
 bsd-user/signal-common.h      |   1 +
 bsd-user/signal.c             |   6 +
 bsd-user/syscall_defs.h       |  46 ++++
 13 files changed, 1668 insertions(+), 1 deletion(-)
 create mode 100644 bsd-user/bsd-proc.c
 create mode 100644 bsd-user/freebsd/os-proc.c
 create mode 100644 bsd-user/freebsd/os-proc.h
 create mode 100644 bsd-user/qemu-bsd.h

-- 
2.40.0



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

* [PATCH 01/32] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:07   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 02/32] bsd-user: Implement procctl(2) system call Karim Taha
                   ` (30 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Kyle Evans, Karim Taha

From: Kyle Evans <kevans@FreeBSD.org>

Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/syscall_defs.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index e4825f2662..daf7f5637e 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -179,6 +179,10 @@ struct target_freebsd__wrusage {
     struct target_freebsd_rusage wru_children;
 };
 
+/* sys/unistd.h */
+/* user: vfork(2) semantics, clear signals */
+#define TARGET_RFSPAWN (1U << 31)
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
-- 
2.40.0



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

* [PATCH 02/32] bsd-user: Implement procctl(2) system call.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
  2023-08-27 15:57 ` [PATCH 01/32] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:10   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 03/32] bsd-user: Implement host_to_target_siginfo Karim Taha
                   ` (29 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Implement procctl flags and related structs:
struct target_procctl_reaper_status
struct target_procctl_reaper_pidinfo
struct target_procctl_reaper_pids
struct target_procctl_reaper_kill

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/syscall_defs.h | 42 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index daf7f5637e..25b3d304ca 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -183,6 +183,48 @@ struct target_freebsd__wrusage {
 /* user: vfork(2) semantics, clear signals */
 #define TARGET_RFSPAWN (1U << 31)
 
+/*
+ * from sys/procctl.h
+ */
+#define TARGET_PROC_SPROTECT            1
+#define TARGET_PROC_REAP_ACQUIRE        2
+#define TARGET_PROC_REAP_RELEASE        3
+#define TARGET_PROC_REAP_STATUS         4
+#define TARGET_PROC_REAP_GETPIDS        5
+#define TARGET_PROC_REAP_KILL           6
+
+struct target_procctl_reaper_status {
+    uint32_t rs_flags;
+    uint32_t rs_children;
+    uint32_t rs_descendants;
+    uint32_t rs_reaper;
+    uint32_t rs_pid;
+    uint32_t rs_pad0[15];
+};
+
+struct target_procctl_reaper_pidinfo {
+    uint32_t pi_pid;
+    uint32_t pi_subtree;
+    uint32_t pi_flags;
+    uint32_t pi_pad0[15];
+};
+
+struct target_procctl_reaper_pids {
+    uint32_t rp_count;
+    uint32_t rp_pad0[15];
+    abi_ulong rp_pids;
+};
+
+struct target_procctl_reaper_kill {
+    int32_t  rk_sig;
+    uint32_t rk_flags;
+    uint32_t rk_subtree;
+    uint32_t rk_killed;
+    uint32_t rk_fpid;
+    uint32_t rk_pad0[15];
+};
+
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
-- 
2.40.0



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

* [PATCH 03/32] bsd-user: Implement host_to_target_siginfo.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
  2023-08-27 15:57 ` [PATCH 01/32] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics Karim Taha
  2023-08-27 15:57 ` [PATCH 02/32] bsd-user: Implement procctl(2) system call Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:13   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 04/32] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h Karim Taha
                   ` (28 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Used in wait6 system call

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/signal-common.h | 1 +
 bsd-user/signal.c        | 6 ++++++
 2 files changed, 7 insertions(+)

diff --git a/bsd-user/signal-common.h b/bsd-user/signal-common.h
index 6f90345bb2..e37e1c3f9c 100644
--- a/bsd-user/signal-common.h
+++ b/bsd-user/signal-common.h
@@ -35,6 +35,7 @@ int do_sigaction(int sig, const struct target_sigaction *act,
 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
 long do_sigreturn(CPUArchState *env, abi_ulong addr);
 void force_sig_fault(int sig, int code, abi_ulong addr);
+void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
 int host_to_target_signal(int sig);
 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s);
 void process_pending_signals(CPUArchState *env);
diff --git a/bsd-user/signal.c b/bsd-user/signal.c
index 4db85a3485..3ee2ceb910 100644
--- a/bsd-user/signal.c
+++ b/bsd-user/signal.c
@@ -311,6 +311,12 @@ static void tswap_siginfo(target_siginfo_t *tinfo, const target_siginfo_t *info)
     }
 }
 
+void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
+{
+    host_to_target_siginfo_noswap(tinfo, info);
+    tswap_siginfo(tinfo, tinfo);
+}
+
 int block_signals(void)
 {
     TaskState *ts = (TaskState *)thread_cpu->opaque;
-- 
2.40.0



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

* [PATCH 04/32] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (2 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 03/32] bsd-user: Implement host_to_target_siginfo Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:14   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 05/32] bsd-user: add extern declarations for bsd-proc.c conversion functions Karim Taha
                   ` (27 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/main.c | 2 +-
 bsd-user/qemu.h | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 381bb18df8..b94b2d34b6 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -88,7 +88,7 @@ unsigned long reserved_va = MAX_RESERVED_VA;
 unsigned long reserved_va;
 #endif
 
-static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
+const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
 char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
 
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 6724bb9f0a..23bbdd3e0c 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -113,6 +113,7 @@ typedef struct TaskState {
 } __attribute__((aligned(16))) TaskState;
 
 void stop_all_tasks(void);
+extern const char *interp_prefix;
 extern const char *qemu_uname_release;
 
 /*
@@ -251,6 +252,12 @@ abi_long get_errno(abi_long ret);
 bool is_error(abi_long ret);
 int host_to_target_errno(int err);
 
+/* os-proc.c */
+abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
+        abi_ulong guest_envp, int do_fexec);
+abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
+        abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
+
 /* os-sys.c */
 abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
         abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
-- 
2.40.0



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

* [PATCH 05/32] bsd-user: add extern declarations for bsd-proc.c conversion functions
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (3 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 04/32] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:15   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 06/32] bsd-user: Add bsd-proc.c to meson.build Karim Taha
                   ` (26 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/qemu-bsd.h | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 bsd-user/qemu-bsd.h

diff --git a/bsd-user/qemu-bsd.h b/bsd-user/qemu-bsd.h
new file mode 100644
index 0000000000..b93a0b7fd5
--- /dev/null
+++ b/bsd-user/qemu-bsd.h
@@ -0,0 +1,38 @@
+/*
+ *  BSD conversion extern declarations
+ *
+ *  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 QEMU_BSD_H
+#define QEMU_BSD_H
+
+#include <sys/types.h>
+#include <sys/resource.h>
+
+/* bsd-proc.c */
+int target_to_host_resource(int code);
+rlim_t target_to_host_rlim(abi_llong target_rlim);
+abi_llong host_to_target_rlim(rlim_t rlim);
+abi_long host_to_target_rusage(abi_ulong target_addr,
+        const struct rusage *rusage);
+abi_long host_to_target_wrusage(abi_ulong target_addr,
+        const struct __wrusage *wrusage);
+int host_to_target_waitstatus(int status);
+void h2g_rusage(const struct rusage *rusage,
+        struct target_freebsd_rusage *target_rusage);
+
+#endif /* QEMU_BSD_H */
-- 
2.40.0



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

* [PATCH 06/32] bsd-user: Add bsd-proc.c to meson.build
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (4 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 05/32] bsd-user: add extern declarations for bsd-proc.c conversion functions Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:17   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 07/32] bsd-user: Implement target_to_host_resource conversion function Karim Taha
                   ` (25 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Karim Taha

From: Warner Losh <imp@bsdimp.com>

Signed-off-by: Warner Losh <imp@bsdimp.com>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h  | 4 ++++
 bsd-user/meson.build | 6 ++++++
 2 files changed, 10 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index a1061bffb8..048773a75d 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -22,6 +22,10 @@
 
 #include <sys/resource.h>
 
+#include "qemu-bsd.h"
+#include "gdbstub/syscalls.h"
+#include "qemu/plugin.h"
+
 /* exit(2) */
 static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
 {
diff --git a/bsd-user/meson.build b/bsd-user/meson.build
index 5243122fc5..b97fce1472 100644
--- a/bsd-user/meson.build
+++ b/bsd-user/meson.build
@@ -7,6 +7,7 @@ bsd_user_ss = ss.source_set()
 common_user_inc += include_directories('include')
 
 bsd_user_ss.add(files(
+  'bsd-proc.c',
   'bsdload.c',
   'elfload.c',
   'main.c',
@@ -16,6 +17,11 @@ bsd_user_ss.add(files(
   'uaccess.c',
 ))
 
+elf = cc.find_library('elf', required: true)
+procstat = cc.find_library('procstat', required: true)
+kvm = cc.find_library('kvm', required: true)
+bsd_user_ss.add(elf, procstat, kvm)
+
 # Pull in the OS-specific build glue, if any
 subdir(targetos)
 
-- 
2.40.0



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

* [PATCH 07/32] bsd-user: Implement target_to_host_resource conversion function
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (5 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 06/32] bsd-user: Add bsd-proc.c to meson.build Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:33   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 08/32] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion Karim Taha
                   ` (24 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
 create mode 100644 bsd-user/bsd-proc.c

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
new file mode 100644
index 0000000000..ae2e636bb3
--- /dev/null
+++ b/bsd-user/bsd-proc.c
@@ -0,0 +1,83 @@
+/*
+ *  BSD process related system call helpers
+ *
+ *  Copyright (c) 2013-14 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/>.
+ */
+#include "qemu/osdep.h"
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/resource.h>
+#include <sys/wait.h>
+
+#include "qemu.h"
+#include "qemu-bsd.h"
+#include "signal-common.h"
+
+#include "bsd-proc.h"
+
+/*
+ * resource/rusage conversion
+ */
+int target_to_host_resource(int code)
+{
+
+    switch (code) {
+    case TARGET_RLIMIT_AS:
+        return RLIMIT_AS;
+
+    case TARGET_RLIMIT_CORE:
+        return RLIMIT_CORE;
+
+    case TARGET_RLIMIT_CPU:
+        return RLIMIT_CPU;
+
+    case TARGET_RLIMIT_DATA:
+        return RLIMIT_DATA;
+
+    case TARGET_RLIMIT_FSIZE:
+        return RLIMIT_FSIZE;
+
+    case TARGET_RLIMIT_MEMLOCK:
+        return RLIMIT_MEMLOCK;
+
+    case TARGET_RLIMIT_NOFILE:
+        return RLIMIT_NOFILE;
+
+    case TARGET_RLIMIT_NPROC:
+        return RLIMIT_NPROC;
+
+    case TARGET_RLIMIT_RSS:
+        return RLIMIT_RSS;
+
+    case TARGET_RLIMIT_SBSIZE:
+        return RLIMIT_SBSIZE;
+
+    case TARGET_RLIMIT_STACK:
+        return RLIMIT_STACK;
+
+    case TARGET_RLIMIT_SWAP:
+        return RLIMIT_SWAP;
+
+    case TARGET_RLIMIT_NPTS:
+        return RLIMIT_NPTS;
+
+    default:
+        return code;
+    }
+}
+
-- 
2.40.0



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

* [PATCH 08/32] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (6 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 07/32] bsd-user: Implement target_to_host_resource conversion function Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:36   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 09/32] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage Karim Taha
                   ` (23 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index ae2e636bb3..12e43cfeca 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -81,3 +81,36 @@ int target_to_host_resource(int code)
     }
 }
 
+rlim_t target_to_host_rlim(abi_llong target_rlim)
+{
+    abi_llong target_rlim_swap;
+    rlim_t result;
+
+    target_rlim_swap = tswap64(target_rlim);
+    if (target_rlim_swap == TARGET_RLIM_INFINITY) {
+        return RLIM_INFINITY;
+    }
+
+    result = target_rlim_swap;
+    if (target_rlim_swap != (rlim_t)result) {
+        return RLIM_INFINITY;
+    }
+
+    return result;
+}
+
+abi_llong host_to_target_rlim(rlim_t rlim)
+{
+    abi_llong target_rlim_swap;
+    abi_llong result;
+
+    if (rlim == RLIM_INFINITY || rlim != (abi_llong)rlim) {
+        target_rlim_swap = TARGET_RLIM_INFINITY;
+    } else {
+        target_rlim_swap = rlim;
+    }
+    result = tswap64(target_rlim_swap);
+
+    return result;
+}
+
-- 
2.40.0



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

* [PATCH 09/32] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (7 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 08/32] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:39   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 10/32] bsd-user: Implement host_to_target_waitstatus conversion Karim Taha
                   ` (22 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.c | 54 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 12e43cfeca..8e6dd5e427 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -114,3 +114,57 @@ abi_llong host_to_target_rlim(rlim_t rlim)
     return result;
 }
 
+void h2g_rusage(const struct rusage *rusage,
+                struct target_freebsd_rusage *target_rusage)
+{
+    __put_user(rusage->ru_utime.tv_sec, &target_rusage->ru_utime.tv_sec);
+    __put_user(rusage->ru_utime.tv_usec, &target_rusage->ru_utime.tv_usec);
+
+    __put_user(rusage->ru_stime.tv_sec, &target_rusage->ru_stime.tv_sec);
+    __put_user(rusage->ru_stime.tv_usec, &target_rusage->ru_stime.tv_usec);
+
+    __put_user(rusage->ru_maxrss, &target_rusage->ru_maxrss);
+    __put_user(rusage->ru_idrss, &target_rusage->ru_idrss);
+    __put_user(rusage->ru_idrss, &target_rusage->ru_idrss);
+    __put_user(rusage->ru_isrss, &target_rusage->ru_isrss);
+    __put_user(rusage->ru_minflt, &target_rusage->ru_minflt);
+    __put_user(rusage->ru_majflt, &target_rusage->ru_majflt);
+    __put_user(rusage->ru_nswap, &target_rusage->ru_nswap);
+    __put_user(rusage->ru_inblock, &target_rusage->ru_inblock);
+    __put_user(rusage->ru_oublock, &target_rusage->ru_oublock);
+    __put_user(rusage->ru_msgsnd, &target_rusage->ru_msgsnd);
+    __put_user(rusage->ru_msgrcv, &target_rusage->ru_msgrcv);
+    __put_user(rusage->ru_nsignals, &target_rusage->ru_nsignals);
+    __put_user(rusage->ru_nvcsw, &target_rusage->ru_nvcsw);
+    __put_user(rusage->ru_nivcsw, &target_rusage->ru_nivcsw);
+}
+
+abi_long host_to_target_rusage(abi_ulong target_addr,
+        const struct rusage *rusage)
+{
+    struct target_freebsd_rusage *target_rusage;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    h2g_rusage(rusage, target_rusage);
+    unlock_user_struct(target_rusage, target_addr, 1);
+
+    return 0;
+}
+
+abi_long host_to_target_wrusage(abi_ulong target_addr,
+                                const struct __wrusage *wrusage)
+{
+    struct target_freebsd__wrusage *target_wrusage;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_wrusage, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    h2g_rusage(&wrusage->wru_self, &target_wrusage->wru_self);
+    h2g_rusage(&wrusage->wru_children, &target_wrusage->wru_children);
+    unlock_user_struct(target_wrusage, target_addr, 1);
+
+    return 0;
+}
+
-- 
2.40.0



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

* [PATCH 10/32] bsd-user: Implement host_to_target_waitstatus conversion.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (8 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 09/32] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:40   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 11/32] bsd-user: Get number of cpus Karim Taha
                   ` (21 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 8e6dd5e427..49c0fb67d0 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -168,3 +168,20 @@ abi_long host_to_target_wrusage(abi_ulong target_addr,
     return 0;
 }
 
+/*
+ * wait status conversion.
+ *
+ * Map host to target signal numbers for the wait family of syscalls.
+ * Assume all other status bits are the same.
+ */
+int host_to_target_waitstatus(int status)
+{
+    if (WIFSIGNALED(status)) {
+        return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
+    }
+    if (WIFSTOPPED(status)) {
+        return (host_to_target_signal(WSTOPSIG(status)) << 8) | (status & 0xff);
+    }
+    return status;
+}
+
-- 
2.40.0



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

* [PATCH 11/32] bsd-user: Get number of cpus.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (9 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 10/32] bsd-user: Implement host_to_target_waitstatus conversion Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:49   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 12/32] bsd-user: Implement getgroups(2) and setgroups(2) system calls Karim Taha
                   ` (20 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Kyle Evans, Karim Taha

From: Kyle Evans <kevans@FreeBSD.org>

Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.c | 39 +++++++++++++++++++++++++++++++++++++++
 bsd-user/bsd-proc.h |  2 ++
 2 files changed, 41 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 49c0fb67d0..dd6bad6de3 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -185,3 +185,42 @@ int host_to_target_waitstatus(int status)
     return status;
 }
 
+int bsd_get_ncpu(void)
+{
+    static int ncpu = -1;
+
+    if (ncpu != -1) {
+        return ncpu;
+    }
+    if (ncpu == -1) {
+        cpuset_t mask;
+
+        CPU_ZERO(&mask);
+
+        if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
+                               &mask) == 0) {
+            ncpu = CPU_COUNT(&mask);
+        }
+    }
+#ifdef _SC_NPROCESSORS_ONLN
+    if (ncpu == -1)
+        ncpu = sysconf(_SC_NPROCESSORS_ONLN);
+#endif
+#if defined(CTL_HW) && defined(HW_NCPU)
+    if (ncpu == -1) {
+        int mib[2] = {CTL_HW, HW_NCPU};
+        size_t sz;
+
+        sz = sizeof(ncpu);
+        if (sysctl(mib, 2, &ncpu, &sz, NULL, NULL) == -1) {
+            ncpu = -1;
+        }
+    }
+#endif
+    if (ncpu == -1) {
+        gemu_log("XXX Missing bsd_get_ncpu() implementation\n");
+        ncpu = 1;
+    }
+    return ncpu;
+}
+
diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 048773a75d..b6225e520e 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -26,6 +26,8 @@
 #include "gdbstub/syscalls.h"
 #include "qemu/plugin.h"
 
+int bsd_get_ncpu(void);
+
 /* exit(2) */
 static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
 {
-- 
2.40.0



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

* [PATCH 12/32] bsd-user: Implement getgroups(2) and setgroups(2) system calls.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (10 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 11/32] bsd-user: Get number of cpus Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:53   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 13/32] bsd-user: Implement umask(2), setlogin(2) and getlogin(2) Karim Taha
                   ` (19 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 44 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  9 +++++++
 2 files changed, 53 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index b6225e520e..ecd6a13c2d 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -41,4 +41,48 @@ static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
     return 0;
 }
 
+/* getgroups(2) */
+static inline abi_long do_bsd_getgroups(abi_long gidsetsize, abi_long arg2)
+{
+    abi_long ret;
+    uint32_t *target_grouplist;
+    gid_t *grouplist;
+    int i;
+
+    grouplist = alloca(gidsetsize * sizeof(gid_t));
+    ret = get_errno(getgroups(gidsetsize, grouplist));
+    if (gidsetsize != 0) {
+        if (!is_error(ret)) {
+            target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
+            if (!target_grouplist) {
+                return -TARGET_EFAULT;
+            }
+            for (i = 0; i < ret; i++) {
+                target_grouplist[i] = tswap32(grouplist[i]);
+            }
+            unlock_user(target_grouplist, arg2, gidsetsize * 2);
+        }
+    }
+    return ret;
+}
+
+/* setgroups(2) */
+static inline abi_long do_bsd_setgroups(abi_long gidsetsize, abi_long arg2)
+{
+    uint32_t *target_grouplist;
+    gid_t *grouplist;
+    int i;
+
+    grouplist = alloca(gidsetsize * sizeof(gid_t));
+    target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
+    if (!target_grouplist) {
+        return -TARGET_EFAULT;
+    }
+    for (i = 0; i < gidsetsize; i++) {
+        grouplist[i] = tswap32(target_grouplist[i]);
+    }
+    unlock_user(target_grouplist, arg2, 0);
+    return get_errno(setgroups(gidsetsize, grouplist));
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 2224a280ea..17160ab532 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -220,6 +220,15 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_exit(cpu_env, arg1);
         break;
 
+    case TARGET_FREEBSD_NR_getgroups: /* getgroups(2) */
+        ret = do_bsd_getgroups(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_setgroups: /* setgroups(2) */
+        ret = do_bsd_setgroups(arg1, arg2);
+        break;
+
+
         /*
          * File system calls.
          */
-- 
2.40.0



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

* [PATCH 13/32] bsd-user: Implement umask(2), setlogin(2) and getlogin(2)
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (11 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 12/32] bsd-user: Implement getgroups(2) and setgroups(2) system calls Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:56   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 14/32] bsd-user: Implement getrusage(2) Karim Taha
                   ` (18 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 39 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 12 +++++++++++
 2 files changed, 51 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index ecd6a13c2d..5228b4be78 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -26,6 +26,7 @@
 #include "gdbstub/syscalls.h"
 #include "qemu/plugin.h"
 
+extern int _getlogin(char*, int);
 int bsd_get_ncpu(void);
 
 /* exit(2) */
@@ -85,4 +86,42 @@ static inline abi_long do_bsd_setgroups(abi_long gidsetsize, abi_long arg2)
     return get_errno(setgroups(gidsetsize, grouplist));
 }
 
+/* umask(2) */
+static inline abi_long do_bsd_umask(abi_long arg1)
+{
+    return get_errno(umask(arg1));
+}
+
+/* setlogin(2) */
+static inline abi_long do_bsd_setlogin(abi_long arg1)
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user_string(arg1);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(setlogin(p));
+    unlock_user(p, arg1, 0);
+
+    return ret;
+}
+
+/* getlogin(2) */
+static inline abi_long do_bsd_getlogin(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user_string(arg1);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(_getlogin(p, arg2));
+    unlock_user(p, arg1, 0);
+
+    return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 17160ab532..194248924d 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -228,6 +228,18 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_setgroups(arg1, arg2);
         break;
 
+    case TARGET_FREEBSD_NR_umask: /* umask(2) */
+        ret = do_bsd_umask(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_setlogin: /* setlogin(2) */
+        ret = do_bsd_setlogin(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_getlogin: /* getlogin(2) */
+        ret = do_bsd_getlogin(arg1, arg2);
+        break;
+
 
         /*
          * File system calls.
-- 
2.40.0



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

* [PATCH 14/32] bsd-user: Implement getrusage(2).
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (12 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 13/32] bsd-user: Implement umask(2), setlogin(2) and getlogin(2) Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:57   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 15/32] bsd-user: Implement getrlimit(2) and setrlimit(2) Karim Taha
                   ` (17 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 13 +++++++++++++
 bsd-user/freebsd/os-syscall.c |  4 ++++
 2 files changed, 17 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 5228b4be78..ddb5a4d452 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -124,4 +124,17 @@ static inline abi_long do_bsd_getlogin(abi_long arg1, abi_long arg2)
     return ret;
 }
 
+/* getrusage(2) */
+static inline abi_long do_bsd_getrusage(abi_long who, abi_ulong target_addr)
+{
+    abi_long ret;
+    struct rusage rusage;
+
+    ret = get_errno(getrusage(who, &rusage));
+    if (!is_error(ret)) {
+        host_to_target_rusage(target_addr, &rusage);
+    }
+    return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 194248924d..f7c4a64f9a 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -240,6 +240,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_getlogin(arg1, arg2);
         break;
 
+    case TARGET_FREEBSD_NR_getrusage: /* getrusage(2) */
+        ret = do_bsd_getrusage(arg1, arg2);
+        break;
+
 
         /*
          * File system calls.
-- 
2.40.0



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

* [PATCH 15/32] bsd-user: Implement getrlimit(2) and setrlimit(2)
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (13 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 14/32] bsd-user: Implement getrusage(2) Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:58   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 16/32] bsd-user: Implement several get/set system calls: Karim Taha
                   ` (16 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 59 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  8 +++++
 2 files changed, 67 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index ddb5a4d452..2c225b12bc 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -137,4 +137,63 @@ static inline abi_long do_bsd_getrusage(abi_long who, abi_ulong target_addr)
     return ret;
 }
 
+/* getrlimit(2) */
+static inline abi_long do_bsd_getrlimit(abi_long arg1, abi_ulong arg2)
+{
+    abi_long ret;
+    int resource = target_to_host_resource(arg1);
+    struct target_rlimit *target_rlim;
+    struct rlimit rlim;
+
+    switch (resource) {
+    case RLIMIT_STACK:
+        rlim.rlim_cur = target_dflssiz;
+        rlim.rlim_max = target_maxssiz;
+        ret = 0;
+        break;
+
+    case RLIMIT_DATA:
+        rlim.rlim_cur = target_dfldsiz;
+        rlim.rlim_max = target_maxdsiz;
+        ret = 0;
+        break;
+
+    default:
+        ret = get_errno(getrlimit(resource, &rlim));
+        break;
+    }
+    if (!is_error(ret)) {
+        if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) {
+            return -TARGET_EFAULT;
+        }
+        target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
+        target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
+        unlock_user_struct(target_rlim, arg2, 1);
+    }
+    return ret;
+}
+
+/* setrlimit(2) */
+static inline abi_long do_bsd_setrlimit(abi_long arg1, abi_ulong arg2)
+{
+    abi_long ret;
+    int resource = target_to_host_resource(arg1);
+    struct target_rlimit *target_rlim;
+    struct rlimit rlim;
+
+    if (RLIMIT_STACK == resource) {
+        /* XXX We should, maybe, allow the stack size to shrink */
+        ret = -TARGET_EPERM;
+    } else {
+        if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) {
+            return -TARGET_EFAULT;
+        }
+        rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
+        rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
+        unlock_user_struct(target_rlim, arg2, 0);
+        ret = get_errno(setrlimit(resource, &rlim));
+    }
+    return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index f7c4a64f9a..5467cb2341 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -244,6 +244,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_getrusage(arg1, arg2);
         break;
 
+    case TARGET_FREEBSD_NR_getrlimit: /* getrlimit(2) */
+        ret = do_bsd_getrlimit(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_setrlimit: /* setrlimit(2) */
+        ret = do_bsd_setrlimit(arg1, arg2);
+        break;
+
 
         /*
          * File system calls.
-- 
2.40.0



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

* [PATCH 16/32] bsd-user: Implement several get/set system calls:
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (14 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 15/32] bsd-user: Implement getrlimit(2) and setrlimit(2) Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:58   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 17/32] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid Karim Taha
                   ` (15 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

getpid(2), getppid(2), getpgrp(2)
setreuid(2), setregid(2)
getuid(2), geteuid(2), getgid(2), getegid(2), getpgid(2)
setuid(2), seteuid(2), setgid(2), setegid(2), setpgid(2)

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 90 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 60 +++++++++++++++++++++++
 2 files changed, 150 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 2c225b12bc..98be0be2b9 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -196,4 +196,94 @@ static inline abi_long do_bsd_setrlimit(abi_long arg1, abi_ulong arg2)
     return ret;
 }
 
+/* getpid(2) */
+static inline abi_long do_bsd_getpid(void)
+{
+    return get_errno(getpid());
+}
+
+/* getppid(2) */
+static inline abi_long do_bsd_getppid(void)
+{
+    return get_errno(getppid());
+}
+
+/* getuid(2) */
+static inline abi_long do_bsd_getuid(void)
+{
+    return get_errno(getuid());
+}
+
+/* geteuid(2) */
+static inline abi_long do_bsd_geteuid(void)
+{
+    return get_errno(geteuid());
+}
+
+/* getgid(2) */
+static inline abi_long do_bsd_getgid(void)
+{
+    return get_errno(getgid());
+}
+
+/* getegid(2) */
+static inline abi_long do_bsd_getegid(void)
+{
+    return get_errno(getegid());
+}
+
+/* setuid(2) */
+static inline abi_long do_bsd_setuid(abi_long arg1)
+{
+    return get_errno(setuid(arg1));
+}
+
+/* seteuid(2) */
+static inline abi_long do_bsd_seteuid(abi_long arg1)
+{
+    return get_errno(seteuid(arg1));
+}
+
+/* setgid(2) */
+static inline abi_long do_bsd_setgid(abi_long arg1)
+{
+    return get_errno(setgid(arg1));
+}
+
+/* setegid(2) */
+static inline abi_long do_bsd_setegid(abi_long arg1)
+{
+    return get_errno(setegid(arg1));
+}
+
+/* getpgid(2) */
+static inline abi_long do_bsd_getpgid(pid_t pid)
+{
+    return get_errno(getpgid(pid));
+}
+
+/* setpgid(2) */
+static inline abi_long do_bsd_setpgid(int pid, int pgrp)
+{
+    return get_errno(setpgid(pid, pgrp));
+}
+
+/* getpgrp(2) */
+static inline abi_long do_bsd_getpgrp(void)
+{
+    return get_errno(getpgrp());
+}
+
+/* setreuid(2) */
+static inline abi_long do_bsd_setreuid(abi_long arg1, abi_long arg2)
+{
+    return get_errno(setreuid(arg1, arg2));
+}
+
+/* setregid(2) */
+static inline abi_long do_bsd_setregid(abi_long arg1, abi_long arg2)
+{
+    return get_errno(setregid(arg1, arg2));
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 5467cb2341..af3aff778d 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -252,6 +252,66 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_setrlimit(arg1, arg2);
         break;
 
+    case TARGET_FREEBSD_NR_getpid: /* getpid(2) */
+        ret = do_bsd_getpid();
+        break;
+
+    case TARGET_FREEBSD_NR_getppid: /* getppid(2) */
+        ret = do_bsd_getppid();
+        break;
+
+    case TARGET_FREEBSD_NR_getuid: /* getuid(2) */
+        ret = do_bsd_getuid();
+        break;
+
+    case TARGET_FREEBSD_NR_geteuid: /* geteuid(2) */
+        ret = do_bsd_geteuid();
+        break;
+
+    case TARGET_FREEBSD_NR_getgid: /* getgid(2) */
+        ret = do_bsd_getgid();
+        break;
+
+    case TARGET_FREEBSD_NR_getegid: /* getegid(2) */
+        ret = do_bsd_getegid();
+        break;
+
+    case TARGET_FREEBSD_NR_setuid: /* setuid(2) */
+        ret = do_bsd_setuid(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_seteuid: /* seteuid(2) */
+        ret = do_bsd_seteuid(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_setgid: /* setgid(2) */
+        ret = do_bsd_setgid(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_setegid: /* setegid(2) */
+        ret = do_bsd_setegid(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_getpgrp: /* getpgrp(2) */
+        ret = do_bsd_getpgrp();
+        break;
+
+    case TARGET_FREEBSD_NR_getpgid: /* getpgid(2) */
+         ret = do_bsd_getpgid(arg1);
+         break;
+
+    case TARGET_FREEBSD_NR_setpgid: /* setpgid(2) */
+         ret = do_bsd_setpgid(arg1, arg2);
+         break;
+
+    case TARGET_FREEBSD_NR_setreuid: /* setreuid(2) */
+        ret = do_bsd_setreuid(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_setregid: /* setregid(2) */
+        ret = do_bsd_setregid(arg1, arg2);
+        break;
+
 
         /*
          * File system calls.
-- 
2.40.0



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

* [PATCH 17/32] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (15 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 16/32] bsd-user: Implement several get/set system calls: Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 19:59   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 18/32] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2) Karim Taha
                   ` (14 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 76 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 28 +++++++++++++
 2 files changed, 104 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 98be0be2b9..5c072d75b7 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -286,4 +286,80 @@ static inline abi_long do_bsd_setregid(abi_long arg1, abi_long arg2)
     return get_errno(setregid(arg1, arg2));
 }
 
+/* setresgid(2) */
+static inline abi_long do_bsd_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
+{
+    return get_errno(setresgid(rgid, egid, sgid));
+}
+
+/* setresuid(2) */
+static inline abi_long do_bsd_setresuid(uid_t ruid, uid_t euid, uid_t suid)
+{
+    return get_errno(setresuid(ruid, euid, suid));
+}
+
+/* getresuid(2) */
+static inline abi_long do_bsd_getresuid(abi_ulong arg1, abi_ulong arg2,
+        abi_ulong arg3)
+{
+    abi_long ret;
+    uid_t ruid, euid, suid;
+
+    ret = get_errno(getresuid(&ruid, &euid, &suid));
+    if (is_error(ret)) {
+            return ret;
+    }
+    if (put_user_s32(ruid, arg1)) {
+        return -TARGET_EFAULT;
+    }
+    if (put_user_s32(euid, arg2)) {
+        return -TARGET_EFAULT;
+    }
+    if (put_user_s32(suid, arg3)) {
+        return -TARGET_EFAULT;
+    }
+    return ret;
+}
+
+/* getresgid(2) */
+static inline abi_long do_bsd_getresgid(abi_ulong arg1, abi_ulong arg2,
+                                        abi_ulong arg3)
+{
+    abi_long ret;
+    uid_t ruid, euid, suid;
+
+    ret = get_errno(getresgid(&ruid, &euid, &suid));
+    if (is_error(ret)) {
+            return ret;
+    }
+    if (put_user_s32(ruid, arg1)) {
+        return -TARGET_EFAULT;
+    }
+    if (put_user_s32(euid, arg2)) {
+        return -TARGET_EFAULT;
+    }
+    if (put_user_s32(suid, arg3)) {
+        return -TARGET_EFAULT;
+    }
+    return ret;
+}
+
+/* getsid(2) */
+static inline abi_long do_bsd_getsid(abi_long arg1)
+{
+    return get_errno(getsid(arg1));
+}
+
+/* setsid(2) */
+static inline abi_long do_bsd_setsid(void)
+{
+    return get_errno(setsid());
+}
+
+/* issetugid(2) */
+static inline abi_long do_bsd_issetugid(void)
+{
+    return get_errno(issetugid());
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index af3aff778d..bbfd260fe0 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -312,6 +312,34 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_setregid(arg1, arg2);
         break;
 
+    case TARGET_FREEBSD_NR_getresuid: /* getresuid(2) */
+        ret = do_bsd_getresuid(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_getresgid: /* getresgid(2) */
+        ret = do_bsd_getresgid(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_setresuid: /* setresuid(2) */
+        ret = do_bsd_setresuid(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_setresgid: /* setresgid(2) */
+        ret = do_bsd_setresgid(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_getsid: /* getsid(2) */
+        ret = do_bsd_getsid(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_setsid: /* setsid(2) */
+        ret = do_bsd_setsid();
+        break;
+
+    case TARGET_FREEBSD_NR_issetugid: /* issetugid(2) */
+        ret = do_bsd_issetugid();
+        break;
+
 
         /*
          * File system calls.
-- 
2.40.0



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

* [PATCH 18/32] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2).
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (16 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 17/32] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:00   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 19/32] bsd-user: Implement getpriority(2) and setpriority(2) Karim Taha
                   ` (13 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 28 ++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 16 ++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 5c072d75b7..c9b5a4cbb6 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -362,4 +362,32 @@ static inline abi_long do_bsd_issetugid(void)
     return get_errno(issetugid());
 }
 
+/* profil(2) */
+static inline abi_long do_bsd_profil(abi_long arg1, abi_long arg2,
+                                     abi_long arg3, abi_long arg4)
+{
+    return -TARGET_ENOSYS;
+}
+
+/* ktrace(2) */
+static inline abi_long do_bsd_ktrace(abi_long arg1, abi_long arg2,
+                                     abi_long arg3, abi_long arg4)
+{
+    return -TARGET_ENOSYS;
+}
+
+/* utrace(2) */
+static inline abi_long do_bsd_utrace(abi_long arg1, abi_long arg2)
+{
+    return -TARGET_ENOSYS;
+}
+
+
+/* ptrace(2) */
+static inline abi_long do_bsd_ptrace(abi_long arg1, abi_long arg2,
+        abi_long arg3, abi_long arg4)
+{
+    return -TARGET_ENOSYS;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index bbfd260fe0..7c5c17e70b 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -340,6 +340,22 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_issetugid();
         break;
 
+    case TARGET_FREEBSD_NR_profil: /* profil(2) */
+        ret = do_bsd_profil(arg1, arg2, arg3, arg4);
+        break;
+
+    case TARGET_FREEBSD_NR_ktrace: /* ktrace(2) */
+        ret = do_bsd_ktrace(arg1, arg2, arg3, arg4);
+        break;
+
+    case TARGET_FREEBSD_NR_utrace: /* utrace(2) */
+        ret = do_bsd_utrace(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_ptrace: /* ptrace(2) */
+        ret = do_bsd_ptrace(arg1, arg2, arg3, arg4);
+        break;
+
 
         /*
          * File system calls.
-- 
2.40.0



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

* [PATCH 19/32] bsd-user: Implement getpriority(2) and setpriority(2).
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (17 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 18/32] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2) Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:10   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 20/32] bsd-user: Add freebsd/os-proc.c to meson.build Karim Taha
                   ` (12 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 27 +++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  8 ++++++++
 2 files changed, 35 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index c9b5a4cbb6..c7769e3560 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -390,4 +390,31 @@ static inline abi_long do_bsd_ptrace(abi_long arg1, abi_long arg2,
     return -TARGET_ENOSYS;
 }
 
+/* getpriority(2) */
+static inline abi_long do_bsd_getpriority(abi_long which, abi_long who)
+{
+    abi_long ret;
+    /*
+     * Note that negative values are valid for getpriority, so we must
+     * differentiate based on errno settings.
+     */
+    errno = 0;
+    ret = getpriority(which, who);
+    if (ret == -1 && errno != 0) {
+        ret = -host_to_target_errno(errno);
+        return ret;
+    }
+    /* Return value is a biased priority to avoid negative numbers. */
+    ret = 20 - ret;
+
+    return ret;
+}
+
+/* setpriority(2) */
+static inline abi_long do_bsd_setpriority(abi_long which, abi_long who,
+                                          abi_long prio)
+{
+    return get_errno(setpriority(which, who, prio));
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7c5c17e70b..0f25187d63 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -356,6 +356,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_ptrace(arg1, arg2, arg3, arg4);
         break;
 
+    case TARGET_FREEBSD_NR_getpriority: /* getpriority(2) */
+        ret = do_bsd_getpriority(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_setpriority: /* setpriority(2) */
+        ret = do_bsd_setpriority(arg1, arg2, arg3);
+        break;
+
 
         /*
          * File system calls.
-- 
2.40.0



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

* [PATCH 20/32] bsd-user: Add freebsd/os-proc.c to meson.build
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (18 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 19/32] bsd-user: Implement getpriority(2) and setpriority(2) Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:12   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 21/32] bsd-user: Implement get_filename_from_fd Karim Taha
                   ` (11 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Karim Taha

Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/meson.build | 1 +
 1 file changed, 1 insertion(+)

diff --git a/bsd-user/freebsd/meson.build b/bsd-user/freebsd/meson.build
index f87c788e84..d169e31235 100644
--- a/bsd-user/freebsd/meson.build
+++ b/bsd-user/freebsd/meson.build
@@ -1,4 +1,5 @@
 bsd_user_ss.add(files(
+  'os-proc.c',
   'os-sys.c',
   'os-syscall.c',
 ))
-- 
2.40.0



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

* [PATCH 21/32] bsd-user: Implement get_filename_from_fd.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (19 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 20/32] bsd-user: Add freebsd/os-proc.c to meson.build Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:17   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 22/32] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve Karim Taha
                   ` (10 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.c | 74 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
 create mode 100644 bsd-user/freebsd/os-proc.c

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
new file mode 100644
index 0000000000..5cd800e607
--- /dev/null
+++ b/bsd-user/freebsd/os-proc.c
@@ -0,0 +1,74 @@
+/*
+ *  FreeBSD process related emulation code
+ *
+ *  Copyright (c) 2013-15 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/>.
+ */
+#include "qemu/osdep.h"
+
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/sysctl.h>
+struct kinfo_proc;
+#include <libprocstat.h>
+
+#include "qemu.h"
+
+/*
+ * Get the filename for the given file descriptor.
+ * Note that this may return NULL (fail) if no longer cached in the kernel.
+ */
+static char *
+get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
+{
+    char *ret = NULL;
+    unsigned int cnt;
+    struct procstat *procstat = NULL;
+    struct kinfo_proc *kp = NULL;
+    struct filestat_list *head = NULL;
+    struct filestat *fst;
+
+    procstat = procstat_open_sysctl();
+    if (procstat == NULL)
+        goto out;
+
+    kp = procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt);
+    if (kp == NULL)
+        goto out;
+
+    head = procstat_getfiles(procstat, kp, 0);
+    if (head == NULL)
+        goto out;
+
+    STAILQ_FOREACH(fst, head, next) {
+        if (fd == fst->fs_fd) {
+            if (fst->fs_path != NULL) {
+                (void)strlcpy(filename, fst->fs_path, len);
+                ret = filename;
+            }
+            break;
+        }
+    }
+
+out:
+    if (head != NULL)
+        procstat_freefiles(procstat, head);
+    if (kp != NULL)
+        procstat_freeprocs(procstat, kp);
+    if (procstat != NULL)
+        procstat_close(procstat);
+    return ret;
+}
+
-- 
2.40.0



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

* [PATCH 22/32] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (20 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 21/32] bsd-user: Implement get_filename_from_fd Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:28   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 23/32] bsd-user: Implement t2h procctl control request commands and h2t reaper status struct conversion Karim Taha
                   ` (9 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.c | 177 +++++++++++++++++++++++++++++++++++++
 1 file changed, 177 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index 5cd800e607..396f258a64 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -72,3 +72,180 @@ out:
     return ret;
 }
 
+/*
+ * execve/fexecve
+ */
+abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
+        abi_ulong guest_envp, int do_fexec)
+{
+    char **argp, **envp, **qargp, **qarg1, **qarg0, **qargend;
+    int argc, envc;
+    abi_ulong gp;
+    abi_ulong addr;
+    char **q;
+    int total_size = 0;
+    void *p;
+    abi_long ret;
+
+    argc = 0;
+    for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
+        if (get_user_ual(addr, gp)) {
+            return -TARGET_EFAULT;
+        }
+        if (!addr) {
+            break;
+        }
+        argc++;
+    }
+    envc = 0;
+    for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
+        if (get_user_ual(addr, gp)) {
+            return -TARGET_EFAULT;
+        }
+        if (!addr) {
+            break;
+        }
+        envc++;
+    }
+
+    qarg0 = argp = g_new0(char *, argc + 9);
+    /* save the first agrument for the emulator */
+    *argp++ = (char *)getprogname();
+    qargp = argp;
+    *argp++ = (char *)getprogname();
+    qarg1 = argp;
+    envp = g_new0(char *, envc + 1);
+    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+        if (get_user_ual(addr, gp)) {
+            ret = -TARGET_EFAULT;
+            goto execve_end;
+        }
+        if (!addr) {
+            break;
+        }
+        *q = lock_user_string(addr);
+        if (*q == NULL) {
+            ret = -TARGET_EFAULT;
+            goto execve_end;
+        }
+        total_size += strlen(*q) + 1;
+    }
+    *q++ = NULL;
+    qargend = q;
+
+    for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
+        if (get_user_ual(addr, gp)) {
+            ret = -TARGET_EFAULT;
+            goto execve_end;
+        }
+        if (!addr) {
+            break;
+        }
+        *q = lock_user_string(addr);
+        if (*q == NULL) {
+            ret = -TARGET_EFAULT;
+            goto execve_end;
+        }
+        total_size += strlen(*q) + 1;
+    }
+    *q = NULL;
+
+    /*
+     * This case will not be caught by the host's execve() if its
+     * page size is bigger than the target's.
+     */
+    if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
+        ret = -TARGET_E2BIG;
+        goto execve_end;
+    }
+
+    if (do_fexec) {
+        if (((int)path_or_fd > 0 &&
+            is_target_elf_binary((int)path_or_fd)) == 1) {
+            char execpath[PATH_MAX];
+
+            /*
+             * The executable is an elf binary for the target
+             * arch.  execve() it using the emulator if we can
+             * determine the filename path from the fd.
+             */
+            if (get_filename_from_fd(getpid(), (int)path_or_fd, execpath,
+                        sizeof(execpath)) != NULL) {
+                memmove(qarg1 + 2, qarg1, (qargend-qarg1) * sizeof(*qarg1));
+		qarg1[1] = qarg1[0];
+		qarg1[0] = (char *)"-0";
+		qarg1 += 2;
+		qargend += 2;
+                *qarg1 = execpath;
+#ifndef DONT_INHERIT_INTERP_PREFIX
+                memmove(qarg1 + 2, qarg1, (qargend-qarg1) * sizeof(*qarg1));
+                *qarg1++ = (char *)"-L";
+                *qarg1++ = (char *)interp_prefix;
+#endif
+                ret = get_errno(execve(qemu_proc_pathname, qargp, envp));
+            } else {
+                /* Getting the filename path failed. */
+                ret = -TARGET_EBADF;
+                goto execve_end;
+            }
+        } else {
+            ret = get_errno(fexecve((int)path_or_fd, argp, envp));
+        }
+    } else {
+        int fd;
+
+        p = lock_user_string(path_or_fd);
+        if (p == NULL) {
+            ret = -TARGET_EFAULT;
+            goto execve_end;
+        }
+
+        /*
+         * Check the header and see if it a target elf binary.  If so
+         * then execute using qemu user mode emulator.
+         */
+        fd = open(p, O_RDONLY | O_CLOEXEC);
+        if (fd > 0 && is_target_elf_binary(fd) == 1) {
+            close(fd);
+            /* execve() as a target binary using emulator. */
+            memmove(qarg1 + 2, qarg1, (qargend-qarg1) * sizeof(*qarg1));
+            qarg1[1] = qarg1[0];
+            qarg1[0] = (char *)"-0";
+            qarg1 += 2;
+	    qargend += 2;
+            *qarg1 = (char *)p;
+#ifndef DONT_INHERIT_INTERP_PREFIX
+            memmove(qarg1 + 2, qarg1, (qargend-qarg1) * sizeof(*qarg1));
+            *qarg1++ = (char *)"-L";
+            *qarg1++ = (char *)interp_prefix;
+#endif
+            ret = get_errno(execve(qemu_proc_pathname, qargp, envp));
+        } else {
+            close(fd);
+            /* Execve() as a host native binary. */
+            ret = get_errno(execve(p, argp, envp));
+        }
+        unlock_user(p, path_or_fd, 0);
+    }
+
+execve_end:
+    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
+        if (get_user_ual(addr, gp) || !addr) {
+            break;
+        }
+        unlock_user(*q, addr, 0);
+    }
+
+    for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
+        if (get_user_ual(addr, gp) || !addr) {
+            break;
+        }
+        unlock_user(*q, addr, 0);
+    }
+
+    g_free(qarg0);
+    g_free(envp);
+
+    return ret;
+}
+
-- 
2.40.0



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

* [PATCH 23/32] bsd-user: Implement t2h procctl control request commands and h2t reaper status struct conversion.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (21 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 22/32] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:29   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 24/32] bsd-user: Implement h2t reaper_pidinfo and h2t/t2h reaper_kill structs conversion functions Karim Taha
                   ` (8 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.c | 52 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index 396f258a64..f069472156 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -249,3 +249,55 @@ execve_end:
     return ret;
 }
 
+#include <sys/procctl.h>
+
+static abi_long
+t2h_procctl_cmd(int target_cmd, int *host_cmd)
+{
+
+    switch(target_cmd) {
+    case TARGET_PROC_SPROTECT:
+        *host_cmd = PROC_SPROTECT;
+        break;
+
+    case TARGET_PROC_REAP_ACQUIRE:
+        *host_cmd = PROC_REAP_ACQUIRE;
+        break;
+
+    case TARGET_PROC_REAP_RELEASE:
+        *host_cmd = PROC_REAP_RELEASE;
+        break;
+
+    case TARGET_PROC_REAP_STATUS:
+        *host_cmd = PROC_REAP_STATUS;
+        break;
+
+    case TARGET_PROC_REAP_KILL:
+        *host_cmd = PROC_REAP_KILL;
+        break;
+
+    default:
+        return (-TARGET_EINVAL);
+    }
+
+    return 0;
+}
+
+static abi_long
+h2t_reaper_status(struct procctl_reaper_status *host_rs,
+        abi_ulong target_rs_addr)
+{
+    struct target_procctl_reaper_status *target_rs;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_rs, target_rs_addr, 0))
+        return -TARGET_EFAULT;
+    __put_user(host_rs->rs_flags, &target_rs->rs_flags);
+    __put_user(host_rs->rs_children, &target_rs->rs_children);
+    __put_user(host_rs->rs_descendants, &target_rs->rs_descendants);
+    __put_user(host_rs->rs_reaper, &target_rs->rs_reaper);
+    __put_user(host_rs->rs_pid, &target_rs->rs_pid);
+    unlock_user_struct(target_rs, target_rs_addr, 1);
+
+    return 0;
+}
+
-- 
2.40.0



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

* [PATCH 24/32] bsd-user: Implement h2t reaper_pidinfo and h2t/t2h reaper_kill structs conversion functions.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (22 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 23/32] bsd-user: Implement t2h procctl control request commands and h2t reaper status struct conversion Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:29   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 25/32] bsd-user: Implement procctl(2) system call Karim Taha
                   ` (7 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.c | 50 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index f069472156..a413109bc2 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -301,3 +301,53 @@ h2t_reaper_status(struct procctl_reaper_status *host_rs,
     return 0;
 }
 
+static abi_long
+t2h_reaper_kill(abi_ulong target_rk_addr, struct procctl_reaper_kill *host_rk)
+{
+    struct target_procctl_reaper_kill *target_rk;
+
+    if (!lock_user_struct(VERIFY_READ, target_rk, target_rk_addr, 1))
+        return -TARGET_EFAULT;
+    __get_user(host_rk->rk_sig, &target_rk->rk_sig);
+    __get_user(host_rk->rk_flags, &target_rk->rk_flags);
+    __get_user(host_rk->rk_subtree, &target_rk->rk_subtree);
+    __get_user(host_rk->rk_killed, &target_rk->rk_killed);
+    __get_user(host_rk->rk_fpid, &target_rk->rk_fpid);
+    unlock_user_struct(target_rk, target_rk_addr, 0);
+
+    return 0;
+}
+
+static abi_long
+h2t_reaper_kill(struct procctl_reaper_kill *host_rk, abi_ulong target_rk_addr)
+{
+    struct target_procctl_reaper_kill *target_rk;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_rk, target_rk_addr, 0))
+        return -TARGET_EFAULT;
+    __put_user(host_rk->rk_sig, &target_rk->rk_sig);
+    __put_user(host_rk->rk_flags, &target_rk->rk_flags);
+    __put_user(host_rk->rk_subtree, &target_rk->rk_subtree);
+    __put_user(host_rk->rk_killed, &target_rk->rk_killed);
+    __put_user(host_rk->rk_fpid, &target_rk->rk_fpid);
+    unlock_user_struct(target_rk, target_rk_addr, 1);
+
+    return 0;
+}
+
+static abi_long
+h2t_procctl_reaper_pidinfo(struct procctl_reaper_pidinfo *host_pi,
+        abi_ulong target_pi_addr)
+{
+    struct target_procctl_reaper_pidinfo *target_pi;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_pi, target_pi_addr, 0))
+        return -TARGET_EFAULT;
+    __put_user(host_pi->pi_pid, &target_pi->pi_pid);
+    __put_user(host_pi->pi_subtree, &target_pi->pi_subtree);
+    __put_user(host_pi->pi_flags, &target_pi->pi_flags);
+    unlock_user_struct(target_pi, target_pi_addr, 1);
+
+    return 0;
+}
+
-- 
2.40.0



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

* [PATCH 25/32] bsd-user: Implement procctl(2) system call.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (23 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 24/32] bsd-user: Implement h2t reaper_pidinfo and h2t/t2h reaper_kill structs conversion functions Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:30   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 26/32] bsd-user: Implement execve(2) and fexecve(2) system calls Karim Taha
                   ` (6 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.c    | 114 ++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |   3 +
 2 files changed, 117 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index a413109bc2..3e6903d8cd 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -351,3 +351,117 @@ h2t_procctl_reaper_pidinfo(struct procctl_reaper_pidinfo *host_pi,
     return 0;
 }
 
+abi_long
+do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2, abi_ulong arg3,
+       abi_ulong arg4, abi_ulong arg5, abi_ulong arg6)
+{
+    abi_long error = 0, target_rp_pids;
+    void *data;
+    int host_cmd, flags;
+    uint32_t u, target_rp_count;
+    union {
+        struct procctl_reaper_status rs;
+        struct procctl_reaper_pids rp;
+        struct procctl_reaper_kill rk;
+    } host;
+    struct target_procctl_reaper_pids *target_rp;
+    id_t id; /* 64-bit */
+    int target_cmd;
+    abi_ulong target_arg;
+
+#if TARGET_ABI_BITS == 32
+    /* See if we need to align the register pairs. */
+    if (regpairs_aligned(cpu_env)) {
+        id = (id_t)target_arg64(arg3, arg4);
+        target_cmd = (int)arg5;
+        target_arg = arg6;
+    } else {
+        id = (id_t)target_arg64(arg2, arg3);
+        target_cmd = (int)arg4;
+        target_arg = arg5;
+    }
+#else
+    id = (id_t)arg2;
+    target_cmd = (int)arg3;
+    target_arg = arg4;
+#endif
+
+    error = t2h_procctl_cmd(target_cmd, &host_cmd);
+    if (error)
+        return error;
+
+    switch (host_cmd) {
+    case PROC_SPROTECT:
+        data = &flags;
+        break;
+
+    case PROC_REAP_ACQUIRE:
+    case PROC_REAP_RELEASE:
+        if (target_arg == 0)
+            data = NULL;
+        else
+            error = -TARGET_EINVAL;
+        break;
+
+    case PROC_REAP_STATUS:
+        data = &host.rs;
+        break;
+
+    case PROC_REAP_GETPIDS:
+        if (!lock_user_struct(VERIFY_READ, target_rp, target_arg, 1)) {
+            return -TARGET_EFAULT;
+        }
+        __get_user(target_rp_count, &target_rp->rp_count);
+        __get_user(target_rp_pids, &target_rp->rp_pids);
+        unlock_user_struct(target_rp, target_arg, 0);
+        host.rp.rp_count = target_rp_count;
+        /* XXX we should check target_rc_count to see if it is reasonable. */
+        host.rp.rp_pids = alloca(target_rp_count *
+                sizeof(struct procctl_reaper_pidinfo));
+        if (host.rp.rp_pids == NULL)
+            error = -TARGET_ENOMEM;
+        else
+            data = &host.rp;
+        break;
+
+    case PROC_REAP_KILL:
+        error = t2h_reaper_kill(target_arg, &host.rk);
+        break;
+    }
+
+    if (error)
+        return error;
+
+    error = get_errno(procctl(idtype, id, host_cmd, data));
+
+    if (error)
+        return error;
+
+    switch(host_cmd) {
+    case PROC_SPROTECT:
+        if (put_user_s32(flags, target_arg))
+            return -TARGET_EFAULT;
+        break;
+
+    case PROC_REAP_STATUS:
+        error = h2t_reaper_status(&host.rs, target_arg);
+        break;
+
+    case PROC_REAP_GETPIDS:
+        /* copyout reaper pidinfo */
+        for (u = 0; u < target_rp_count; u++) {
+            error = h2t_procctl_reaper_pidinfo(&host.rp.rp_pids[u],
+                    target_rp_pids +
+                    (u * sizeof(struct target_procctl_reaper_pidinfo)));
+            if (error)
+                break;
+        }
+        break;
+
+    case PROC_REAP_KILL:
+        error = h2t_reaper_kill(&host.rk, target_arg);
+        break;
+    }
+
+    return error;
+}
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 0f25187d63..43f4561301 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -364,6 +364,9 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_setpriority(arg1, arg2, arg3);
         break;
 
+    case TARGET_FREEBSD_NR_procctl: /* procctl(2) */
+        ret = do_freebsd_procctl(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
+        break;
 
         /*
          * File system calls.
-- 
2.40.0



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

* [PATCH 26/32] bsd-user: Implement execve(2) and fexecve(2) system calls.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (24 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 25/32] bsd-user: Implement procctl(2) system call Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:31   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 27/32] bsd-user: Implement wait4(2) and wait6(2) " Karim Taha
                   ` (5 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.h    | 49 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 10 +++++++
 2 files changed, 59 insertions(+)
 create mode 100644 bsd-user/freebsd/os-proc.h

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
new file mode 100644
index 0000000000..75ed39f8dd
--- /dev/null
+++ b/bsd-user/freebsd/os-proc.h
@@ -0,0 +1,49 @@
+/*
+ *  process related system call shims and definitions
+ *
+ *  Copyright (c) 2013-14 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_USER_FREEBSD_OS_PROC_H
+#define BSD_USER_FREEBSD_OS_PROC_H
+
+#include <sys/param.h>
+#include <sys/procctl.h>
+#include <sys/signal.h>
+#include <sys/types.h>
+#include <sys/procdesc.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "target_arch_cpu.h"
+
+/* execve(2) */
+static inline abi_long do_freebsd_execve(abi_ulong path_or_fd, abi_ulong argp,
+        abi_ulong envp)
+{
+
+    return freebsd_exec_common(path_or_fd, argp, envp, 0);
+}
+
+/* fexecve(2) */
+static inline abi_long do_freebsd_fexecve(abi_ulong path_or_fd, abi_ulong argp,
+        abi_ulong envp)
+{
+
+    return freebsd_exec_common(path_or_fd, argp, envp, 1);
+}
+
+#endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 43f4561301..3f3ca96752 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -36,6 +36,8 @@
 #include "bsd-file.h"
 #include "bsd-proc.h"
 
+#include "os-proc.h"
+
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
 safe_syscall4(int, openat, int, fd, const char *, path, int, flags, mode_t,
@@ -216,6 +218,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         /*
          * process system calls
          */
+    case TARGET_FREEBSD_NR_execve: /* execve(2) */
+        ret = do_freebsd_execve(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_fexecve: /* fexecve(2) */
+        ret = do_freebsd_fexecve(arg1, arg2, arg3);
+        break;
+
     case TARGET_FREEBSD_NR_exit: /* exit(2) */
         ret = do_bsd_exit(cpu_env, arg1);
         break;
-- 
2.40.0



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

* [PATCH 27/32] bsd-user: Implement wait4(2) and wait6(2) system calls.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (25 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 26/32] bsd-user: Implement execve(2) and fexecve(2) system calls Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:33   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 28/32] bsd-user: Implement setloginclass(2) and getloginclass(2) " Karim Taha
                   ` (4 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.h    | 75 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 14 +++++++
 2 files changed, 89 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 75ed39f8dd..544e45b3ef 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -30,6 +30,10 @@
 
 #include "target_arch_cpu.h"
 
+pid_t safe_wait4(pid_t wpid, int *status, int options, struct rusage *rusage);
+pid_t safe_wait6(idtype_t idtype, id_t id, int *status, int options,
+    struct __wrusage *wrusage, siginfo_t *infop);
+
 /* execve(2) */
 static inline abi_long do_freebsd_execve(abi_ulong path_or_fd, abi_ulong argp,
         abi_ulong envp)
@@ -46,4 +50,75 @@ static inline abi_long do_freebsd_fexecve(abi_ulong path_or_fd, abi_ulong argp,
     return freebsd_exec_common(path_or_fd, argp, envp, 1);
 }
 
+/* wait4(2) */
+static inline abi_long do_freebsd_wait4(abi_long arg1, abi_ulong target_status,
+        abi_long arg3, abi_ulong target_rusage)
+{
+    abi_long ret;
+    int status;
+    struct rusage rusage, *rusage_ptr = NULL;
+
+    if (target_rusage) {
+        rusage_ptr = &rusage;
+    }
+    ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
+    if (target_status != 0) {
+        status = host_to_target_waitstatus(status);
+        if (put_user_s32(status, target_status) != 0) {
+            return -TARGET_EFAULT;
+        }
+    }
+    if (target_rusage != 0) {
+        host_to_target_rusage(target_rusage, &rusage);
+    }
+    return ret;
+}
+
+/* wait6(2) */
+static inline abi_long do_freebsd_wait6(void *cpu_env, abi_long idtype, 
+    abi_long id1, abi_long id2,
+    abi_ulong target_status, abi_long options, abi_ulong target_wrusage,
+	abi_ulong target_infop, abi_ulong pad1)
+{
+    abi_long ret;
+    int status;
+    struct __wrusage wrusage, *wrusage_ptr = NULL;
+    siginfo_t info;
+    void *p;
+
+    if (regpairs_aligned(cpu_env) != 0) {
+		/* printf("shifting args\n"); */
+		/* 64-bit id is aligned, so shift all the arguments over by one */
+		id1 = id2;
+		id2 = target_status;
+		target_status = options;
+		options = target_wrusage;
+		target_wrusage = target_infop;
+		target_infop = pad1;
+    }
+
+    if (target_wrusage) {
+        wrusage_ptr = &wrusage;
+    }
+    ret = get_errno(safe_wait6(idtype, target_arg64(id1, id2), &status, options, wrusage_ptr, &info));
+    if (target_status != 0) {
+        status = host_to_target_waitstatus(status);
+        if (put_user_s32(status, target_status) != 0) {
+            return -TARGET_EFAULT;
+        }
+    }
+    if (target_wrusage != 0) {
+        host_to_target_wrusage(target_wrusage, &wrusage);
+    }
+    if (target_infop != 0) {
+        p = lock_user(VERIFY_WRITE, target_infop, sizeof(target_siginfo_t), 0);
+        if (p == NULL) {
+            return -TARGET_EFAULT;
+        }
+        host_to_target_siginfo(p, &info);
+        unlock_user(p, target_infop, sizeof(target_siginfo_t));
+    }
+    return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 3f3ca96752..2775f89304 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -38,6 +38,12 @@
 
 #include "os-proc.h"
 
+/* used in os-proc */
+safe_syscall4(pid_t, wait4, pid_t, wpid, int *, status, int, options,
+    struct rusage *, rusage);
+safe_syscall6(pid_t, wait6, idtype_t, idtype, id_t, id, int *, status, int,
+    options, struct __wrusage *, wrusage, siginfo_t *, infop);
+
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
 safe_syscall4(int, openat, int, fd, const char *, path, int, flags, mode_t,
@@ -226,6 +232,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd_fexecve(arg1, arg2, arg3);
         break;
 
+    case TARGET_FREEBSD_NR_wait4: /* wait4(2) */
+        ret = do_freebsd_wait4(arg1, arg2, arg3, arg4);
+        break;
+
+    case TARGET_FREEBSD_NR_wait6: /* wait6(2) */
+        ret = do_freebsd_wait6(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
+        break;
+
     case TARGET_FREEBSD_NR_exit: /* exit(2) */
         ret = do_bsd_exit(cpu_env, arg1);
         break;
-- 
2.40.0



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

* [PATCH 28/32] bsd-user: Implement setloginclass(2) and getloginclass(2) system calls.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (26 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 27/32] bsd-user: Implement wait4(2) and wait6(2) " Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:33   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 29/32] bsd-user: Implement pdgetpid(2) and the undocumented setugid Karim Taha
                   ` (3 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.h    | 32 ++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  8 ++++++++
 2 files changed, 40 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 544e45b3ef..7d26d09148 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -121,4 +121,36 @@ static inline abi_long do_freebsd_wait6(void *cpu_env, abi_long idtype,
     return ret;
 }
 
+/* setloginclass(2) */
+static inline abi_long do_freebsd_setloginclass(abi_ulong arg1)
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user_string(arg1);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(setloginclass(p));
+    unlock_user(p, arg1, 0);
+
+    return ret;
+}
+
+/* getloginclass(2) */
+static inline abi_long do_freebsd_getloginclass(abi_ulong arg1, abi_ulong arg2)
+{
+    abi_long ret;
+    void *p;
+
+    p = lock_user_string(arg1);
+    if (p == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(getloginclass(p, arg2));
+    unlock_user(p, arg1, 0);
+
+    return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 2775f89304..63e6c6d478 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -372,6 +372,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_ktrace(arg1, arg2, arg3, arg4);
         break;
 
+    case TARGET_FREEBSD_NR_setloginclass: /* setloginclass(2) */
+        ret = do_freebsd_setloginclass(arg1);
+        break;
+
+    case TARGET_FREEBSD_NR_getloginclass: /* getloginclass(2) */
+        ret = do_freebsd_getloginclass(arg1, arg2);
+        break;
+
     case TARGET_FREEBSD_NR_utrace: /* utrace(2) */
         ret = do_bsd_utrace(arg1, arg2);
         break;
-- 
2.40.0



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

* [PATCH 29/32] bsd-user: Implement pdgetpid(2) and the undocumented setugid.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (27 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 28/32] bsd-user: Implement setloginclass(2) and getloginclass(2) " Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:36   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 30/32] bsd-user: Implement fork(2) and vfork(2) system calls Karim Taha
                   ` (2 subsequent siblings)
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.h    | 23 +++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  8 ++++++++
 2 files changed, 31 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 7d26d09148..bfd72c726c 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -34,6 +34,8 @@ pid_t safe_wait4(pid_t wpid, int *status, int options, struct rusage *rusage);
 pid_t safe_wait6(idtype_t idtype, id_t id, int *status, int options,
     struct __wrusage *wrusage, siginfo_t *infop);
 
+extern int __setugid(int flag);
+
 /* execve(2) */
 static inline abi_long do_freebsd_execve(abi_ulong path_or_fd, abi_ulong argp,
         abi_ulong envp)
@@ -153,4 +155,25 @@ static inline abi_long do_freebsd_getloginclass(abi_ulong arg1, abi_ulong arg2)
     return ret;
 }
 
+/* pdgetpid(2) */
+static inline abi_long do_freebsd_pdgetpid(abi_long fd, abi_ulong target_pidp)
+{
+    abi_long ret;
+    pid_t pid;
+
+    ret = get_errno(pdgetpid(fd, &pid));
+    if (!is_error(ret)) {
+        if (put_user_u32(pid, target_pidp)) {
+            return -TARGET_EFAULT;
+        }
+    }
+    return ret;
+}
+
+/* undocumented __setugid */
+static inline abi_long do_freebsd___setugid(abi_long arg1)
+{
+    return get_errno(__setugid(arg1));
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 63e6c6d478..52be71546a 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -380,6 +380,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd_getloginclass(arg1, arg2);
         break;
 
+    case TARGET_FREEBSD_NR_pdgetpid: /* pdgetpid(2) */
+        ret = do_freebsd_pdgetpid(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR___setugid: /* undocumented */
+        ret = do_freebsd___setugid(arg1);
+        break;
+
     case TARGET_FREEBSD_NR_utrace: /* utrace(2) */
         ret = do_bsd_utrace(arg1, arg2);
         break;
-- 
2.40.0



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

* [PATCH 30/32] bsd-user: Implement fork(2) and vfork(2) system calls.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (28 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 29/32] bsd-user: Implement pdgetpid(2) and the undocumented setugid Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:39   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 31/32] bsd-user: Implement rfork(2) system call Karim Taha
  2023-08-27 15:57 ` [PATCH 32/32] bsd-user: Implement pdfork(2) " Karim Taha
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.h    | 34 ++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  8 ++++++++
 2 files changed, 42 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index bfd72c726c..385fc15adf 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -176,4 +176,38 @@ static inline abi_long do_freebsd___setugid(abi_long arg1)
     return get_errno(__setugid(arg1));
 }
 
+/* fork(2) */
+static inline abi_long do_freebsd_fork(void *cpu_env)
+{
+    abi_long ret;
+    abi_ulong child_flag;
+
+    fork_start();
+    ret = fork();
+    if (ret == 0) {
+        /* child */
+        child_flag = 1;
+        target_cpu_clone_regs(cpu_env, 0);
+    } else {
+        /* parent */
+        child_flag = 0;
+    }
+
+    /*
+     * The fork system call sets a child flag in the second return
+     * value: 0 for parent process, 1 for child process.
+     */
+    set_second_rval(cpu_env, child_flag);
+
+    fork_end(child_flag);
+
+    return ret;
+}
+
+/* vfork(2) */
+static inline abi_long do_freebsd_vfork(void *cpu_env)
+{
+    return do_freebsd_fork(cpu_env);
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 52be71546a..84c9bffff5 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -224,6 +224,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         /*
          * process system calls
          */
+    case TARGET_FREEBSD_NR_fork: /* fork(2) */
+        ret = do_freebsd_fork(cpu_env);
+        break;
+
+    case TARGET_FREEBSD_NR_vfork: /* vfork(2) */
+        ret = do_freebsd_vfork(cpu_env);
+        break;
+
     case TARGET_FREEBSD_NR_execve: /* execve(2) */
         ret = do_freebsd_execve(arg1, arg2, arg3);
         break;
-- 
2.40.0



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

* [PATCH 31/32] bsd-user: Implement rfork(2) system call.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (29 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 30/32] bsd-user: Implement fork(2) and vfork(2) system calls Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:43   ` Richard Henderson
  2023-08-27 15:57 ` [PATCH 32/32] bsd-user: Implement pdfork(2) " Karim Taha
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.h    | 38 +++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  4 ++++
 2 files changed, 42 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 385fc15adf..94824d737a 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -210,4 +210,42 @@ static inline abi_long do_freebsd_vfork(void *cpu_env)
     return do_freebsd_fork(cpu_env);
 }
 
+/* rfork(2) */
+static inline abi_long do_freebsd_rfork(void *cpu_env, abi_long flags)
+{
+    abi_long ret;
+    abi_ulong child_flag;
+
+    /*
+     * XXX We need to handle RFMEM here, as well.  Neither are safe to execute
+     * as-is on x86 hosts because they'll split memory but not the stack,
+     * wreaking havoc on host architectures that use the stack to store the
+     * return address as both threads try to pop it off.  Rejecting RFSPAWN
+     * entirely for now is ok, the only consumer at the moment is posix_spawn
+     * and it will fall back to classic vfork(2) if we return EINVAL.
+     */
+    if ((flags & TARGET_RFSPAWN) != 0)
+        return -TARGET_EINVAL;
+    fork_start();
+    ret = rfork(flags);
+    if (ret == 0) {
+        /* child */
+        child_flag = 1;
+        target_cpu_clone_regs(cpu_env, 0);
+    } else {
+        /* parent */
+        child_flag = 0;
+    }
+
+    /*
+     * The fork system call sets a child flag in the second return
+     * value: 0 for parent process, 1 for child process.
+     */
+    set_second_rval(cpu_env, child_flag);
+    fork_end(child_flag);
+
+    return ret;
+
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 84c9bffff5..4464b3369c 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -232,6 +232,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd_vfork(cpu_env);
         break;
 
+    case TARGET_FREEBSD_NR_rfork: /* rfork(2) */
+        ret = do_freebsd_rfork(cpu_env, arg1);
+        break;
+
     case TARGET_FREEBSD_NR_execve: /* execve(2) */
         ret = do_freebsd_execve(arg1, arg2, arg3);
         break;
-- 
2.40.0



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

* [PATCH 32/32] bsd-user: Implement pdfork(2) system call.
  2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
                   ` (30 preceding siblings ...)
  2023-08-27 15:57 ` [PATCH 31/32] bsd-user: Implement rfork(2) system call Karim Taha
@ 2023-08-27 15:57 ` Karim Taha
  2023-08-29 20:58   ` Richard Henderson
  31 siblings, 1 reply; 73+ messages in thread
From: Karim Taha @ 2023-08-27 15:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-proc.h    | 32 ++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  4 ++++
 2 files changed, 36 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 94824d737a..1eaba908a5 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -248,4 +248,36 @@ static inline abi_long do_freebsd_rfork(void *cpu_env, abi_long flags)
 
 }
 
+/* pdfork(2) */
+static inline abi_long do_freebsd_pdfork(void *cpu_env, abi_ulong target_fdp,
+        abi_long flags)
+{
+    abi_long ret;
+    abi_ulong child_flag;
+    int fd;
+
+    fork_start();
+    ret = pdfork(&fd, flags);
+    if (ret == 0) {
+        /* child */
+        child_flag = 1;
+        target_cpu_clone_regs(cpu_env, 0);
+    } else {
+        /* parent */
+        child_flag = 0;
+    }
+    if (put_user_s32(fd, target_fdp)) {
+        return -TARGET_EFAULT;
+    }
+
+    /*
+     * The fork system call sets a child flag in the second return
+     * value: 0 for parent process, 1 for child process.
+     */
+    set_second_rval(cpu_env, child_flag);
+    fork_end(child_flag);
+
+    return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 4464b3369c..27fc9d21fb 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -236,6 +236,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd_rfork(cpu_env, arg1);
         break;
 
+    case TARGET_FREEBSD_NR_pdfork: /* pdfork(2) */
+        ret = do_freebsd_pdfork(cpu_env, arg1, arg2);
+        break;
+
     case TARGET_FREEBSD_NR_execve: /* execve(2) */
         ret = do_freebsd_execve(arg1, arg2, arg3);
         break;
-- 
2.40.0



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

* Re: [PATCH 01/32] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics.
  2023-08-27 15:57 ` [PATCH 01/32] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics Karim Taha
@ 2023-08-29 19:07   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:07 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Kyle Evans

On 8/27/23 08:57, Karim Taha wrote:
> From: Kyle Evans<kevans@FreeBSD.org>
> 
> Signed-off-by: Kyle Evans<kevans@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/syscall_defs.h | 4 ++++
>   1 file changed, 4 insertions(+)

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


r~


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

* Re: [PATCH 02/32] bsd-user: Implement procctl(2) system call.
  2023-08-27 15:57 ` [PATCH 02/32] bsd-user: Implement procctl(2) system call Karim Taha
@ 2023-08-29 19:10   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:10 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Implement procctl flags and related structs:
> struct target_procctl_reaper_status
> struct target_procctl_reaper_pidinfo
> struct target_procctl_reaper_pids
> struct target_procctl_reaper_kill
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/syscall_defs.h | 42 +++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 42 insertions(+)

Fix subject, to define structures not implement a syscall.

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


r~


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

* Re: [PATCH 03/32] bsd-user: Implement host_to_target_siginfo.
  2023-08-27 15:57 ` [PATCH 03/32] bsd-user: Implement host_to_target_siginfo Karim Taha
@ 2023-08-29 19:13   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:13 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Used in wait6 system call
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/signal-common.h | 1 +
>   bsd-user/signal.c        | 6 ++++++
>   2 files changed, 7 insertions(+)

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

r~


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

* Re: [PATCH 04/32] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h.
  2023-08-27 15:57 ` [PATCH 04/32] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h Karim Taha
@ 2023-08-29 19:14   ` Richard Henderson
  2023-09-11 20:58     ` Karim Taha
  0 siblings, 1 reply; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:14 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/main.c | 2 +-
>   bsd-user/qemu.h | 7 +++++++
>   2 files changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/bsd-user/main.c b/bsd-user/main.c
> index 381bb18df8..b94b2d34b6 100644
> --- a/bsd-user/main.c
> +++ b/bsd-user/main.c
> @@ -88,7 +88,7 @@ unsigned long reserved_va = MAX_RESERVED_VA;
>   unsigned long reserved_va;
>   #endif
>   
> -static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
> +const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
>   const char *qemu_uname_release;
>   char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
>   

Adding interp_prefix is unrelated.

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


r~

> diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
> index 6724bb9f0a..23bbdd3e0c 100644
> --- a/bsd-user/qemu.h
> +++ b/bsd-user/qemu.h
> @@ -113,6 +113,7 @@ typedef struct TaskState {
>   } __attribute__((aligned(16))) TaskState;
>   
>   void stop_all_tasks(void);
> +extern const char *interp_prefix;
>   extern const char *qemu_uname_release;
>   
>   /*
> @@ -251,6 +252,12 @@ abi_long get_errno(abi_long ret);
>   bool is_error(abi_long ret);
>   int host_to_target_errno(int err);
>   
> +/* os-proc.c */
> +abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
> +        abi_ulong guest_envp, int do_fexec);
> +abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
> +        abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
> +
>   /* os-sys.c */
>   abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
>           abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);



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

* Re: [PATCH 05/32] bsd-user: add extern declarations for bsd-proc.c conversion functions
  2023-08-27 15:57 ` [PATCH 05/32] bsd-user: add extern declarations for bsd-proc.c conversion functions Karim Taha
@ 2023-08-29 19:15   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:15 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/qemu-bsd.h | 38 ++++++++++++++++++++++++++++++++++++++
>   1 file changed, 38 insertions(+)
>   create mode 100644 bsd-user/qemu-bsd.h

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

r~


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

* Re: [PATCH 06/32] bsd-user: Add bsd-proc.c to meson.build
  2023-08-27 15:57 ` [PATCH 06/32] bsd-user: Add bsd-proc.c to meson.build Karim Taha
@ 2023-08-29 19:17   ` Richard Henderson
  2023-09-11 22:45     ` Karim Taha
  0 siblings, 1 reply; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:17 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp

On 8/27/23 08:57, Karim Taha wrote:
> From: Warner Losh <imp@bsdimp.com>
> 
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.h  | 4 ++++
>   bsd-user/meson.build | 6 ++++++
>   2 files changed, 10 insertions(+)
> 
> diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
> index a1061bffb8..048773a75d 100644
> --- a/bsd-user/bsd-proc.h
> +++ b/bsd-user/bsd-proc.h
> @@ -22,6 +22,10 @@
>   
>   #include <sys/resource.h>
>   
> +#include "qemu-bsd.h"
> +#include "gdbstub/syscalls.h"
> +#include "qemu/plugin.h"
> +
>   /* exit(2) */
>   static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
>   {
> diff --git a/bsd-user/meson.build b/bsd-user/meson.build
> index 5243122fc5..b97fce1472 100644
> --- a/bsd-user/meson.build
> +++ b/bsd-user/meson.build
> @@ -7,6 +7,7 @@ bsd_user_ss = ss.source_set()
>   common_user_inc += include_directories('include')
>   
>   bsd_user_ss.add(files(
> +  'bsd-proc.c',
>     'bsdload.c',
>     'elfload.c',
>     'main.c',
> @@ -16,6 +17,11 @@ bsd_user_ss.add(files(
>     'uaccess.c',
>   ))

Ok so far.

>   
> +elf = cc.find_library('elf', required: true)
> +procstat = cc.find_library('procstat', required: true)
> +kvm = cc.find_library('kvm', required: true)
> +bsd_user_ss.add(elf, procstat, kvm)

What are these for?  Particularly kvm?


r~


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

* Re: [PATCH 07/32] bsd-user: Implement target_to_host_resource conversion function
  2023-08-27 15:57 ` [PATCH 07/32] bsd-user: Implement target_to_host_resource conversion function Karim Taha
@ 2023-08-29 19:33   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:33 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 83 insertions(+)
>   create mode 100644 bsd-user/bsd-proc.c
> 
> diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
> new file mode 100644
> index 0000000000..ae2e636bb3
> --- /dev/null
> +++ b/bsd-user/bsd-proc.c
> @@ -0,0 +1,83 @@
> +/*
> + *  BSD process related system call helpers
> + *
> + *  Copyright (c) 2013-14 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/>.
> + */
> +#include "qemu/osdep.h"
> +
> +#include <sys/param.h>
> +#include <sys/types.h>
> +#include <sys/cpuset.h>
> +#include <sys/resource.h>
> +#include <sys/wait.h>
> +
> +#include "qemu.h"
> +#include "qemu-bsd.h"
> +#include "signal-common.h"
> +
> +#include "bsd-proc.h"
> +
> +/*
> + * resource/rusage conversion
> + */
> +int target_to_host_resource(int code)
> +{

This should be the identity function, correct?


r~

> +
> +    switch (code) {
> +    case TARGET_RLIMIT_AS:
> +        return RLIMIT_AS;
> +
> +    case TARGET_RLIMIT_CORE:
> +        return RLIMIT_CORE;
> +
> +    case TARGET_RLIMIT_CPU:
> +        return RLIMIT_CPU;
> +
> +    case TARGET_RLIMIT_DATA:
> +        return RLIMIT_DATA;
> +
> +    case TARGET_RLIMIT_FSIZE:
> +        return RLIMIT_FSIZE;
> +
> +    case TARGET_RLIMIT_MEMLOCK:
> +        return RLIMIT_MEMLOCK;
> +
> +    case TARGET_RLIMIT_NOFILE:
> +        return RLIMIT_NOFILE;
> +
> +    case TARGET_RLIMIT_NPROC:
> +        return RLIMIT_NPROC;
> +
> +    case TARGET_RLIMIT_RSS:
> +        return RLIMIT_RSS;
> +
> +    case TARGET_RLIMIT_SBSIZE:
> +        return RLIMIT_SBSIZE;
> +
> +    case TARGET_RLIMIT_STACK:
> +        return RLIMIT_STACK;
> +
> +    case TARGET_RLIMIT_SWAP:
> +        return RLIMIT_SWAP;
> +
> +    case TARGET_RLIMIT_NPTS:
> +        return RLIMIT_NPTS;
> +
> +    default:
> +        return code;
> +    }
> +}
> +



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

* Re: [PATCH 08/32] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion.
  2023-08-27 15:57 ` [PATCH 08/32] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion Karim Taha
@ 2023-08-29 19:36   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:36 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.c | 33 +++++++++++++++++++++++++++++++++
>   1 file changed, 33 insertions(+)

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

> 
> +rlim_t target_to_host_rlim(abi_llong target_rlim)
> +{
> +    abi_llong target_rlim_swap;
> +    rlim_t result;
> +
> +    target_rlim_swap = tswap64(target_rlim);
> +    if (target_rlim_swap == TARGET_RLIM_INFINITY) {
> +        return RLIM_INFINITY;
> +    }
> +
> +    result = target_rlim_swap;
> +    if (target_rlim_swap != (rlim_t)result) {
> +        return RLIM_INFINITY;
> +    }
> +
> +    return result;
> +}
> +
> +abi_llong host_to_target_rlim(rlim_t rlim)
> +{
> +    abi_llong target_rlim_swap;
> +    abi_llong result;
> +
> +    if (rlim == RLIM_INFINITY || rlim != (abi_llong)rlim) {
> +        target_rlim_swap = TARGET_RLIM_INFINITY;
> +    } else {
> +        target_rlim_swap = rlim;
> +    }
> +    result = tswap64(target_rlim_swap);
> +
> +    return result;
> +}

Though I think these are the identity function as well, since afaict we're always talking 
about 64-bit data.


r~


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

* Re: [PATCH 09/32] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage.
  2023-08-27 15:57 ` [PATCH 09/32] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage Karim Taha
@ 2023-08-29 19:39   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:39 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.c | 54 +++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 54 insertions(+)

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

r~


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

* Re: [PATCH 10/32] bsd-user: Implement host_to_target_waitstatus conversion.
  2023-08-27 15:57 ` [PATCH 10/32] bsd-user: Implement host_to_target_waitstatus conversion Karim Taha
@ 2023-08-29 19:40   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:40 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.c | 17 +++++++++++++++++
>   1 file changed, 17 insertions(+)

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

r~


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

* Re: [PATCH 11/32] bsd-user: Get number of cpus.
  2023-08-27 15:57 ` [PATCH 11/32] bsd-user: Get number of cpus Karim Taha
@ 2023-08-29 19:49   ` Richard Henderson
  2023-08-29 21:03     ` Warner Losh
  0 siblings, 1 reply; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:49 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Kyle Evans

On 8/27/23 08:57, Karim Taha wrote:
> From: Kyle Evans <kevans@FreeBSD.org>
> 
> Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.c | 39 +++++++++++++++++++++++++++++++++++++++
>   bsd-user/bsd-proc.h |  2 ++
>   2 files changed, 41 insertions(+)
> 
> diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
> index 49c0fb67d0..dd6bad6de3 100644
> --- a/bsd-user/bsd-proc.c
> +++ b/bsd-user/bsd-proc.c
> @@ -185,3 +185,42 @@ int host_to_target_waitstatus(int status)
>       return status;
>   }
>   
> +int bsd_get_ncpu(void)
> +{
> +    static int ncpu = -1;
> +
> +    if (ncpu != -1) {
> +        return ncpu;
> +    }
> +    if (ncpu == -1) {
> +        cpuset_t mask;
> +
> +        CPU_ZERO(&mask);
> +
> +        if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
> +                               &mask) == 0) {
> +            ncpu = CPU_COUNT(&mask);
> +        }
> +    }
> +#ifdef _SC_NPROCESSORS_ONLN
> +    if (ncpu == -1)
> +        ncpu = sysconf(_SC_NPROCESSORS_ONLN);
> +#endif
> +#if defined(CTL_HW) && defined(HW_NCPU)
> +    if (ncpu == -1) {
> +        int mib[2] = {CTL_HW, HW_NCPU};
> +        size_t sz;
> +
> +        sz = sizeof(ncpu);
> +        if (sysctl(mib, 2, &ncpu, &sz, NULL, NULL) == -1) {
> +            ncpu = -1;
> +        }
> +    }
> +#endif
> +    if (ncpu == -1) {
> +        gemu_log("XXX Missing bsd_get_ncpu() implementation\n");
> +        ncpu = 1;
> +    }
> +    return ncpu;
> +}

This has the look of odd compatibility code.  Surely all three of these alternatives are 
functional, and that sysconf() is easiest to use.

Looking at the freebsd implementation of sysconf, it uses AT_NCPUS if available, so the 
value is already cached within the process in the common case.  So I also don't see a need 
for the ncpu local static either.


r~


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

* Re: [PATCH 12/32] bsd-user: Implement getgroups(2) and setgroups(2) system calls.
  2023-08-27 15:57 ` [PATCH 12/32] bsd-user: Implement getgroups(2) and setgroups(2) system calls Karim Taha
@ 2023-08-29 19:53   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:53 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.h           | 44 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c |  9 +++++++
>   2 files changed, 53 insertions(+)
> 
> diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
> index b6225e520e..ecd6a13c2d 100644
> --- a/bsd-user/bsd-proc.h
> +++ b/bsd-user/bsd-proc.h
> @@ -41,4 +41,48 @@ static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
>       return 0;
>   }
>   
> +/* getgroups(2) */
> +static inline abi_long do_bsd_getgroups(abi_long gidsetsize, abi_long arg2)
> +{
> +    abi_long ret;
> +    uint32_t *target_grouplist;
> +    gid_t *grouplist;
> +    int i;
> +
> +    grouplist = alloca(gidsetsize * sizeof(gid_t));

Don't use alloca for items that are sized by the guest.

Use g_autofree and g_try_new, failing with ENOMEM.

> +/* setgroups(2) */
> +static inline abi_long do_bsd_setgroups(abi_long gidsetsize, abi_long arg2)
> +{
> +    uint32_t *target_grouplist;
> +    gid_t *grouplist;
> +    int i;
> +
> +    grouplist = alloca(gidsetsize * sizeof(gid_t));

Likewise.


r~


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

* Re: [PATCH 13/32] bsd-user: Implement umask(2), setlogin(2) and getlogin(2)
  2023-08-27 15:57 ` [PATCH 13/32] bsd-user: Implement umask(2), setlogin(2) and getlogin(2) Karim Taha
@ 2023-08-29 19:56   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:56 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.h           | 39 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c | 12 +++++++++++
>   2 files changed, 51 insertions(+)

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

r~


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

* Re: [PATCH 14/32] bsd-user: Implement getrusage(2).
  2023-08-27 15:57 ` [PATCH 14/32] bsd-user: Implement getrusage(2) Karim Taha
@ 2023-08-29 19:57   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:57 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.h           | 13 +++++++++++++
>   bsd-user/freebsd/os-syscall.c |  4 ++++
>   2 files changed, 17 insertions(+)

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

r~


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

* Re: [PATCH 15/32] bsd-user: Implement getrlimit(2) and setrlimit(2)
  2023-08-27 15:57 ` [PATCH 15/32] bsd-user: Implement getrlimit(2) and setrlimit(2) Karim Taha
@ 2023-08-29 19:58   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:58 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.h           | 59 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c |  8 +++++
>   2 files changed, 67 insertions(+)

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

r~


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

* Re: [PATCH 16/32] bsd-user: Implement several get/set system calls:
  2023-08-27 15:57 ` [PATCH 16/32] bsd-user: Implement several get/set system calls: Karim Taha
@ 2023-08-29 19:58   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:58 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> getpid(2), getppid(2), getpgrp(2)
> setreuid(2), setregid(2)
> getuid(2), geteuid(2), getgid(2), getegid(2), getpgid(2)
> setuid(2), seteuid(2), setgid(2), setegid(2), setpgid(2)
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.h           | 90 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c | 60 +++++++++++++++++++++++
>   2 files changed, 150 insertions(+)

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

r~


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

* Re: [PATCH 17/32] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid.
  2023-08-27 15:57 ` [PATCH 17/32] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid Karim Taha
@ 2023-08-29 19:59   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 19:59 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.h           | 76 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c | 28 +++++++++++++
>   2 files changed, 104 insertions(+)

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

r~


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

* Re: [PATCH 18/32] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2).
  2023-08-27 15:57 ` [PATCH 18/32] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2) Karim Taha
@ 2023-08-29 20:00   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:00 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/bsd-proc.h           | 28 ++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c | 16 ++++++++++++++++
>   2 files changed, 44 insertions(+)

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

r~


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

* Re: [PATCH 19/32] bsd-user: Implement getpriority(2) and setpriority(2).
  2023-08-27 15:57 ` [PATCH 19/32] bsd-user: Implement getpriority(2) and setpriority(2) Karim Taha
@ 2023-08-29 20:10   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:10 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> +static inline abi_long do_bsd_getpriority(abi_long which, abi_long who)
> +{
> +    abi_long ret;
> +    /*
> +     * Note that negative values are valid for getpriority, so we must
> +     * differentiate based on errno settings.
> +     */
> +    errno = 0;
> +    ret = getpriority(which, who);
> +    if (ret == -1 && errno != 0) {
> +        ret = -host_to_target_errno(errno);
> +        return ret;
> +    }
> +    /* Return value is a biased priority to avoid negative numbers. */
> +    ret = 20 - ret;

This appears to be a linux-ism.

There is no such bias in sys/kern/kern_resource.c, kern_getpriority(), but there is in 
sys/compat/linux/linux_misc.c, linux_getpriority().


r~






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

* Re: [PATCH 20/32] bsd-user: Add freebsd/os-proc.c to meson.build
  2023-08-27 15:57 ` [PATCH 20/32] bsd-user: Add freebsd/os-proc.c to meson.build Karim Taha
@ 2023-08-29 20:12   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:12 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp

On 8/27/23 08:57, Karim Taha wrote:
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/meson.build | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/bsd-user/freebsd/meson.build b/bsd-user/freebsd/meson.build
> index f87c788e84..d169e31235 100644
> --- a/bsd-user/freebsd/meson.build
> +++ b/bsd-user/freebsd/meson.build
> @@ -1,4 +1,5 @@
>   bsd_user_ss.add(files(
> +  'os-proc.c',
>     'os-sys.c',
>     'os-syscall.c',
>   ))

This won't build -- won't configure -- without the file being present.
Just merge into the next patch.


r~


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

* Re: [PATCH 21/32] bsd-user: Implement get_filename_from_fd.
  2023-08-27 15:57 ` [PATCH 21/32] bsd-user: Implement get_filename_from_fd Karim Taha
@ 2023-08-29 20:17   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:17 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.c | 74 ++++++++++++++++++++++++++++++++++++++
>   1 file changed, 74 insertions(+)
>   create mode 100644 bsd-user/freebsd/os-proc.c
> 
> diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
> new file mode 100644
> index 0000000000..5cd800e607
> --- /dev/null
> +++ b/bsd-user/freebsd/os-proc.c
> @@ -0,0 +1,74 @@
> +/*
> + *  FreeBSD process related emulation code
> + *
> + *  Copyright (c) 2013-15 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/>.
> + */
> +#include "qemu/osdep.h"
> +
> +#include <sys/param.h>
> +#include <sys/queue.h>
> +#include <sys/sysctl.h>
> +struct kinfo_proc;
> +#include <libprocstat.h>
> +
> +#include "qemu.h"
> +
> +/*
> + * Get the filename for the given file descriptor.
> + * Note that this may return NULL (fail) if no longer cached in the kernel.
> + */
> +static char *
> +get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
> +{
> +    char *ret = NULL;
> +    unsigned int cnt;
> +    struct procstat *procstat = NULL;
> +    struct kinfo_proc *kp = NULL;
> +    struct filestat_list *head = NULL;
> +    struct filestat *fst;
> +
> +    procstat = procstat_open_sysctl();
> +    if (procstat == NULL)
> +        goto out;

Need braces.  Several checkpatch.pl errors.

With those fixed,
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~


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

* Re: [PATCH 22/32] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve.
  2023-08-27 15:57 ` [PATCH 22/32] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve Karim Taha
@ 2023-08-29 20:28   ` Richard Henderson
  2023-08-29 21:34     ` Warner Losh
  0 siblings, 1 reply; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:28 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.c | 177 +++++++++++++++++++++++++++++++++++++
>   1 file changed, 177 insertions(+)

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

> +    if (do_fexec) {
> +        if (((int)path_or_fd > 0 &&
> +            is_target_elf_binary((int)path_or_fd)) == 1) {
> +            char execpath[PATH_MAX];
> +
> +            /*
> +             * The executable is an elf binary for the target
> +             * arch.  execve() it using the emulator if we can
> +             * determine the filename path from the fd.
> +             */
> +            if (get_filename_from_fd(getpid(), (int)path_or_fd, execpath,
> +                        sizeof(execpath)) != NULL) {
> +                memmove(qarg1 + 2, qarg1, (qargend-qarg1) * sizeof(*qarg1));
> +		qarg1[1] = qarg1[0];
> +		qarg1[0] = (char *)"-0";
> +		qarg1 += 2;
> +		qargend += 2;
> +                *qarg1 = execpath;
> +#ifndef DONT_INHERIT_INTERP_PREFIX
> +                memmove(qarg1 + 2, qarg1, (qargend-qarg1) * sizeof(*qarg1));
> +                *qarg1++ = (char *)"-L";
> +                *qarg1++ = (char *)interp_prefix;
> +#endif

I'm not especailly keen on the ifdef, but I'll let that go.

As for get_filename_from_fd, perhaps it would be cleaner to add a command-line parameter 
which would allow qemu to run from an open file descriptor?  Although perhaps that has 
CLOEXEC implications too...


r~


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

* Re: [PATCH 23/32] bsd-user: Implement t2h procctl control request commands and h2t reaper status struct conversion.
  2023-08-27 15:57 ` [PATCH 23/32] bsd-user: Implement t2h procctl control request commands and h2t reaper status struct conversion Karim Taha
@ 2023-08-29 20:29   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:29 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.c | 52 ++++++++++++++++++++++++++++++++++++++
>   1 file changed, 52 insertions(+)
> 
> diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
> index 396f258a64..f069472156 100644
> --- a/bsd-user/freebsd/os-proc.c
> +++ b/bsd-user/freebsd/os-proc.c
> @@ -249,3 +249,55 @@ execve_end:
>       return ret;
>   }
>   
> +#include <sys/procctl.h>
> +
> +static abi_long
> +t2h_procctl_cmd(int target_cmd, int *host_cmd)
> +{
> +

Identity function?


r~


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

* Re: [PATCH 24/32] bsd-user: Implement h2t reaper_pidinfo and h2t/t2h reaper_kill structs conversion functions.
  2023-08-27 15:57 ` [PATCH 24/32] bsd-user: Implement h2t reaper_pidinfo and h2t/t2h reaper_kill structs conversion functions Karim Taha
@ 2023-08-29 20:29   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:29 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.c | 50 ++++++++++++++++++++++++++++++++++++++
>   1 file changed, 50 insertions(+)
> 
> diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
> index f069472156..a413109bc2 100644
> --- a/bsd-user/freebsd/os-proc.c
> +++ b/bsd-user/freebsd/os-proc.c
> @@ -301,3 +301,53 @@ h2t_reaper_status(struct procctl_reaper_status *host_rs,
>       return 0;
>   }
>   
> +static abi_long
> +t2h_reaper_kill(abi_ulong target_rk_addr, struct procctl_reaper_kill *host_rk)
> +{
> +    struct target_procctl_reaper_kill *target_rk;
> +

Unused functions will error; non-bisectable.


r~


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

* Re: [PATCH 25/32] bsd-user: Implement procctl(2) system call.
  2023-08-27 15:57 ` [PATCH 25/32] bsd-user: Implement procctl(2) system call Karim Taha
@ 2023-08-29 20:30   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:30 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.c    | 114 ++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c |   3 +
>   2 files changed, 117

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

I think you need to squash this with patch 24.


r~


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

* Re: [PATCH 26/32] bsd-user: Implement execve(2) and fexecve(2) system calls.
  2023-08-27 15:57 ` [PATCH 26/32] bsd-user: Implement execve(2) and fexecve(2) system calls Karim Taha
@ 2023-08-29 20:31   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:31 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.h    | 49 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c | 10 +++++++
>   2 files changed, 59 insertions(+)
>   create mode 100644 bsd-user/freebsd/os-proc.h

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

r~


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

* Re: [PATCH 27/32] bsd-user: Implement wait4(2) and wait6(2) system calls.
  2023-08-27 15:57 ` [PATCH 27/32] bsd-user: Implement wait4(2) and wait6(2) " Karim Taha
@ 2023-08-29 20:33   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:33 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.h    | 75 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c | 14 +++++++
>   2 files changed, 89 insertions(+)

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

r~


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

* Re: [PATCH 28/32] bsd-user: Implement setloginclass(2) and getloginclass(2) system calls.
  2023-08-27 15:57 ` [PATCH 28/32] bsd-user: Implement setloginclass(2) and getloginclass(2) " Karim Taha
@ 2023-08-29 20:33   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:33 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son<sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son<sson@FreeBSD.org>
> Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.h    | 32 ++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c |  8 ++++++++
>   2 files changed, 40 insertions(+)

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

r~


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

* Re: [PATCH 29/32] bsd-user: Implement pdgetpid(2) and the undocumented setugid.
  2023-08-27 15:57 ` [PATCH 29/32] bsd-user: Implement pdgetpid(2) and the undocumented setugid Karim Taha
@ 2023-08-29 20:36   ` Richard Henderson
  2023-08-29 21:14     ` Warner Losh
  0 siblings, 1 reply; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:36 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.h    | 23 +++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c |  8 ++++++++
>   2 files changed, 31 insertions(+)

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

> +/* undocumented __setugid */
> +static inline abi_long do_freebsd___setugid(abi_long arg1)
> +{
> +    return get_errno(__setugid(arg1));
> +}

Given that this is

#ifdef REGRESSION
...
#else /* !REGRESSION */
         return (ENOSYS);
#endif /* REGRESSION */

in current freebsd, we could probably just stub this out?


r~


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

* Re: [PATCH 30/32] bsd-user: Implement fork(2) and vfork(2) system calls.
  2023-08-27 15:57 ` [PATCH 30/32] bsd-user: Implement fork(2) and vfork(2) system calls Karim Taha
@ 2023-08-29 20:39   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:39 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.h    | 34 ++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c |  8 ++++++++
>   2 files changed, 42 insertions(+)

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

r~


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

* Re: [PATCH 31/32] bsd-user: Implement rfork(2) system call.
  2023-08-27 15:57 ` [PATCH 31/32] bsd-user: Implement rfork(2) system call Karim Taha
@ 2023-08-29 20:43   ` Richard Henderson
  0 siblings, 0 replies; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:43 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.h    | 38 +++++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c |  4 ++++
>   2 files changed, 42 insertions(+)

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


> +    /*
> +     * XXX We need to handle RFMEM here, as well.  Neither are safe to execute
> +     * as-is on x86 hosts because they'll split memory but not the stack,
> +     * wreaking havoc on host architectures that use the stack to store the
> +     * return address as both threads try to pop it off.  Rejecting RFSPAWN
> +     * entirely for now is ok, the only consumer at the moment is posix_spawn
> +     * and it will fall back to classic vfork(2) if we return EINVAL.
> +     */
> +    if ((flags & TARGET_RFSPAWN) != 0)
> +        return -TARGET_EINVAL;

Braces.


r~


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

* Re: [PATCH 32/32] bsd-user: Implement pdfork(2) system call.
  2023-08-27 15:57 ` [PATCH 32/32] bsd-user: Implement pdfork(2) " Karim Taha
@ 2023-08-29 20:58   ` Richard Henderson
  2023-08-29 21:27     ` Warner Losh
  0 siblings, 1 reply; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 20:58 UTC (permalink / raw)
  To: Karim Taha, qemu-devel; +Cc: imp, Stacey Son

On 8/27/23 08:57, Karim Taha wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>   bsd-user/freebsd/os-proc.h    | 32 ++++++++++++++++++++++++++++++++
>   bsd-user/freebsd/os-syscall.c |  4 ++++
>   2 files changed, 36 insertions(+)
> 
> diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
> index 94824d737a..1eaba908a5 100644
> --- a/bsd-user/freebsd/os-proc.h
> +++ b/bsd-user/freebsd/os-proc.h
> @@ -248,4 +248,36 @@ static inline abi_long do_freebsd_rfork(void *cpu_env, abi_long flags)
>   
>   }
>   
> +/* pdfork(2) */
> +static inline abi_long do_freebsd_pdfork(void *cpu_env, abi_ulong target_fdp,
> +        abi_long flags)
> +{
> +    abi_long ret;
> +    abi_ulong child_flag;
> +    int fd;
> +
> +    fork_start();
> +    ret = pdfork(&fd, flags);
> +    if (ret == 0) {
> +        /* child */
> +        child_flag = 1;
> +        target_cpu_clone_regs(cpu_env, 0);
> +    } else {
> +        /* parent */
> +        child_flag = 0;
> +    }
> +    if (put_user_s32(fd, target_fdp)) {
> +        return -TARGET_EFAULT;
> +    }

I *think* this copy belongs in the parent?  It's really hard to follow the path of new 
process creation within the freebsd kernel.

Anyway, the rest looks fine so I'll give an

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


r~


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

* Re: [PATCH 11/32] bsd-user: Get number of cpus.
  2023-08-29 19:49   ` Richard Henderson
@ 2023-08-29 21:03     ` Warner Losh
  0 siblings, 0 replies; 73+ messages in thread
From: Warner Losh @ 2023-08-29 21:03 UTC (permalink / raw)
  To: Richard Henderson; +Cc: Karim Taha, qemu-devel, Kyle Evans

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

On Tue, Aug 29, 2023 at 1:50 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 8/27/23 08:57, Karim Taha wrote:
> > From: Kyle Evans <kevans@FreeBSD.org>
> >
> > Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
> > Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> > ---
> >   bsd-user/bsd-proc.c | 39 +++++++++++++++++++++++++++++++++++++++
> >   bsd-user/bsd-proc.h |  2 ++
> >   2 files changed, 41 insertions(+)
> >
> > diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
> > index 49c0fb67d0..dd6bad6de3 100644
> > --- a/bsd-user/bsd-proc.c
> > +++ b/bsd-user/bsd-proc.c
> > @@ -185,3 +185,42 @@ int host_to_target_waitstatus(int status)
> >       return status;
> >   }
> >
> > +int bsd_get_ncpu(void)
> > +{
> > +    static int ncpu = -1;
> > +
> > +    if (ncpu != -1) {
> > +        return ncpu;
> > +    }
> > +    if (ncpu == -1) {
> > +        cpuset_t mask;
> > +
> > +        CPU_ZERO(&mask);
> > +
> > +        if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
> sizeof(mask),
> > +                               &mask) == 0) {
> > +            ncpu = CPU_COUNT(&mask);
> > +        }
> > +    }
> > +#ifdef _SC_NPROCESSORS_ONLN
> > +    if (ncpu == -1)
> > +        ncpu = sysconf(_SC_NPROCESSORS_ONLN);
> > +#endif
> > +#if defined(CTL_HW) && defined(HW_NCPU)
> > +    if (ncpu == -1) {
> > +        int mib[2] = {CTL_HW, HW_NCPU};
> > +        size_t sz;
> > +
> > +        sz = sizeof(ncpu);
> > +        if (sysctl(mib, 2, &ncpu, &sz, NULL, NULL) == -1) {
> > +            ncpu = -1;
> > +        }
> > +    }
> > +#endif
> > +    if (ncpu == -1) {
> > +        gemu_log("XXX Missing bsd_get_ncpu() implementation\n");
> > +        ncpu = 1;
> > +    }
> > +    return ncpu;
> > +}
>
> This has the look of odd compatibility code.  Surely all three of these
> alternatives are
> functional, and that sysconf() is easiest to use.
>

This code dates to the earliest days of the emulator when it ran on all
three BSDs. NetBSD
does support _SC_NPROCESSORS_ONLN, so we should leave that case.

I think the getaffinity stuff is there so that one can restrict a process
group to a subset of
the CPUs in the system for nicer build farms, but I could be mistaken about
that.  NetBSD
doesn't support this call, AFAICT, but I'd rather not add #ifdef's for
NetBSD until we actually
do a NetBSD port. I'll have to check with Kyle to see if that was really
needed, or if the
code was cut and pasted from elsewhere.

I don't think we need to fall back to the 4.4BSD hw.ncpu sysctl. Everybody
supports the sysconf
interface.


> Looking at the freebsd implementation of sysconf, it uses AT_NCPUS if
> available, so the
> value is already cached within the process in the common case.  So I also
> don't see a need
> for the ncpu local static either.
>

I agree with this... We only use it to impelment hw.ncpu emulation, and to
set AT_NCPUS
when we load, so who cares if it's expensive :).

Warner

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

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

* Re: [PATCH 29/32] bsd-user: Implement pdgetpid(2) and the undocumented setugid.
  2023-08-29 20:36   ` Richard Henderson
@ 2023-08-29 21:14     ` Warner Losh
  0 siblings, 0 replies; 73+ messages in thread
From: Warner Losh @ 2023-08-29 21:14 UTC (permalink / raw)
  To: Richard Henderson; +Cc: Karim Taha, qemu-devel, Stacey Son

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

On Tue, Aug 29, 2023 at 2:36 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 8/27/23 08:57, Karim Taha wrote:
> > From: Stacey Son <sson@FreeBSD.org>
> >
> > Signed-off-by: Stacey Son <sson@FreeBSD.org>
> > Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> > ---
> >   bsd-user/freebsd/os-proc.h    | 23 +++++++++++++++++++++++
> >   bsd-user/freebsd/os-syscall.c |  8 ++++++++
> >   2 files changed, 31 insertions(+)
>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>
> > +/* undocumented __setugid */
> > +static inline abi_long do_freebsd___setugid(abi_long arg1)
> > +{
> > +    return get_errno(__setugid(arg1));
> > +}
>
> Given that this is
>
> #ifdef REGRESSION
> ...
> #else /* !REGRESSION */
>          return (ENOSYS);
> #endif /* REGRESSION */
>
> in current freebsd, we could probably just stub this out?
>

I agree...

The REGRESSION kernel option exists only so that the
tools/regression/security/proc_to_proc tests
can run. this is an interesting set of tests, but hasn't been updated since
2004, except for the
usual 'churn' commits required by sweeps for new-compiler things, or
project policy changes.
So it's not even clear if this specific regression test is still
interesting (though there are many
other tests in the tree that are recent and under active development).

So it's irrelevant to the bsd-user emulator, and returning ENOSYS will
match perfectly what almost any
kernel deployed will do.

Warner

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

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

* Re: [PATCH 32/32] bsd-user: Implement pdfork(2) system call.
  2023-08-29 20:58   ` Richard Henderson
@ 2023-08-29 21:27     ` Warner Losh
  2023-08-29 21:53       ` Richard Henderson
  0 siblings, 1 reply; 73+ messages in thread
From: Warner Losh @ 2023-08-29 21:27 UTC (permalink / raw)
  To: Richard Henderson; +Cc: Karim Taha, qemu-devel, Stacey Son

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

On Tue, Aug 29, 2023 at 2:58 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 8/27/23 08:57, Karim Taha wrote:
> > From: Stacey Son <sson@FreeBSD.org>
> >
> > Signed-off-by: Stacey Son <sson@FreeBSD.org>
> > Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> > ---
> >   bsd-user/freebsd/os-proc.h    | 32 ++++++++++++++++++++++++++++++++
> >   bsd-user/freebsd/os-syscall.c |  4 ++++
> >   2 files changed, 36 insertions(+)
> >
> > diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
> > index 94824d737a..1eaba908a5 100644
> > --- a/bsd-user/freebsd/os-proc.h
> > +++ b/bsd-user/freebsd/os-proc.h
> > @@ -248,4 +248,36 @@ static inline abi_long do_freebsd_rfork(void
> *cpu_env, abi_long flags)
> >
> >   }
> >
> > +/* pdfork(2) */
> > +static inline abi_long do_freebsd_pdfork(void *cpu_env, abi_ulong
> target_fdp,
> > +        abi_long flags)
> > +{
> > +    abi_long ret;
> > +    abi_ulong child_flag;
> > +    int fd;
> > +
> > +    fork_start();
> > +    ret = pdfork(&fd, flags);
> > +    if (ret == 0) {
> > +        /* child */
> > +        child_flag = 1;
> > +        target_cpu_clone_regs(cpu_env, 0);
> > +    } else {
> > +        /* parent */
> > +        child_flag = 0;
> > +    }
> > +    if (put_user_s32(fd, target_fdp)) {
> > +        return -TARGET_EFAULT;
> > +    }
>
> I *think* this copy belongs in the parent?


I think that it's copied out in both cases. For normal fork, this would
be 0 for the pid. However, it appears to return the same FD to both
the parent and child (see your next comment), so it should be in both
paths. And even if it returned something different for parent and child
(which seems unlikely given how the code is setup), we want to return
the fd each one sees. So either way, I think this code is correct.


> It's really hard to follow the path of new
> process creation within the freebsd kernel.
>

Agreed.


> Anyway, the rest looks fine so I'll give an
>
> Acked-by: Richard Henderson <richard.henderson@linaro.org>
>

Reviewed-by: Warner Losh <imp@bsdimp.com>

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

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

* Re: [PATCH 22/32] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve.
  2023-08-29 20:28   ` Richard Henderson
@ 2023-08-29 21:34     ` Warner Losh
  0 siblings, 0 replies; 73+ messages in thread
From: Warner Losh @ 2023-08-29 21:34 UTC (permalink / raw)
  To: Richard Henderson; +Cc: Karim Taha, qemu-devel, Stacey Son

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

On Tue, Aug 29, 2023 at 2:28 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 8/27/23 08:57, Karim Taha wrote:
> > From: Stacey Son<sson@FreeBSD.org>
> >
> > Signed-off-by: Stacey Son<sson@FreeBSD.org>
> > Signed-off-by: Karim Taha<kariem.taha2.7@gmail.com>
> > ---
> >   bsd-user/freebsd/os-proc.c | 177 +++++++++++++++++++++++++++++++++++++
> >   1 file changed, 177 insertions(+)
>
> Acked-by: Richard Henderson <richard.henderson@linaro.org>
>
> > +    if (do_fexec) {
> > +        if (((int)path_or_fd > 0 &&
> > +            is_target_elf_binary((int)path_or_fd)) == 1) {
> > +            char execpath[PATH_MAX];
> > +
> > +            /*
> > +             * The executable is an elf binary for the target
> > +             * arch.  execve() it using the emulator if we can
> > +             * determine the filename path from the fd.
> > +             */
> > +            if (get_filename_from_fd(getpid(), (int)path_or_fd,
> execpath,
> > +                        sizeof(execpath)) != NULL) {
> > +                memmove(qarg1 + 2, qarg1, (qargend-qarg1) *
> sizeof(*qarg1));
> > +             qarg1[1] = qarg1[0];
> > +             qarg1[0] = (char *)"-0";
> > +             qarg1 += 2;
> > +             qargend += 2;
> > +                *qarg1 = execpath;
> > +#ifndef DONT_INHERIT_INTERP_PREFIX
> > +                memmove(qarg1 + 2, qarg1, (qargend-qarg1) *
> sizeof(*qarg1));
> > +                *qarg1++ = (char *)"-L";
> > +                *qarg1++ = (char *)interp_prefix;
> > +#endif
>
> I'm not especailly keen on the ifdef, but I'll let that go.
>
> As for get_filename_from_fd, perhaps it would be cleaner to add a
> command-line parameter
> which would allow qemu to run from an open file descriptor?  Although
> perhaps that has
> CLOEXEC implications too...
>

This is one area that's in transition in the bsd-user stuff, but we've not
yet finished
that transition. Doug Rabson has created something that caches a reference
to
the interpreter, and if we exec the same kind of binary, it will reuse that
reference.
In a jail that Doug's code runs, this allows the interpreter to be running
a binary
from outside the jail, while restricting the emulated binary's reach to the
jail.
This eliminates, in some cases, the need to inherit this prefix. However,
in other
cases, it still seems to be needed (like when I'm not in a chroot
environment and
wanting to pull the shared libraries from a different location). How to
resolve
these two cases is an on-going area of discussions. And all the work may not
yet be merged with the upstream tree.

Warner

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

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

* Re: [PATCH 32/32] bsd-user: Implement pdfork(2) system call.
  2023-08-29 21:27     ` Warner Losh
@ 2023-08-29 21:53       ` Richard Henderson
  2023-08-29 22:06         ` Warner Losh
  0 siblings, 1 reply; 73+ messages in thread
From: Richard Henderson @ 2023-08-29 21:53 UTC (permalink / raw)
  To: Warner Losh; +Cc: Karim Taha, qemu-devel, Stacey Son

On 8/29/23 14:27, Warner Losh wrote:
>      > +    if (put_user_s32(fd, target_fdp)) {
>      > +        return -TARGET_EFAULT;
>      > +    }
> 
>     I *think* this copy belongs in the parent?
> 
> 
> I think that it's copied out in both cases. For normal fork, this would
> be 0 for the pid. However, it appears to return the same FD to both
> the parent and child (see your next comment), so it should be in both
> paths. And even if it returned something different for parent and child
> (which seems unlikely given how the code is setup), we want to return
> the fd each one sees. So either way, I think this code is correct.
> 
>     It's really hard to follow the path of new
>     process creation within the freebsd kernel.
> 
> 
> Agreed.

I think that the child never returns from do_fork.  The child pid == 0 happens as part of 
do_fork or vm_forkproc or somesuch, but the new process definitely begins life at fork_return.

Therefore only the parent passes returns from fork1 to set *fdp.


r~


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

* Re: [PATCH 32/32] bsd-user: Implement pdfork(2) system call.
  2023-08-29 21:53       ` Richard Henderson
@ 2023-08-29 22:06         ` Warner Losh
  0 siblings, 0 replies; 73+ messages in thread
From: Warner Losh @ 2023-08-29 22:06 UTC (permalink / raw)
  To: Richard Henderson; +Cc: Karim Taha, qemu-devel, Stacey Son

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

On Tue, Aug 29, 2023 at 3:53 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 8/29/23 14:27, Warner Losh wrote:
> >      > +    if (put_user_s32(fd, target_fdp)) {
> >      > +        return -TARGET_EFAULT;
> >      > +    }
> >
> >     I *think* this copy belongs in the parent?
> >
> >
> > I think that it's copied out in both cases. For normal fork, this would
> > be 0 for the pid. However, it appears to return the same FD to both
> > the parent and child (see your next comment), so it should be in both
> > paths. And even if it returned something different for parent and child
> > (which seems unlikely given how the code is setup), we want to return
> > the fd each one sees. So either way, I think this code is correct.
> >
> >     It's really hard to follow the path of new
> >     process creation within the freebsd kernel.
> >
> >
> > Agreed.
>
> I think that the child never returns from do_fork.  The child pid == 0
> happens as part of
> do_fork or vm_forkproc or somesuch, but the new process definitely begins
> life at fork_return.
>

I confused the 'returns twice' behavior in userland with the gymnastics the
kernel does
on creating a new process (where things don't return twice). Having looked
at that code,
I'm sure you are right now and it should only be set in the parent. I don't
see where it is
set in the fork_return path. For normal fork, the return value register is
cleared, as is the
carry bit, used to signal errors from system calls on FreeBSD. And having
asked someone
whose more of an expert, he confirms it is not set in the child.

Therefore only the parent passes returns from fork1 to set *fdp.
>

I agree. We should move that code to the parent branch.

Warner


>
> r~
>

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

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

* Re: [PATCH 04/32] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h.
  2023-08-29 19:14   ` Richard Henderson
@ 2023-09-11 20:58     ` Karim Taha
  0 siblings, 0 replies; 73+ messages in thread
From: Karim Taha @ 2023-09-11 20:58 UTC (permalink / raw)
  To: Richard Henderson, qemu-devel; +Cc: Warner Losh

Richard Henderson <richard.henderson@linaro.org> wrote:

> On 8/27/23 08:57, Karim Taha wrote:
>> From: Stacey Son <sson@FreeBSD.org>
>> 
>> Signed-off-by: Stacey Son <sson@FreeBSD.org>
>> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
>> ---
>>   bsd-user/main.c | 2 +-
>>   bsd-user/qemu.h | 7 +++++++
>>   2 files changed, 8 insertions(+), 1 deletion(-)
>> 
>> diff --git a/bsd-user/main.c b/bsd-user/main.c
>> index 381bb18df8..b94b2d34b6 100644
>> --- a/bsd-user/main.c
>> +++ b/bsd-user/main.c
>> @@ -88,7 +88,7 @@ unsigned long reserved_va = MAX_RESERVED_VA;
>>   unsigned long reserved_va;
>>   #endif
>>   
>> -static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
>> +const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
>>   const char *qemu_uname_release;
>>   char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
>>   
>
> Adding interp_prefix is unrelated.
>
> Without that,
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>
>
> r~
>
I grepped for `interp_prefix`, it's later used in the
`freebsd_exec_common` function definition, so do you mean I should add
it with the relevant commit that defines the function?

--
Karim Taha

>> diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
>> index 6724bb9f0a..23bbdd3e0c 100644
>> --- a/bsd-user/qemu.h
>> +++ b/bsd-user/qemu.h
>> @@ -113,6 +113,7 @@ typedef struct TaskState {
>>   } __attribute__((aligned(16))) TaskState;
>>   
>>   void stop_all_tasks(void);
>> +extern const char *interp_prefix;
>>   extern const char *qemu_uname_release;
>>   
>>   /*
>> @@ -251,6 +252,12 @@ abi_long get_errno(abi_long ret);
>>   bool is_error(abi_long ret);
>>   int host_to_target_errno(int err);
>>   
>> +/* os-proc.c */
>> +abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
>> +        abi_ulong guest_envp, int do_fexec);
>> +abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
>> +        abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
>> +
>>   /* os-sys.c */
>>   abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
>>           abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);


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

* Re: [PATCH 06/32] bsd-user: Add bsd-proc.c to meson.build
  2023-08-29 19:17   ` Richard Henderson
@ 2023-09-11 22:45     ` Karim Taha
  0 siblings, 0 replies; 73+ messages in thread
From: Karim Taha @ 2023-09-11 22:45 UTC (permalink / raw)
  To: Richard Henderson, qemu-devel; +Cc: Warner Losh

Richard Henderson <richard.henderson@linaro.org> wrote:

>> +elf = cc.find_library('elf', required: true)
>> +procstat = cc.find_library('procstat', required: true)
>> +kvm = cc.find_library('kvm', required: true)
>> +bsd_user_ss.add(elf, procstat, kvm)
>
> What are these for?  Particularly kvm?
>
>
> r~

It's need to link with `libprocstat`, which is need for the 
`filestat` struct definition, and it's `proc_*` functions used
is `get_filename_from_fd` function, however the function is declared
static, which emits an `unused function warning`, but compiles
successfully.

The linker errors only when the `get_filename_from_fd` is used in
`freebsd_exec_common` function.

--
Karim Taha


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

end of thread, other threads:[~2023-09-11 22:47 UTC | newest]

Thread overview: 73+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-27 15:57 [PATCH 00/32] bsd-user: Implement freebsd process related system calls Karim Taha
2023-08-27 15:57 ` [PATCH 01/32] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics Karim Taha
2023-08-29 19:07   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 02/32] bsd-user: Implement procctl(2) system call Karim Taha
2023-08-29 19:10   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 03/32] bsd-user: Implement host_to_target_siginfo Karim Taha
2023-08-29 19:13   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 04/32] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h Karim Taha
2023-08-29 19:14   ` Richard Henderson
2023-09-11 20:58     ` Karim Taha
2023-08-27 15:57 ` [PATCH 05/32] bsd-user: add extern declarations for bsd-proc.c conversion functions Karim Taha
2023-08-29 19:15   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 06/32] bsd-user: Add bsd-proc.c to meson.build Karim Taha
2023-08-29 19:17   ` Richard Henderson
2023-09-11 22:45     ` Karim Taha
2023-08-27 15:57 ` [PATCH 07/32] bsd-user: Implement target_to_host_resource conversion function Karim Taha
2023-08-29 19:33   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 08/32] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion Karim Taha
2023-08-29 19:36   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 09/32] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage Karim Taha
2023-08-29 19:39   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 10/32] bsd-user: Implement host_to_target_waitstatus conversion Karim Taha
2023-08-29 19:40   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 11/32] bsd-user: Get number of cpus Karim Taha
2023-08-29 19:49   ` Richard Henderson
2023-08-29 21:03     ` Warner Losh
2023-08-27 15:57 ` [PATCH 12/32] bsd-user: Implement getgroups(2) and setgroups(2) system calls Karim Taha
2023-08-29 19:53   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 13/32] bsd-user: Implement umask(2), setlogin(2) and getlogin(2) Karim Taha
2023-08-29 19:56   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 14/32] bsd-user: Implement getrusage(2) Karim Taha
2023-08-29 19:57   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 15/32] bsd-user: Implement getrlimit(2) and setrlimit(2) Karim Taha
2023-08-29 19:58   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 16/32] bsd-user: Implement several get/set system calls: Karim Taha
2023-08-29 19:58   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 17/32] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid Karim Taha
2023-08-29 19:59   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 18/32] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2) Karim Taha
2023-08-29 20:00   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 19/32] bsd-user: Implement getpriority(2) and setpriority(2) Karim Taha
2023-08-29 20:10   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 20/32] bsd-user: Add freebsd/os-proc.c to meson.build Karim Taha
2023-08-29 20:12   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 21/32] bsd-user: Implement get_filename_from_fd Karim Taha
2023-08-29 20:17   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 22/32] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve Karim Taha
2023-08-29 20:28   ` Richard Henderson
2023-08-29 21:34     ` Warner Losh
2023-08-27 15:57 ` [PATCH 23/32] bsd-user: Implement t2h procctl control request commands and h2t reaper status struct conversion Karim Taha
2023-08-29 20:29   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 24/32] bsd-user: Implement h2t reaper_pidinfo and h2t/t2h reaper_kill structs conversion functions Karim Taha
2023-08-29 20:29   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 25/32] bsd-user: Implement procctl(2) system call Karim Taha
2023-08-29 20:30   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 26/32] bsd-user: Implement execve(2) and fexecve(2) system calls Karim Taha
2023-08-29 20:31   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 27/32] bsd-user: Implement wait4(2) and wait6(2) " Karim Taha
2023-08-29 20:33   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 28/32] bsd-user: Implement setloginclass(2) and getloginclass(2) " Karim Taha
2023-08-29 20:33   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 29/32] bsd-user: Implement pdgetpid(2) and the undocumented setugid Karim Taha
2023-08-29 20:36   ` Richard Henderson
2023-08-29 21:14     ` Warner Losh
2023-08-27 15:57 ` [PATCH 30/32] bsd-user: Implement fork(2) and vfork(2) system calls Karim Taha
2023-08-29 20:39   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 31/32] bsd-user: Implement rfork(2) system call Karim Taha
2023-08-29 20:43   ` Richard Henderson
2023-08-27 15:57 ` [PATCH 32/32] bsd-user: Implement pdfork(2) " Karim Taha
2023-08-29 20:58   ` Richard Henderson
2023-08-29 21:27     ` Warner Losh
2023-08-29 21:53       ` Richard Henderson
2023-08-29 22:06         ` Warner Losh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).