Re: [PATCH v4 03/12] {linux,bsd}-user: Update ts_tid after fork()

2024-02-19 Thread Warner Losh
On Mon, Feb 19, 2024 at 7:22 AM Ilya Leoshkevich  wrote:

> Currently ts_tid contains the parent tid after fork(), which is not
> correct. So far it has not affected anything, but the upcoming
> follow-fork-mode child support relies on the correct value, so fix it.
>
> Reviewed-by: Alex Bennée 
> Signed-off-by: Ilya Leoshkevich 
>

Reviewed-by: Warner Losh 

Warner


> ---
>  bsd-user/main.c   | 1 +
>  linux-user/main.c | 1 +
>  2 files changed, 2 insertions(+)
>
> diff --git a/bsd-user/main.c b/bsd-user/main.c
> index e5efb7b8458..72289673a94 100644
> --- a/bsd-user/main.c
> +++ b/bsd-user/main.c
> @@ -127,6 +127,7 @@ void fork_end(int child)
>   * state, so we don't need to end_exclusive() here.
>   */
>  qemu_init_cpu_list();
> +get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
>  gdbserver_fork(thread_cpu);
>  } else {
>  mmap_fork_end(child);
> diff --git a/linux-user/main.c b/linux-user/main.c
> index 74b2fbb3938..1d53f708354 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -160,6 +160,7 @@ void fork_end(int child)
>  }
>  }
>  qemu_init_cpu_list();
> +get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
>  gdbserver_fork(thread_cpu);
>  } else {
>  cpu_list_unlock();
> --
> 2.43.2
>
>


Re: [PATCH v4 02/12] {linux,bsd}-user: Introduce get_task_state()

2024-02-19 Thread Warner Losh
On Mon, Feb 19, 2024 at 7:21 AM Ilya Leoshkevich  wrote:

> A CPU's TaskState is stored in the CPUState's void *opaque field,
> accessing which is somewhat awkward due to having to use a cast.
> Introduce a wrapper and use it everywhere.
>
> Suggested-by: Alex Bennée 
> Signed-off-by: Ilya Leoshkevich 
>

Reviewed-by: Warner Losh 

The bsd-user stuff is definitely good. The linux-user seems good, but I
didn't look
at it as closely.

Warner

>  bsd-user/bsd-file.h   |  2 +-
>  bsd-user/qemu.h   |  5 +
>  bsd-user/signal.c | 20 ++--
>  gdbstub/user-target.c |  4 ++--
>  include/user/safe-syscall.h   |  2 +-
>  linux-user/aarch64/cpu_loop.c |  2 +-
>  linux-user/arm/cpu_loop.c |  4 ++--
>  linux-user/arm/signal.c   |  2 +-
>  linux-user/cris/cpu_loop.c|  2 +-
>  linux-user/elfload.c  |  6 +++---
>  linux-user/hppa/signal.c  |  2 +-
>  linux-user/linuxload.c|  2 +-
>  linux-user/m68k/cpu_loop.c|  2 +-
>  linux-user/m68k/target_cpu.h  |  2 +-
>  linux-user/mips/cpu_loop.c|  2 +-
>  linux-user/ppc/signal.c   |  4 ++--
>  linux-user/qemu.h |  5 +
>  linux-user/riscv/cpu_loop.c   |  2 +-
>  linux-user/signal-common.h|  2 +-
>  linux-user/signal.c   | 30 +++---
>  linux-user/syscall.c  | 26 +-
>  linux-user/vm86.c | 18 +-
>  linux-user/xtensa/signal.c|  2 +-
>  plugins/api.c |  8 
>  semihosting/arm-compat-semi.c |  8 
>  25 files changed, 87 insertions(+), 77 deletions(-)
>
> diff --git a/bsd-user/bsd-file.h b/bsd-user/bsd-file.h
> index 3c00dc00567..6fa2c30b4de 100644
> --- a/bsd-user/bsd-file.h
> +++ b/bsd-user/bsd-file.h
> @@ -641,7 +641,7 @@ static abi_long do_bsd_readlink(CPUArchState *env,
> abi_long arg1,
>  }
>  if (strcmp(p1, "/proc/curproc/file") == 0) {
>  CPUState *cpu = env_cpu(env);
> -TaskState *ts = (TaskState *)cpu->opaque;
> +TaskState *ts = get_task_state(cpu);
>  strncpy(p2, ts->bprm->fullpath, arg3);
>  ret = MIN((abi_long)strlen(ts->bprm->fullpath), arg3);
>  } else {
> diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
> index dc842fffa7d..a2417b25156 100644
> --- a/bsd-user/qemu.h
> +++ b/bsd-user/qemu.h
> @@ -110,6 +110,11 @@ typedef struct TaskState {
>  struct target_sigaltstack sigaltstack_used;
>  } __attribute__((aligned(16))) TaskState;
>
> +static inline TaskState *get_task_state(CPUState *cs)
> +{
> +return cs->opaque;
> +}
> +
>  void stop_all_tasks(void);
>  extern const char *interp_prefix;
>  extern const char *qemu_uname_release;
> diff --git a/bsd-user/signal.c b/bsd-user/signal.c
> index f4352e4530f..e9f80a06d32 100644
> --- a/bsd-user/signal.c
> +++ b/bsd-user/signal.c
> @@ -319,7 +319,7 @@ void host_to_target_siginfo(target_siginfo_t *tinfo,
> const siginfo_t *info)
>
>  int block_signals(void)
>  {
> -TaskState *ts = (TaskState *)thread_cpu->opaque;
> +TaskState *ts = get_task_state(thread_cpu);
>  sigset_t set;
>
>  /*
> @@ -359,7 +359,7 @@ void dump_core_and_abort(int target_sig)
>  {
>  CPUState *cpu = thread_cpu;
>  CPUArchState *env = cpu_env(cpu);
> -TaskState *ts = cpu->opaque;
> +TaskState *ts = get_task_state(cpu);
>  int core_dumped = 0;
>  int host_sig;
>  struct sigaction act;
> @@ -421,7 +421,7 @@ void queue_signal(CPUArchState *env, int sig, int
> si_type,
>target_siginfo_t *info)
>  {
>  CPUState *cpu = env_cpu(env);
> -TaskState *ts = cpu->opaque;
> +TaskState *ts = get_task_state(cpu);
>
>  trace_user_queue_signal(env, sig);
>
> @@ -476,7 +476,7 @@ void force_sig_fault(int sig, int code, abi_ulong addr)
>  static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
>  {
>  CPUState *cpu = thread_cpu;
> -TaskState *ts = cpu->opaque;
> +TaskState *ts = get_task_state(cpu);
>  target_siginfo_t tinfo;
>  ucontext_t *uc = puc;
>  struct emulated_sigtable *k;
> @@ -585,7 +585,7 @@ static void host_signal_handler(int host_sig,
> siginfo_t *info, void *puc)
>  /* compare to kern/kern_sig.c sys_sigaltstack() and kern_sigaltstack() */
>  abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr,
> abi_ulong sp)
>  {
> -TaskState *ts = (TaskState *)thread_cpu->opaque;
> +TaskState *ts = get_task_state(thread_cpu);
>  int ret;
>  target_stack_t oss;
>
> @@ -714,7 +714,7 @@ int do_sigaction(int sig, const struct
> target

Re: [PATCH v3 29/29] user: Prefer fast cpu_env() over slower CPU QOM cast macro

2024-02-07 Thread Warner Losh
[[ I dont know if it's too late ]]

On Mon, Jan 29, 2024 at 9:48 AM Philippe Mathieu-Daudé 
wrote:

> Mechanical patch produced running the command documented
> in scripts/coccinelle/cpu_env.cocci_template header.
>
> Signed-off-by: Philippe Mathieu-Daudé 
> ---
>  bsd-user/signal.c   | 3 +--
>  linux-user/signal.c | 6 ++
>  2 files changed, 3 insertions(+), 6 deletions(-)
>

Reviewed-by: Warner Losh 


> diff --git a/bsd-user/signal.c b/bsd-user/signal.c
> index ca31470772..c6f0b1be38 100644
> --- a/bsd-user/signal.c
> +++ b/bsd-user/signal.c
> @@ -463,14 +463,13 @@ static int fatal_signal(int sig)
>  void force_sig_fault(int sig, int code, abi_ulong addr)
>  {
>  CPUState *cpu = thread_cpu;
> -CPUArchState *env = cpu_env(cpu);
>  target_siginfo_t info = {};
>
>  info.si_signo = sig;
>  info.si_errno = 0;
>  info.si_code = code;
>  info.si_addr = addr;
> -queue_signal(env, sig, QEMU_SI_FAULT, );
> +queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, );
>  }
>
>  static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
> diff --git a/linux-user/signal.c b/linux-user/signal.c
> index c9527adfa3..f78f7fc476 100644
> --- a/linux-user/signal.c
> +++ b/linux-user/signal.c
> @@ -623,7 +623,6 @@ void signal_init(void)
>  void force_sig(int sig)
>  {
>  CPUState *cpu = thread_cpu;
> -CPUArchState *env = cpu_env(cpu);
>  target_siginfo_t info = {};
>
>  info.si_signo = sig;
> @@ -631,7 +630,7 @@ void force_sig(int sig)
>  info.si_code = TARGET_SI_KERNEL;
>  info._sifields._kill._pid = 0;
>  info._sifields._kill._uid = 0;
> -queue_signal(env, info.si_signo, QEMU_SI_KILL, );
> +queue_signal(cpu_env(cpu), info.si_signo, QEMU_SI_KILL, );
>  }
>
>  /*
> @@ -641,14 +640,13 @@ void force_sig(int sig)
>  void force_sig_fault(int sig, int code, abi_ulong addr)
>  {
>  CPUState *cpu = thread_cpu;
> -CPUArchState *env = cpu_env(cpu);
>  target_siginfo_t info = {};
>
>  info.si_signo = sig;
>  info.si_errno = 0;
>  info.si_code = code;
>  info._sifields._sigfault._addr = addr;
> -queue_signal(env, sig, QEMU_SI_FAULT, );
> +queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, );
>  }
>
>  /* Force a SIGSEGV if we couldn't write to memory trying to set
> --
> 2.41.0
>
>


Re: [PATCH v2 08/14] meson: Link with libinotify on FreeBSD

2024-02-07 Thread Warner Losh
On Wed, Feb 7, 2024 at 9:38 AM Alex Bennée  wrote:

> From: Ilya Leoshkevich 
>
> make vm-build-freebsd fails with:
>
> ld: error: undefined symbol: inotify_init1
> >>> referenced by filemonitor-inotify.c:183
> (../src/util/filemonitor-inotify.c:183)
> >>>   util_filemonitor-inotify.c.o:(qemu_file_monitor_new)
> in archive libqemuutil.a
>
> On FreeBSD the inotify functions are defined in libinotify.so. Add it
> to the dependencies.
>
> Signed-off-by: Ilya Leoshkevich 
> Reviewed-by: Thomas Huth 
> Message-Id: <20240206002344.12372-5-...@linux.ibm.com>
> Signed-off-by: Alex Bennée 
> ---
>  meson.build  | 23 +++
>  util/meson.build |  6 +++++-
>  2 files changed, 24 insertions(+), 5 deletions(-)
>

Reviewed-by: Warner Losh 



> diff --git a/meson.build b/meson.build
> index b5d6dc94a83..e5d6f2d057e 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -2384,6 +2384,22 @@ else
>  endif
>  config_host_data.set('CONFIG_ASAN_IFACE_FIBER', have_asan_fiber)
>
> +have_inotify_init = cc.has_header_symbol('sys/inotify.h', 'inotify_init')
> +have_inotify_init1 = cc.has_header_symbol('sys/inotify.h',
> 'inotify_init1')
> +inotify = not_found
> +if (have_inotify_init or have_inotify_init1) and host_os == 'freebsd'
> +  # libinotify-kqueue
> +  inotify = cc.find_library('inotify')
> +  if have_inotify_init
> +have_inotify_init = inotify.found()
> +  endif
> +  if have_inotify_init1
> +have_inotify_init1 = inotify.found()
> +  endif
> +endif
> +config_host_data.set('CONFIG_INOTIFY', have_inotify_init)
> +config_host_data.set('CONFIG_INOTIFY1', have_inotify_init1)
> +
>  # has_header_symbol
>  config_host_data.set('CONFIG_BLKZONED',
>   cc.has_header_symbol('linux/blkzoned.h',
> 'BLKOPENZONE'))
> @@ -2400,10 +2416,6 @@ config_host_data.set('CONFIG_FIEMAP',
>  config_host_data.set('CONFIG_GETRANDOM',
>   cc.has_function('getrandom') and
>   cc.has_header_symbol('sys/random.h',
> 'GRND_NONBLOCK'))
> -config_host_data.set('CONFIG_INOTIFY',
> - cc.has_header_symbol('sys/inotify.h',
> 'inotify_init'))
> -config_host_data.set('CONFIG_INOTIFY1',
> - cc.has_header_symbol('sys/inotify.h',
> 'inotify_init1'))
>  config_host_data.set('CONFIG_PRCTL_PR_SET_TIMERSLACK',
>   cc.has_header_symbol('sys/prctl.h',
> 'PR_SET_TIMERSLACK'))
>  config_host_data.set('CONFIG_RTNETLINK',
> @@ -4407,6 +4419,9 @@ summary_info += {'libudev':   libudev}
>  summary_info += {'FUSE lseek':fuse_lseek.found()}
>  summary_info += {'selinux':   selinux}
>  summary_info += {'libdw': libdw}
> +if host_os == 'freebsd'
> +  summary_info += {'libinotify-kqueue': inotify}
> +endif
>  summary(summary_info, bool_yn: true, section: 'Dependencies')
>
>  if host_arch == 'unknown'
> diff --git a/util/meson.build b/util/meson.build
> index af3bf5692d8..0ef9886be04 100644
> --- a/util/meson.build
> +++ b/util/meson.build
> @@ -104,7 +104,11 @@ if have_block
>util_ss.add(files('throttle.c'))
>util_ss.add(files('timed-average.c'))
>if config_host_data.get('CONFIG_INOTIFY1')
> -util_ss.add(files('filemonitor-inotify.c'))
> +freebsd_dep = []
> +if host_os == 'freebsd'
> +  freebsd_dep = inotify
> +endif
> +util_ss.add(files('filemonitor-inotify.c'), freebsd_dep)
>else
>  util_ss.add(files('filemonitor-stub.c'))
>endif
> --
> 2.39.2
>
>


Re: [PATCH v2 06/14] tests/vm/freebsd: Reload the sshd configuration

2024-02-07 Thread Warner Losh
On Wed, Feb 7, 2024 at 9:38 AM Alex Bennée  wrote:

> From: Ilya Leoshkevich 
>
> After console_sshd_config(), the SSH server needs to be nudged to pick
> up the new configs. The scripts for the other BSD flavors already do
> this with a reboot, but a simple reload is sufficient.
>
> Reviewed-by: Thomas Huth 
> Signed-off-by: Ilya Leoshkevich 
> Message-Id: <20240206002344.12372-3-...@linux.ibm.com>
> Signed-off-by: Alex Bennée 
> ---
>  tests/vm/freebsd | 1 +
>  1 file changed, 1 insertion(+)
>

Reviewed-by: Warner Losh 



> diff --git a/tests/vm/freebsd b/tests/vm/freebsd
> index b581bd17fb7..1247f40a385 100755
> --- a/tests/vm/freebsd
> +++ b/tests/vm/freebsd
> @@ -108,6 +108,7 @@ class FreeBSDVM(basevm.BaseVM):
>  prompt = "root@freebsd:~ #"
>  self.console_ssh_init(prompt, "root", self._config["root_pass"])
>  self.console_sshd_config(prompt)
> +self.console_wait_send(prompt, "service sshd reload\n")
>
>  # setup virtio-blk #1 (tarfile)
>  self.console_wait(prompt)
> --
> 2.39.2
>
>


Re: [PATCH v2 07/14] test-util-filemonitor: Adapt to the FreeBSD inotify rename semantics

2024-02-07 Thread Warner Losh
On Wed, Feb 7, 2024 at 9:38 AM Alex Bennée  wrote:

> From: Ilya Leoshkevich 
>
> Unlike on Linux, on FreeBSD renaming a file when the destination
> already exists results in an IN_DELETE event for that existing file:
>
> $ FILEMONITOR_DEBUG=1 build/tests/unit/test-util-filemonitor
> Rename /tmp/test-util-filemonitor-K13LI2/fish/one.txt ->
> /tmp/test-util-filemonitor-K13LI2/two.txt
> Event id=2 event=2 file=one.txt
> Queue event id 2 event 2 file one.txt
> Queue event id 1 event 2 file two.txt
> Queue event id 10002 event 2 file two.txt
> Queue event id 1 event 0 file two.txt
> Queue event id 10002 event 0 file two.txt
> Event id=1 event=0 file=two.txt
> Expected event 0 but got 2
>
> This difference in behavior is not expected to break the real users, so
> teach the test to accept it.
>

Reviewed-by: Warner Losh 


Suggested-by: Daniel P. Berrange 
> Signed-off-by: Ilya Leoshkevich 
> Message-Id: <20240206002344.12372-4-...@linux.ibm.com>
> Signed-off-by: Alex Bennée 
> ---
>  tests/unit/test-util-filemonitor.c | 8 
>  1 file changed, 8 insertions(+)
>
> diff --git a/tests/unit/test-util-filemonitor.c
> b/tests/unit/test-util-filemonitor.c
> index a22de275955..02e67fc96ac 100644
> --- a/tests/unit/test-util-filemonitor.c
> +++ b/tests/unit/test-util-filemonitor.c
> @@ -360,6 +360,14 @@ test_file_monitor_events(void)
>  { .type = QFILE_MONITOR_TEST_OP_EVENT,
>.filesrc = "one.txt", .watchid = ,
>.eventid = QFILE_MONITOR_EVENT_DELETED },
> +#ifdef __FreeBSD__
> +{ .type = QFILE_MONITOR_TEST_OP_EVENT,
> +  .filesrc = "two.txt", .watchid = ,
> +  .eventid = QFILE_MONITOR_EVENT_DELETED },
> +{ .type = QFILE_MONITOR_TEST_OP_EVENT,
> +  .filesrc = "two.txt", .watchid = ,
> +  .eventid = QFILE_MONITOR_EVENT_DELETED },
> +#endif
>  { .type = QFILE_MONITOR_TEST_OP_EVENT,
>.filesrc = "two.txt", .watchid = ,
>.eventid = QFILE_MONITOR_EVENT_CREATED },
> --
> 2.39.2
>
>


Re: [PATCH 3/3] meson: Disable CONFIG_NOTIFY1 on FreeBSD

2024-02-05 Thread Warner Losh
On Wed, Jan 31, 2024 at 9:42 AM Daniel P. Berrangé 
wrote:

> On Wed, Jan 31, 2024 at 05:24:10PM +0100, Philippe Mathieu-Daudé wrote:
> > Hi,
> >
> > Warner, do you remember what this is about?
> >
> > (
> https://cgit.freebsd.org/ports/commit/emulators/qemu-devel/files/patch-util_meson.build?id=2ab482e2c8f51eae7ffd747685b7f181fe1b3809
> > isn't very verbose).
>
> That's simply going to workaround our incomplete feature
> check. We look for sys/inotify.h and if present, we
> assume that is in the C library. That's true on Linux,
> but not true on *BSD, hence the undefined symbol.
>
> We need to augment the header file check with a linker
> symbol check for the C library.
>
> If we wanted to also check for -linotify that'd make
> it portable to BSD, but not the behaviour difference
> mentioned below.
>
> >
> > On 25/1/24 20:48, Ilya Leoshkevich wrote:
> > > make vm-build-freebsd fails with:
> > >
> > >  ld: error: undefined symbol: inotify_init1
> > >  >>> referenced by filemonitor-inotify.c:183
> (../src/util/filemonitor-inotify.c:183)
> > >  >>>
>  util_filemonitor-inotify.c.o:(qemu_file_monitor_new) in archive
> libqemuutil.a
> > >
> > > On FreeBSD inotify functions are defined in libinotify.so, so it might
> > > be tempting to add it to the dependencies. Doing so, however, reveals
> > > that this library handles rename events differently from Linux:
> > >
> > >  $ FILEMONITOR_DEBUG=1 build/tests/unit/test-util-filemonitor
> > >  Rename /tmp/test-util-filemonitor-K13LI2/fish/one.txt ->
> /tmp/test-util-filemonitor-K13LI2/two.txt
> > >  Event id=2 event=2 file=one.txt
> > >  Queue event id 2 event 2 file one.txt
> > >  Queue event id 1 event 2 file two.txt
> > >  Queue event id 10002 event 2 file two.txt
> > >  Queue event id 1 event 0 file two.txt
> > >  Queue event id 10002 event 0 file two.txt
> > >  Event id=1 event=0 file=two.txt
> > >  Expected event 0 but got 2
>
> Interesting. So In the "Rename" test, the destination already exists.
>
> BSD is thus reporting that 'two.txt' is deleted, before being (re)created
> Linux is only reporting 'two.txt' is created.
>
> I don't think we can easily paper over this difference. The easiest is
> probably to conditionalize the test
>
>  git diff
> diff --git a/tests/unit/test-util-filemonitor.c
> b/tests/unit/test-util-filemonitor.c
> index a22de27595..c3b2006365 100644
> --- a/tests/unit/test-util-filemonitor.c
> +++ b/tests/unit/test-util-filemonitor.c
> @@ -281,6 +281,14 @@ test_file_monitor_events(void)
>  { .type = QFILE_MONITOR_TEST_OP_EVENT,
>.filesrc = "one.txt", .watchid = ,
>.eventid = QFILE_MONITOR_EVENT_DELETED },
> +#ifdef __FreeBSD__
> +{ .type = QFILE_MONITOR_TEST_OP_EVENT,
> +  .filesrc = "two.txt", .watchid = ,
> +  .eventid = QFILE_MONITOR_EVENT_DELETED },
> +{ .type = QFILE_MONITOR_TEST_OP_EVENT,
> +  .filesrc = "two.txt", .watchid = ,
> +  .eventid = QFILE_MONITOR_EVENT_DELETED },
> +#endif
>  { .type = QFILE_MONITOR_TEST_OP_EVENT,
>.filesrc = "two.txt", .watchid = ,
>.eventid = QFILE_MONITOR_EVENT_CREATED },
>

I agree this is likely the best course of action. Has anybody filed a bug
at https://bugs.freebsd.org?

Warner


Re: [PATCH] {linux,bsd}-user: Fail mmap() if size doesn't fit into host's size_t

2024-01-29 Thread Warner Losh
On Thu, Jan 25, 2024 at 1:07 PM Ilya Leoshkevich  wrote:

> s390x's branch-relative-long test fails with the following error
> message on 32-bit hosts:
>
> qemu-s390x: ../accel/tcg/user-exec.c:493: page_set_flags: Assertion
> `last <= GUEST_ADDR_MAX' failed.
>
> The root cause is that the size passed to mmap() by this test does not
> fit into 32 bits and gets truncated. Since there is no chance for such
> mmap() to succeed, detect this condition and fail the mmap() right away.
>
> Signed-off-by: Ilya Leoshkevich 
> ---
>  bsd-user/mmap.c   | 4 
>  linux-user/mmap.c | 4 
>  2 files changed, 8 insertions(+)
>
> diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
> index 3ef11b28079..5dc327d0ad3 100644
> --- a/bsd-user/mmap.c
> +++ b/bsd-user/mmap.c
> @@ -256,6 +256,10 @@ static abi_ulong mmap_find_vma_aligned(abi_ulong
> start, abi_ulong size,
>
>  size = HOST_PAGE_ALIGN(size);
>
> +if (size != (size_t)size) {
> +return (abi_ulong)(-1);
> +}
> +
>  if (reserved_va) {
>  return mmap_find_vma_reserved(start, size,
>  (alignment != 0 ? 1 << alignment :
>

Reviewed-by: Warner Losh 

Seems good to me..  I can queue it to this month's landing code, unless
Richard beats me to it.

Warner


Re: Qemu resets terminal to crazy defaults

2023-12-19 Thread Warner Losh
On Tue, Dec 19, 2023, 1:55 PM Peter Maydell 
wrote:

> On Tue, 19 Dec 2023 at 19:40, Fabiano Rosas  wrote:
> >
> > Dave Blanchard  writes:
> >
> > > Hello all, can you please help me to understand what Qemu is doing
> here?
> > >
> > > When connecting to the guest for example using a serial/tcp/telnet
> link, some kind of code is being immediately transmitted over the link
> which screws up my Xterm terminal settings, including changing the text
> cursor shape and most notably, disabling wraparound of long lines, so that
> they get truncated at the edge of the window instead.
> > >
> > > Can this behavior be disabled by command line, and if not, what is the
> code doing exactly so I can know where to disable it? I tried disabling all
> calls to tcsetattr() but that had no effect.
>
> > I looked into the automatic margins issue a long time ago and I seem to
> > remember it was caused by the firmware (SeaBIOS) configuring the
> > terminal and QEMU just never returning it to the original state. I
> > eventually gave up trying to fix it because I was having trouble finding
> > a reliable point in QEMU shutdown sequence to enable the capability
> > back. Nowadays I just run 'tput smam' after quitting QEMU.
>
> To check whether this is happening because of the BIOS (or other
> guest code) vs QEMU itself, you can try running QEMU in a configuration
> where it doesn't run any BIOS code. One I happen to know offhand
> is an arm one:
>
>qemu-system-aarch64 -M virt -serial stdio
>
> This won't print anything, because we haven't loaded any guest
> code at all and there's no default BIOS on this machine type.
> (The emulated CPU is sat in effectively a tight loop taking
> exceptions.) If that messes up the terminal settings, then it's
> likely being done by something inside QEMU. If it doesn't, then
> it sounds like as you say it'll be because of the SeaBIOS
> firmware writing stuff to the terminal.
>
> (There might be a way to run the x86 PC machine without it
> running a BIOS, for a similar test, but I don't know if there
> is or how to do it off the top of my head.)
>
> I do know that QEMU doesn't clean up things the guest does
> to the terminal, because for instance if you have a serial
> terminal and the guest puts it into "emit boldface/bright",
> that doesn't go back to normal non-bold text when QEMU exits.
> (It would be nice if it did do that...)
>

It would be nice indeed. Trouble is quarrying the state beforehand to know
what to reset by random software producing effectively random bytes..

ESC c

is the reset sequence as well...but that's likely too big a hammer.

Warner

thanks
> -- PMM
>
>


Re: Qemu resets terminal to crazy defaults

2023-12-19 Thread Warner Losh
On Tue, Dec 19, 2023, 12:20 PM Dave Blanchard  wrote:

> Hello all, can you please help me to understand what Qemu is doing here?
>
> When connecting to the guest for example using a serial/tcp/telnet link,
> some kind of code is being immediately transmitted over the link which
> screws up my Xterm terminal settings, including changing the text cursor
> shape and most notably, disabling wraparound of long lines, so that they
> get truncated at the edge of the window instead.
>
> Can this behavior be disabled by command line, and if not, what is the
> code doing exactly so I can know where to disable it? I tried disabling all
> calls to tcsetattr() but that had no effect.
>

Is it qemu doing this or your boit loader. What you describe has nothing to
do with tcsetattr, and everything to do with the state of your terminal
emulation... which is controlled with escape sequences...

Warner


Dave
>
>


Re: [PATCH 24/24] target: Restrict 'sysemu/reset.h' to system emulation

2023-12-11 Thread Warner Losh
On Mon, Dec 11, 2023 at 2:23 PM Philippe Mathieu-Daudé 
wrote:

> vCPU "reset" is only possible with system emulation.
>
> Signed-off-by: Philippe Mathieu-Daudé 
> ---
>  target/i386/cpu.c  | 2 +-
>  target/loongarch/cpu.c | 2 ++
>  2 files changed, 3 insertions(+), 1 deletion(-)
>

Reviewed-by: Warner Losh 


Re: [PATCH 14/24] gdbstub: Include missing 'hw/core/cpu.h' header

2023-12-11 Thread Warner Losh
On Mon, Dec 11, 2023 at 2:22 PM Philippe Mathieu-Daudé 
wrote:

> Functions such gdb_get_cpu_pid() dereference CPUState so
> require the structure declaration from "hw/core/cpu.h":
>
>   static uint32_t gdb_get_cpu_pid(CPUState *cpu)
>   {
> ...
> return cpu->cluster_index + 1;
>   }
>
> Signed-off-by: Philippe Mathieu-Daudé 
> ---
>  gdbstub/gdbstub.c | 1 +
>  1 file changed, 1 insertion(+)
>

Reviewed-by: Warner Losh 


Re: [PATCH 02/24] exec: Expose 'target_page.h' API to user emulation

2023-12-11 Thread Warner Losh
On Mon, Dec 11, 2023 at 2:20 PM Philippe Mathieu-Daudé 
wrote:

> User-only objects might benefit from the "exec/target_page.h"
> API, which allows to build some objects once for all targets.
>
> Signed-off-by: Philippe Mathieu-Daudé 
> ---
>  meson.build  |  2 +-
>  page-target.c| 43 +++
>  system/physmem.c | 35 ---
>  3 files changed, 44 insertions(+), 36 deletions(-)
>  create mode 100644 page-target.c
>

Reviewed-by: Warner Losh 


Re: [PATCH 01/24] exec: Include 'cpu.h' before validating CPUArchState placement

2023-12-11 Thread Warner Losh
On Mon, Dec 11, 2023 at 2:53 PM Warner Losh  wrote:

>
>
> On Mon, Dec 11, 2023 at 2:20 PM Philippe Mathieu-Daudé 
> wrote:
>
>> CPUArchState 'env' field is defined within the ArchCPU structure,
>> so we need to include each target "cpu.h" header which defines it.
>>
>> Signed-off-by: Philippe Mathieu-Daudé 
>> ---
>>  include/exec/cpu-all.h | 9 +
>>  1 file changed, 5 insertions(+), 4 deletions(-)
>>
>
> Signed-off-by: Warner Losh 
>

Brain f I meant:

Reviewed-by: Warner Losh 


Re: [PATCH 01/24] exec: Include 'cpu.h' before validating CPUArchState placement

2023-12-11 Thread Warner Losh
On Mon, Dec 11, 2023 at 2:20 PM Philippe Mathieu-Daudé 
wrote:

> CPUArchState 'env' field is defined within the ArchCPU structure,
> so we need to include each target "cpu.h" header which defines it.
>
> Signed-off-by: Philippe Mathieu-Daudé 
> ---
>  include/exec/cpu-all.h | 9 +
>  1 file changed, 5 insertions(+), 4 deletions(-)
>

Signed-off-by: Warner Losh 


Re: QEMU Summit Minutes 2023

2023-11-29 Thread Warner Losh
On Wed, Nov 29, 2023 at 8:44 AM Daniel P. Berrangé 
wrote:

> On Tue, Nov 28, 2023 at 06:54:42PM +0100, Cédric Le Goater wrote:
> > On 11/21/23 18:11, Alex Bennée wrote:
> > > Peter Maydell  writes:
> > >
> > > > QEMU Summit Minutes 2023
> > > > 
> > > >
> > > > As usual, we held a QEMU Summit meeting at KVM Forum.  This is an
> > > > invite-only meeting for the most active maintainers and
> submaintainers
> > > > in the project, and we discuss various project-wide issues, usually
> > > > process stuff. We then post the minutes of the meeting to the list as
> > > > a jumping off point for wider discussion and for those who weren't
> > > > able to attend.
> > > >
> > > 
> > > >
> > > > Topic 2: Are we happy with the email workflow?
> > > > ==
> > > >
> > > > This was a topic to see if there was any consensus among maintainers
> > > > about the long-term acceptability of sticking with email for patch
> > > > submission and review -- in five years' time, if we're still doing it
> > > > the same way, how would we feel about it?
> > > >
> > > > One area where we did get consensus was that now that we're doing CI
> > > > on gitlab we can change pull requests from maintainers from via-email
> > > > to gitlab merge requests. This would hopefully mean that instead of
> > > > the release-manager having to tell gitlab to do a merge and then
> > > > reporting back the results of any CI failures, the maintainer
> > > > could directly see the CI results and deal with fixing up failures
> > > > and resubmitting without involving the release manager. (This
> > > > may have the disbenefit that there isn't a single person any
> > > > more who looks at all the CI results and gets a sense of whether
> > > > particular test cases have pre-existing intermittent failures.)
> > >
> > > If we are keen to start processing merge requests for the 9.0 release
> we
> > > really should consider how it is going to work before we open up the
> > > taps post 8.2-final going out.
> > >
> > > Does anyone want to have a go at writing an updated process for
> > > docs/devel/submitting-a-pull-request.rst (or I guess merge-request) so
> > > we can discuss it and be ready early in the cycle? Ideally someone who
> > > already has experience with the workflow with other gitlab hosted
> > > projects.
>
> If no one else beats me to it, I can try and write up something,
> since I'm pretty familiar with gitlab PR from libvirt & other
> projects.
>
> > Reading the Topic 2 paragraph above, I understand that a maintainer
> > of a subsystem would be able to merge its '-next' branch in the main
> > repository when CI is all green. Correct ?
>
> A maintainer would have their own fork of qemu-project/qemu, under
> their namespace, or if there are maintainers collaborating, they
> might have a separate group nmamespace for their subsystem.
> eg qemu-block-subsys/qemu, or we could use sub-groups perhaps
> so  qemu-project/block-subsys/qemu  for official subsystem
> trees.
>
> Anyway, when a maintainer wants to merge a tree, I would expect to
> have a MR opened against 'master' in qemu-project/qemu.  The CI
> ought to then run and if it is all green, then someone would approve
> it to merge to master.
>
> > It seems to me that we should also have a group of people approving
> > the MR.
>
> Yes, while we could have one designated gate keeper approving all
> MRs, that would defeat some of the benefit of MRs. So likely would
> be good to have a pool, and also setup the config so that the owner
> of an MR is not allow to approve their own MR, to guarantee there
> is always a 2nd pair of eyes as sanity check.
>
> We might also need to consider enabling 'merge trains', so that
> we get a serialized CI run again after hte MR is approved, in
> case 'master' moved onwards since the initial CI pipeline when
> the MR was opened.
>

I'd honestly optimize for 'frequent merges of smaller things' rather than
'infrequent merges of larger things'. The latter has caused most of the
issues for me. It's harder to contribute because the overhead of doing so
is so large you want to batch everything. Let's not optimize for that
workaround for the high-friction submission process we have now. If there's
always smaller bits of work going in all the time, you'll find few commit
races... though the CI pipeline is rather large, so having a ci-staging
branch to land the MRs to that have completed CI, but not CI on the tip,
might not be bad... but the resolution of conflicts can be tricky, though
infrequent, so if a ci-staging branch bounces, all MRs would need to be
manually requeued after humans look at why and think through who needs to
talk to whom, or if it's just a 'other things landed before you could get
yours in and it's not the ci-staging being full of other people's commits
that is at fault.

Warner


> With regards,
> Daniel
> --
> |: https://berrange.com  -o-
> https://www.flickr.com/photos/dberrange :|
> 

Re: QEMU Summit Minutes 2023

2023-11-29 Thread Warner Losh
On Wed, Nov 29, 2023 at 8:33 AM Philippe Mathieu-Daudé 
wrote:

> On 28/11/23 19:06, Daniel P. Berrangé wrote:
> > On Tue, Nov 28, 2023 at 06:54:42PM +0100, Cédric Le Goater wrote:
>
> > Anyway, when a maintainer wants to merge a tree, I would expect to
> > have a MR opened against 'master' in qemu-project/qemu.  The CI
> > ought to then run and if it is all green, then someone would approve
> > it to merge to master.
> >
> >> It seems to me that we should also have a group of people approving
> >> the MR.
> >
> > Yes, while we could have one designated gate keeper approving all
> > MRs, that would defeat some of the benefit of MRs. So likely would
> > be good to have a pool, and also setup the config so that the owner
> > of an MR is not allow to approve their own MR, to guarantee there
> > is always a 2nd pair of eyes as sanity check.
>
> Are all our tests already on GitLab? Last time I remember Peter still
> had manual tests.
>

As a low-volume maintainer, I'd love nothing more than to push my PR
asynchronously to the release cycle. I'll get immediate yes/no feedback and
have a chance to fix the 'no' from the CI and/or reviewers. I'd know early
in the review when CI tests break that I can deal with in parallel. All as
part of the normal process. Now I have to publish in email, and push to
gitlab and it's very manual, not integrated and a large source of friction
for me as someone who does things from time to time rather than all the
time (since it's the most radically different set or processes from
anything else I contribute to). This way, I don't have to care about
freezes or whatever. During the non-freeze times it goes in once whatever
criteria are ticked (reviewers and no objections, my say so, CI working,
etc) During the freeze times the release engineer ticks another box for it
to go in... or not... and after the freeze, we'll have a battle royale of
accumulated MRs that will go in, though not all queued once since we'll
have to re-run the CI with the new changes.

And maybe we could consider just branching for release. Freeze master for
as long as it takes to branch (which needn't be tip) and then master goes
on with life and the release engineer lands bug fixes to the release branch
like we do now in frozen master. That way we don't get the big in-rush
effects when the freeze lifts. FreeBSD went to this a decade ago and makes
releases so much easier.

Warner


Re: [PATCH trivial 01/21] bsd-user: spelling fixes: necesary, agrument, undocummented

2023-11-14 Thread Warner Losh
On Tue, Nov 14, 2023 at 9:58 AM Michael Tokarev  wrote:

> Fixes: a99d74034754 "bsd-user: Implement do_obreak function"
> Fixes: 8632729060bf "bsd-user: Implement freebsd_exec_common, used in
> implementing execve/fexecve."
> Fixes: bf14f13d8be8 "bsd-user: Implement stat related syscalls"
> Cc: Stacey Son 
> Cc: Warner Losh 
> Cc: Kyle Evans 
> Signed-off-by: Michael Tokarev 
> ---
>  bsd-user/bsd-mem.h | 2 +-
>  bsd-user/freebsd/os-proc.c | 2 +-
>  bsd-user/freebsd/os-stat.h | 6 +++---
>  3 files changed, 5 insertions(+), 5 deletions(-)


Reviewed-by: Warner Losh 

These changes are fine, and won't have any hassles with merging to our
not-yet-merged branch.


Re: [PATCH 0/1] Enable -Wshadow=local

2023-10-25 Thread Warner Losh
On Wed, Oct 25, 2023, 11:31 PM Markus Armbruster  wrote:

> Requires Brian's pull request and two patches from Thomas to compile:
>
> [PULL 0/2] hex queue - GETPC() fixes, shadowing fixes
> [PATCH v2] block/snapshot: Fix compiler warning with -Wshadow=local
> [PATCH v2] migration/ram: Fix compilation with -Wshadow=local
>
> Stefan, the PR was posted a week ago; anything blocking it?
>
> Warner, I believe not waiting for your cleanup of bsd-user is fine.
> Please holler if it isn't.
>

If it's not enabled by default for Clang, then sure. It's only one small
change at this point, but i was ill for a few weeks (much longer than i
thought I'd be) and am still catching up.

Warner

 <20231019021733.2258592-1-bc...@quicinc.com>

> Based-on: <20231023175038.111607-1-th...@redhat.com>
> Based-on: <20231024092220.55305-1-th...@redhat.com>
>
> Markus Armbruster (1):
>   meson: Enable -Wshadow=local
>
>  meson.build | 1 +
>  1 file changed, 1 insertion(+)
>
> --
> 2.41.0
>
>


Re: [PATCH 1/1] meson: Enable -Wshadow=local

2023-10-25 Thread Warner Losh
On Wed, Oct 25, 2023, 11:31 PM Markus Armbruster  wrote:

> Local variables shadowing other local variables or parameters make the
> code needlessly hard to understand.  Bugs love to hide in such code.
> Evidence: commit bbde656263d (migration/rdma: Fix save_page method to
> fail on polling error).
>
> Enable -Wshadow=local to prevent such issues.  Possible thanks to
> recent cleanups.  Enabling -Wshadow would prevent more issues, but
> we're not yet ready for that.
>
> As usual, the warning is only enabled when the compiler recognizes it.
> GCC does, Clang doesn't.
>
> Some shadowed locals remain in bsd-user.  Since BSD prefers Clang,
> let's not wait for its cleanup.
>
> Signed-off-by: Markus Armbruster 
> ---
>  meson.build | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/meson.build b/meson.build
> index dcef8b1e79..89220443b8 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -462,6 +462,7 @@ warn_flags = [
>'-Wno-tautological-type-limit-compare',
>'-Wno-psabi',
>'-Wno-gnu-variable-sized-type-not-at-end',
> +  '-Wshadow=local',
>

Does this work with clang? I've not had good luck enabling it.

Warner

 ]
>
>  if targetos != 'darwin'
> --
> 2.41.0
>
>


Re: [PATCH] tests/vm/freebsd: Add additional library paths for libfdt

2023-10-16 Thread Warner Losh
On Mon, Oct 16, 2023, 10:11 AM Thomas Huth  wrote:

> libfdt is installed in /usr/local on FreeBSD, and since this
> library does not have a pkg-config file, we have to specify the
> paths manually. This way we can avoid that Meson has to recompile
> the dtc subproject each time.
>
> Signed-off-by: Thomas Huth 
> ---
>  tests/vm/freebsd | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>

Reviewed-by: Warner Losh 

diff --git a/tests/vm/freebsd b/tests/vm/freebsd
> index ac51376c82..b581bd17fb 100755
> --- a/tests/vm/freebsd
> +++ b/tests/vm/freebsd
> @@ -38,8 +38,9 @@ class FreeBSDVM(basevm.BaseVM):
>  cd $(mktemp -d /home/qemu/qemu-test.XX);
>  mkdir src build; cd src;
>  tar -xf /dev/vtbd1;
> -cd ../build
> -../src/configure --python=python3.9 {configure_opts};
> +cd ../build;
> +../src/configure --python=python3.9
> --extra-ldflags=-L/usr/local/lib \
> + --extra-cflags=-I/usr/local/include
> {configure_opts};
>  gmake --output-sync -j{jobs} {target} {verbose};
>  """
>
> --
> 2.41.0
>
>


Re: [RFC PATCH 06/11] tests/avocado: Add FreeBSD distro boot tests for ppc

2023-10-10 Thread Warner Losh
On Tue, Oct 10, 2023 at 8:23 PM Nicholas Piggin  wrote:

> On Wed Oct 11, 2023 at 7:55 AM AEST, Warner Losh wrote:
> > On Tue, Oct 10, 2023 at 1:53 AM Nicholas Piggin 
> wrote:
> >
> > > FreeBSD project provides qcow2 images that work well for testing QEMU.
> > > Add pseries tests for HPT and Radix, KVM and TCG.
> > >
> > > Other architectures could be added so this does not get a ppc_ prefix
> > > but is instead named similarly to boot_linux.
> > >
> > > Cc: Warner Losh 
> > > Signed-off-by: Nicholas Piggin 
> > >
> > > CC'ing Warner to check if it's okay for us to use these images and
> > > any comments or suggestions. avocado tests have many Linux boots so
> we'd
> > > do much better to expand test coverage by adding some other systems.
> > >
> >
> > I like this I'm a little worried at the exact hash encoded in it, but
> > since there's a checksum
> > to match, it's OK I guess. It will give this code a shelf-life of months,
> > IIRC our retention policy.
>
> The oldest 15.0 CURRENT image on there is May 1st, so ~6 months? That's
> not too bad. There are some release qcow2 images as well which sound
> like they're maintained longer-term:
>
> https://download.freebsd.org/releases/VM-IMAGES/
>
> No builds for powerpc, but those might be preferable for other targets.
>
> Another option for powerpc might be to use a release .iso. It's much
> nicer to have a qcow image already installed though. I'll tinker with
> it a bit more, but may stick with the snapshot for now.
>

I'll try to track that down. It may just be an oversight since powerpc64le
is
new.

Warner


> Thanks,
> Nick
>


Re: [RFC PATCH 06/11] tests/avocado: Add FreeBSD distro boot tests for ppc

2023-10-10 Thread Warner Losh
On Tue, Oct 10, 2023 at 6:36 PM Nicholas Piggin  wrote:

> On Wed Oct 11, 2023 at 7:55 AM AEST, Warner Losh wrote:
> > On Tue, Oct 10, 2023 at 1:53 AM Nicholas Piggin 
> wrote:
> >
> > > FreeBSD project provides qcow2 images that work well for testing QEMU.
> > > Add pseries tests for HPT and Radix, KVM and TCG.
> > >
> > > Other architectures could be added so this does not get a ppc_ prefix
> > > but is instead named similarly to boot_linux.
> > >
> > > Cc: Warner Losh 
> > > Signed-off-by: Nicholas Piggin 
> > >
> > > CC'ing Warner to check if it's okay for us to use these images and
> > > any comments or suggestions. avocado tests have many Linux boots so
> we'd
> > > do much better to expand test coverage by adding some other systems.
> > >
> >
> > I like this I'm a little worried at the exact hash encoded in it, but
> > since there's a checksum
> > to match, it's OK I guess. It will give this code a shelf-life of months,
> > IIRC our retention policy.
>
> Oh I didn't realise, I saw some 2021 dates in the directory listing but
> looks
> like they're not for the artifacts themselves.
>
> I don't suppose you know if there are any long-term artifacts kept
> around, or someone who I could ask?
>
> The downside of using short term images is that it can be harder to
> reproduce reports from others, bisect, run manual testing, etc. I think
> these would still be useful, so long as they get updated regularly.
>

Yes. We're in kind of a weird zone. powerpc64le is a new architecture, so
hasn't had artifacts for long. 14.0 is in progress, but not done yet, so
there's
no 'long term stable' version to use yet.

I don't know what our current retention policy is, hence my caution.


> >
> > Other than that, I think this is good. Not familiar enough with Avocado
> to
> > understand
> > skipping for gitlab CI, but given the extreme crunch on minutes, I think
> > that's OK.
>
> Yeah I'm not sure what the situation there is, I didn't want to add new
> tests of any significant weight yet. We could always flip it on later if
> people want it.
>

That makes sense.


> >
> > Other than one nit below which is fine if it is intentionally left behind
> > (or removed):
> >
> > Reviewed-by: Warner Losh 
> >
> > Please don't hesitate to reach out to me if this is failing. I'll act as
> a
> > backstop to get
> > it to the right people.
>
> Thanks Warner.
>

You bet. I'll give a heads up once we have 14.0 out so we can switch to
a more stable artifact.

Warner


> >
> > Warner
> >
> >
> [snip]
>
> > > +def run_pseries_test(self, force_HPT=False):
> > > +# We need zstd for all the tuxrun tests
> > > +# See
> https://github.com/avocado-framework/avocado/issues/5609
> > > +zstd = find_command('zstd', False)
> > > +if zstd is False:
> > > +self.cancel('Could not find "zstd", which is required to '
> > > +'decompress rootfs')
> > > +self.zstd = zstd
> > > +
> > > +drive_url = ('
> > >
> https://artifact.ci.freebsd.org/snapshot/15.0-CURRENT/a2440348eed75bb7682579af0905b652747fd016/powerpc/powerpc64le/disk.qcow2.zst
> > > ')
> > > +drive_hash = '8ab11a05ccab3d44215fd4667a70454ed10a203f'
> > > +drive_path_zstd = self.fetch_asset(drive_url,
> > > asset_hash=drive_hash)
> > > +drive_path = os.path.join(self.workdir, 'disk.qcow2')
> > > +# archive.zstd_uncompress(drive_path_zstd, drive_path)
> > >
> >
> > Why is this commented out? It looks like a leftover maybe?
> >
>
> Ah yes, avocado recently got zstd_uncompress but it seems not
> available for QEMU yet so we have to do it by hand. I'll remove.
>
> Thanks,
> Nick
>


Re: [RFC PATCH 06/11] tests/avocado: Add FreeBSD distro boot tests for ppc

2023-10-10 Thread Warner Losh
On Tue, Oct 10, 2023 at 1:53 AM Nicholas Piggin  wrote:

> FreeBSD project provides qcow2 images that work well for testing QEMU.
> Add pseries tests for HPT and Radix, KVM and TCG.
>
> Other architectures could be added so this does not get a ppc_ prefix
> but is instead named similarly to boot_linux.
>
> Cc: Warner Losh 
> Signed-off-by: Nicholas Piggin 
>
> CC'ing Warner to check if it's okay for us to use these images and
> any comments or suggestions. avocado tests have many Linux boots so we'd
> do much better to expand test coverage by adding some other systems.
>

I like this I'm a little worried at the exact hash encoded in it, but
since there's a checksum
to match, it's OK I guess. It will give this code a shelf-life of months,
IIRC our retention policy.

Other than that, I think this is good. Not familiar enough with Avocado to
understand
skipping for gitlab CI, but given the extreme crunch on minutes, I think
that's OK.

Other than one nit below which is fine if it is intentionally left behind
(or removed):

Reviewed-by: Warner Losh 

Please don't hesitate to reach out to me if this is failing. I'll act as a
backstop to get
it to the right people.

Warner


> ---
>  tests/avocado/boot_freebsd.py | 109 ++
>  1 file changed, 109 insertions(+)
>  create mode 100644 tests/avocado/boot_freebsd.py
>
> diff --git a/tests/avocado/boot_freebsd.py b/tests/avocado/boot_freebsd.py
> new file mode 100644
> index 00..9a499a28ad
> --- /dev/null
> +++ b/tests/avocado/boot_freebsd.py
> @@ -0,0 +1,109 @@
> +# Functional tests that boot FreeBSD in various configurations
> +#
> +# Copyright (c) 2023 IBM Corporation
> +#
> +# This work is licensed under the terms of the GNU GPL, version 2 or
> +# later. See the COPYING file in the top-level directory.
> +
> +import os
> +
> +from avocado import skipUnless
> +from avocado import skipIf
> +from avocado_qemu import QemuSystemTest
> +from avocado_qemu import wait_for_console_pattern
> +from avocado_qemu import exec_command
> +from avocado.utils import archive
> +from avocado.utils import process
> +from avocado.utils.path import find_command
> +
> +class BootFreeBSDPPC64(QemuSystemTest):
> +"""
> +:avocado: tags=arch:ppc64
> +"""
> +
> +timeout = 360
> +
> +def run_pseries_test(self, force_HPT=False):
> +# We need zstd for all the tuxrun tests
> +# See https://github.com/avocado-framework/avocado/issues/5609
> +zstd = find_command('zstd', False)
> +if zstd is False:
> +self.cancel('Could not find "zstd", which is required to '
> +'decompress rootfs')
> +self.zstd = zstd
> +
> +drive_url = ('
> https://artifact.ci.freebsd.org/snapshot/15.0-CURRENT/a2440348eed75bb7682579af0905b652747fd016/powerpc/powerpc64le/disk.qcow2.zst
> ')
> +drive_hash = '8ab11a05ccab3d44215fd4667a70454ed10a203f'
> +drive_path_zstd = self.fetch_asset(drive_url,
> asset_hash=drive_hash)
> +drive_path = os.path.join(self.workdir, 'disk.qcow2')
> +# archive.zstd_uncompress(drive_path_zstd, drive_path)
>

Why is this commented out? It looks like a leftover maybe?


> +
> +cmd = f"{self.zstd} -d {drive_path_zstd} -o {drive_path}"
> +process.run(cmd)
> +
> +drive = f"file={drive_path},format=qcow2,if=virtio"
> +
> +self.vm.set_console()
> +if force_HPT:
> +self.vm.add_args('-m', '4g')
> +else:
> +self.vm.add_args('-m', '1g')
> +self.vm.add_args('-smp', '4')
> +self.vm.add_args('-drive', drive)
> +self.vm.add_args('-net', 'nic,model=virtio')
> +self.vm.launch()
> +
> +wait_for_console_pattern(self, 'Hit [Enter] to boot immediately,
> or any other key for command prompt.')
> +if force_HPT:
> +exec_command(self, 'x')
> +wait_for_console_pattern(self, 'OK')
> +exec_command(self, 'set radix_mmu=0')
> +exec_command(self, 'boot')
> +wait_for_console_pattern(self, 'cas: selected hash MMU',
> 'panic:')
> +else:
> +exec_command(self, '')
> +wait_for_console_pattern(self, 'cas: selected radix MMU')
> +
> +wait_for_console_pattern(self, 'FreeBSD 15.0-CURRENT #0 a244034:
> Mon Sep 25 02:05:22 UTC 2023', 'panic:')
> +wait_for_console_pattern(self, 'FreeBSD/SMP: Multiprocessor
> System Detected: 4 CPUs')
> +wait_for_console_pattern(self, 'FreeBSD/powerpc (Amnesiac)
> (ttyu0)', 'panic:')
> +
> +@skipIf(os.getenv('GITLAB_CI'), 'Runnin

Re: [v3] Help wanted for enabling -Wshadow=local

2023-10-06 Thread Warner Losh
On Fri, Oct 6, 2023, 11:55 AM Thomas Huth  wrote:

> On 06/10/2023 18.18, Thomas Huth wrote:
> > On 06/10/2023 16.45, Markus Armbruster wrote:
> >> Local variables shadowing other local variables or parameters make the
> >> code needlessly hard to understand.  Bugs love to hide in such code.
> >> Evidence: "[PATCH v3 1/7] migration/rdma: Fix save_page method to fail
> >> on polling error".
> >>
> >> Enabling -Wshadow would prevent bugs like this one.  But we have to
> >> clean up all the offenders first.
> >>
> >> Quite a few people responded to my calls for help.  Thank you so much!
> >>
> >> I'm collecting patches in my git repo at
> >> https://repo.or.cz/qemu/armbru.git in branch shadow-next.  All but the
> >> last two are in a pending pull request.
> >>
> >> My test build is down to seven files with warnings.  "[PATCH v2 0/3]
> >> hexagon: GETPC() and shadowing fixes" takes care of four, but it needs a
> >> rebase.
> >>
> >> Remaining three:
> >>
> >>  In file included from ../hw/display/virtio-gpu-virgl.c:19:
> >>  ../hw/display/virtio-gpu-virgl.c: In function
> ‘virgl_cmd_submit_3d’:
> >>  /work/armbru/qemu/include/hw/virtio/virtio-gpu.h:228:16: warning:
> >> declaration of ‘s’ shadows a previous local [-Wshadow=compatible-local]
> >>228 | size_t
> >> s;   \
> >>|^
> >>  ../hw/display/virtio-gpu-virgl.c:215:5: note: in expansion of
> macro
> >> ‘VIRTIO_GPU_FILL_CMD’
> >>215 | VIRTIO_GPU_FILL_CMD(cs);
> >>| ^~~
> >>  ../hw/display/virtio-gpu-virgl.c:213:12: note: shadowed
> declaration
> >> is here
> >>213 | size_t s;
> >>|^
> >>
> >>  In file included from ../contrib/vhost-user-gpu/virgl.h:18,
> >>   from ../contrib/vhost-user-gpu/virgl.c:17:
> >>  ../contrib/vhost-user-gpu/virgl.c: In function
> ‘virgl_cmd_submit_3d’:
> >>  ../contrib/vhost-user-gpu/vugpu.h:167:16: warning: declaration of
> ‘s’
> >> shadows a previous local [-Wshadow=compatible-local]
> >>167 | size_t
> >> s;   \
> >>|^
> >>  ../contrib/vhost-user-gpu/virgl.c:203:5: note: in expansion of
> macro
> >> ‘VUGPU_FILL_CMD’
> >>203 | VUGPU_FILL_CMD(cs);
> >>| ^~
> >>  ../contrib/vhost-user-gpu/virgl.c:201:12: note: shadowed
> declaration
> >> is here
> >>201 | size_t s;
> >>|^
> >>
> >>  ../contrib/vhost-user-gpu/vhost-user-gpu.c: In function
> >> ‘vg_resource_flush’:
> >>  ../contrib/vhost-user-gpu/vhost-user-gpu.c:837:29: warning:
> >> declaration of ‘i’ shadows a previous local [-Wshadow=local]
> >>837 | pixman_image_t *i =
> >>| ^
> >>  ../contrib/vhost-user-gpu/vhost-user-gpu.c:757:9: note: shadowed
> >> declaration is here
> >>757 | int i;
> >>| ^
> >>
> >> Gerd, Marc-André, or anybody else?
> >>
> >> More warnings may lurk in code my test build doesn't compile.  Need a
> >> full CI build with -Wshadow=local to find them.  Anybody care to kick
> >> one off?
> >
> > I ran a build here (with -Werror enabled, so that it's easier to see
> where
> > it breaks):
> >
> >   https://gitlab.com/thuth/qemu/-/pipelines/1028023489
> >
> > ... but I didn't see any additional spots in the logs beside the ones
> that
> > you already listed.
>
> After adding two more patches to fix the above warnings, things look
> pretty
> good:
>
>   https://gitlab.com/thuth/qemu/-/pipelines/1028413030
>
> There are just some warnings left in the BSD code, as Warner already
> mentioned in his reply to v2 of your mail:
>
>   https://gitlab.com/thuth/qemu/-/jobs/5241420713


I think I have fixes for these. I need to merge what just landed into
bsd-user fork, rebase, test, the apply them to qemu master branch, retest
and send them off...

My illness has hung on longer than I thought so I'm still behind...

Warner


>   Thomas
>
>


Re: Wshadow: Better name for 'optarg'?

2023-10-04 Thread Warner Losh
On Wed, Oct 4, 2023, 11:44 AM Philippe Mathieu-Daudé 
wrote:

> On 4/10/23 19:35, Thomas Huth wrote:
> > On 04/10/2023 19.23, Richard Henderson wrote:
> >> On 10/4/23 03:05, Philippe Mathieu-Daudé wrote:
> >>> Hi,
> >>>
> >>> I'm getting a bunch of errors for 'optarg' declared in :
> >>
> >> I thought things like this is why we were trying -Wshadow=local.
> >>
> >> I think it's unlikely that we'll be able to prevent all such cases.
> >
> > Given the broad range of operating systems and libraries that we support
> > in QEMU, I agree with Richard - it will likely be impossible to enable
> > that option without =local by default without risking that compilation
> > breaks on some exotic systems or new versions of various libraries.
>
> -Wshadow=local doesn't seem to work here which is why I switched
> to -Wshadow. I probably misunderstood something from Markus cover
> letter. My setup is:
>
> C compiler for the host machine: clang (clang 14.0.3 "Apple clang
> version 14.0.3 (clang-1403.0.22.14.1)")
>


I had trouble with -Wshadow=local with clang too.

In general I agree not wanting it by default... but for globals defined by
the standard, we'd definitely want to fix.

Warner

I suppose we'll figure that out when eventually enabling -Wshadow=local
> on CI. Meanwhile I already cleaned the 'optarg' warnings that were
> bugging me, see:
>
> https://lore.kernel.org/qemu-devel/20231004120019.93101-1-phi...@linaro.org/
> I'll try to get -Wshadow=local, but the other series still seems a
> good cleanup, as I used more meaningful variable names.
>
> Regards,
>
> Phil.
>
>


Re: Wshadow: Better name for 'optarg'?

2023-10-04 Thread Warner Losh
On Wed, Oct 4, 2023, 4:16 AM Daniel P. Berrangé  wrote:

> On Wed, Oct 04, 2023 at 12:05:04PM +0200, Philippe Mathieu-Daudé wrote:
> > Hi,
> >
> > I'm getting a bunch of errors for 'optarg' declared in :
> >
> > NAME
> >  getopt – get option character from command line argument list
> >
> > LIBRARY
> >  Standard C Library (libc, -lc)
> >
> > SYNOPSIS
> >  #include 
> >
> >  extern char *optarg;
> >
> >
> > qom/object_interfaces.c:262:53: error: declaration shadows a variable in
> the
> > global scope [-Werror,-Wshadow]
> > ObjectOptions *user_creatable_parse_str(const char *optarg, Error **errp)
>
> snip
>
> > Do we want to clean those? Any good name suggestion?
>
> Yes.  any of "argval", "opts", "optstr", "optval".
>

For the parsing in bsd-user I just removed the variable entirely and
removed the updating of its value since the parsing code was trying to do
what getopt also did...

Warner

With regards,
> Daniel
> --
> |: https://berrange.com  -o-
> https://www.flickr.com/photos/dberrange :|
> |: https://libvirt.org -o-
> https://fstop138.berrange.com :|
> |: https://entangle-photo.org-o-
> https://www.instagram.com/dberrange :|
>
>
>


[PULL 51/51] bsd-user: Add stubs for vadvise(), sbrk() and sstk()

2023-10-03 Thread Warner Losh
The above system calls are not supported by qemu.

Signed-off-by: Warner Losh 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-24-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 18 ++
 bsd-user/freebsd/os-syscall.c | 12 
 2 files changed, 30 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index c512a4e3756..c3e72e3b866 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -431,4 +431,22 @@ static inline abi_long do_bsd_shmdt(abi_ulong shmaddr)
 return ret;
 }
 
+static inline abi_long do_bsd_vadvise(void)
+{
+/* See sys_ovadvise() in vm_unix.c */
+return -TARGET_EINVAL;
+}
+
+static inline abi_long do_bsd_sbrk(void)
+{
+/* see sys_sbrk() in vm_mmap.c */
+return -TARGET_EOPNOTSUPP;
+}
+
+static inline abi_long do_bsd_sstk(void)
+{
+/* see sys_sstk() in vm_mmap.c */
+return -TARGET_EOPNOTSUPP;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 39e66312da1..ca2f6fdb66e 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -879,6 +879,18 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_shmdt(arg1);
 break;
 
+case TARGET_FREEBSD_NR_freebsd11_vadvise:
+ret = do_bsd_vadvise();
+break;
+
+case TARGET_FREEBSD_NR_sbrk:
+ret = do_bsd_sbrk();
+break;
+
+case TARGET_FREEBSD_NR_sstk:
+ret = do_bsd_sstk();
+break;
+
 /*
  * Misc
  */
-- 
2.41.0




[PULL 49/51] bsd-user: Implement shmctl(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-22-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 39 +++
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 43 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index c362cc07a30..b82f3eaa253 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -305,4 +305,43 @@ static inline abi_long do_bsd_shmget(abi_long arg1, 
abi_ulong arg2,
 return get_errno(shmget(arg1, arg2, arg3));
 }
 
+/* shmctl(2) */
+static inline abi_long do_bsd_shmctl(abi_long shmid, abi_long cmd,
+abi_ulong buff)
+{
+struct shmid_ds dsarg;
+abi_long ret = -TARGET_EINVAL;
+
+cmd &= 0xff;
+
+switch (cmd) {
+case IPC_STAT:
+if (target_to_host_shmid_ds(, buff)) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(shmctl(shmid, cmd, ));
+if (host_to_target_shmid_ds(buff, )) {
+return -TARGET_EFAULT;
+}
+break;
+
+case IPC_SET:
+if (target_to_host_shmid_ds(, buff)) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(shmctl(shmid, cmd, ));
+break;
+
+case IPC_RMID:
+ret = get_errno(shmctl(shmid, cmd, NULL));
+break;
+
+default:
+ret = -TARGET_EINVAL;
+break;
+}
+
+return ret;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 4f67677eb92..0512d41db7c 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -867,6 +867,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_shmget(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_shmctl: /* shmctl(2) */
+ret = do_bsd_shmctl(arg1, arg2, arg3);
+break;
+
 /*
  * Misc
  */
-- 
2.41.0




[PULL 44/51] bsd-user: Implment madvise(2) to match the linux-user implementation.

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-17-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 53 +++
 bsd-user/freebsd/os-syscall.c |  4 +++
 bsd-user/syscall_defs.h   |  2 ++
 3 files changed, 59 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 16c22593bfd..b00ab3aed8e 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -129,6 +129,59 @@ static inline abi_long do_bsd_munlockall(void)
 return get_errno(munlockall());
 }
 
+/* madvise(2) */
+static inline abi_long do_bsd_madvise(abi_long arg1, abi_long arg2,
+abi_long arg3)
+{
+abi_ulong len;
+int ret = 0;
+abi_long start = arg1;
+abi_long len_in = arg2;
+abi_long advice = arg3;
+
+if (start & ~TARGET_PAGE_MASK) {
+return -TARGET_EINVAL;
+}
+if (len_in == 0) {
+return 0;
+}
+len = TARGET_PAGE_ALIGN(len_in);
+if (len == 0 || !guest_range_valid_untagged(start, len)) {
+return -TARGET_EINVAL;
+}
+
+/*
+ * Most advice values are hints, so ignoring and returning success is ok.
+ *
+ * However, some advice values such as MADV_DONTNEED, are not hints and
+ * need to be emulated.
+ *
+ * A straight passthrough for those may not be safe because qemu sometimes
+ * turns private file-backed mappings into anonymous mappings.
+ * If all guest pages have PAGE_PASSTHROUGH set, mappings have the
+ * same semantics for the host as for the guest.
+ *
+ * MADV_DONTNEED is passed through, if possible.
+ * If passthrough isn't possible, we nevertheless (wrongly!) return
+ * success, which is broken but some userspace programs fail to work
+ * otherwise. Completely implementing such emulation is quite complicated
+ * though.
+ */
+mmap_lock();
+switch (advice) {
+case MADV_DONTNEED:
+if (page_check_range(start, len, PAGE_PASSTHROUGH)) {
+ret = get_errno(madvise(g2h_untagged(start), len, advice));
+if (ret == 0) {
+page_reset_target_data(start, start + len - 1);
+}
+}
+}
+mmap_unlock();
+
+return ret;
+}
+
 /* minherit(2) */
 static inline abi_long do_bsd_minherit(abi_long addr, abi_long len,
 abi_long inherit)
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7a7ae26793f..b8c44cea0ff 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -831,6 +831,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_munlockall();
 break;
 
+case TARGET_FREEBSD_NR_madvise: /* madvise(2) */
+ret = do_bsd_madvise(arg1, arg2, arg3);
+break;
+
 case TARGET_FREEBSD_NR_minherit: /* minherit(2) */
 ret = do_bsd_minherit(arg1, arg2, arg3);
 break;
diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index ff692814333..52f84d5dd17 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -95,6 +95,8 @@ struct bsd_shm_regions {
 /*
  *  sys/mman.h
  */
+#define TARGET_MADV_DONTNEED4   /* dont need these pages */
+
 #define TARGET_FREEBSD_MAP_RESERVED0080 0x0080  /* previously misimplemented */
 /* MAP_INHERIT */
 #define TARGET_FREEBSD_MAP_RESERVED0100 0x0100  /* previously unimplemented */
-- 
2.41.0




[PULL 35/51] bsd-user: Add bsd-mem.c to meson.build

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-8-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.c   | 0
 bsd-user/meson.build | 1 +
 2 files changed, 1 insertion(+)
 create mode 100644 bsd-user/bsd-mem.c

diff --git a/bsd-user/bsd-mem.c b/bsd-user/bsd-mem.c
new file mode 100644
index 000..e69de29bb2d
diff --git a/bsd-user/meson.build b/bsd-user/meson.build
index b97fce14722..c6bfd3b2b53 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-mem.c',
   'bsd-proc.c',
   'bsdload.c',
   'elfload.c',
-- 
2.41.0




[PULL 02/51] bsd-user: Define procctl(2) related structs

2023-10-03 Thread Warner Losh
From: Stacey Son 

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 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-3-kariem.taha...@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 ddd38c13e08..a3bc738ff89 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -390,6 +390,48 @@ struct target_freebsd_flock {
 /* user: vfork(2) semantics, clear signals */
 #define TARGET_RFSPAWN (1U << 31)
 
+/*
+ * from sys/procctl.h
+ */
+#define TARGET_PROC_SPROTECT1
+#define TARGET_PROC_REAP_ACQUIRE2
+#define TARGET_PROC_REAP_RELEASE3
+#define TARGET_PROC_REAP_STATUS 4
+#define TARGET_PROC_REAP_GETPIDS5
+#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.41.0




[PULL 40/51] bsd-user: Implement mmap(2) and munmap(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-13-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 20 
 bsd-user/freebsd/os-syscall.c |  9 +
 2 files changed, 29 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index d865e0807d8..76b504f70c5 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -61,4 +61,24 @@ extern struct bsd_shm_regions bsd_shm_regions[];
 extern abi_ulong target_brk;
 extern abi_ulong initial_target_brk;
 
+/* mmap(2) */
+static inline abi_long do_bsd_mmap(void *cpu_env, abi_long arg1, abi_long arg2,
+abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6, abi_long arg7,
+abi_long arg8)
+{
+if (regpairs_aligned(cpu_env) != 0) {
+arg6 = arg7;
+arg7 = arg8;
+}
+return get_errno(target_mmap(arg1, arg2, arg3,
+ target_to_host_bitmask(arg4, mmap_flags_tbl),
+ arg5, target_arg64(arg6, arg7)));
+}
+
+/* munmap(2) */
+static inline abi_long do_bsd_munmap(abi_long arg1, abi_long arg2)
+{
+return get_errno(target_munmap(arg1, arg2));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7887ad4c0c6..b03837d032a 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -798,6 +798,15 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 /*
  * Memory management system calls.
  */
+case TARGET_FREEBSD_NR_mmap: /* mmap(2) */
+ret = do_bsd_mmap(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6, arg7,
+  arg8);
+break;
+
+case TARGET_FREEBSD_NR_munmap: /* munmap(2) */
+ret = do_bsd_munmap(arg1, arg2);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 31/51] bsd-user: Declarations for ipc_perm and shmid_ds conversion functions

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-4-kariem.taha...@gmail.com>
---
 bsd-user/qemu-bsd.h | 20 
 1 file changed, 20 insertions(+)

diff --git a/bsd-user/qemu-bsd.h b/bsd-user/qemu-bsd.h
index b93a0b7fd5b..ffc64bb244a 100644
--- a/bsd-user/qemu-bsd.h
+++ b/bsd-user/qemu-bsd.h
@@ -22,6 +22,16 @@
 
 #include 
 #include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
 /* bsd-proc.c */
 int target_to_host_resource(int code);
@@ -35,4 +45,14 @@ int host_to_target_waitstatus(int status);
 void h2g_rusage(const struct rusage *rusage,
 struct target_freebsd_rusage *target_rusage);
 
+/* bsd-mem.c */
+void target_to_host_ipc_perm__locked(struct ipc_perm *host_ip,
+struct target_ipc_perm *target_ip);
+void host_to_target_ipc_perm__locked(struct target_ipc_perm *target_ip,
+struct ipc_perm *host_ip);
+abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
+abi_ulong target_addr);
+abi_long host_to_target_shmid_ds(abi_ulong target_addr,
+struct shmid_ds *host_sd);
+
 #endif /* QEMU_BSD_H */
-- 
2.41.0




[PULL 28/51] bsd-user: Implement pdfork(2) system call.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Acked-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-29-kariem.taha...@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 0a3cd0ef57c..d6418780344 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -258,4 +258,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(, 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 4c4e773d1d3..d04712f0a7e 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -238,6 +238,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.41.0




[PULL 27/51] bsd-user: Implement rfork(2) system call.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-28-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 39 +++
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 43 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 7b2e6a9f796..0a3cd0ef57c 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -219,4 +219,43 @@ 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 cb9425c9bab..4c4e773d1d3 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -234,6 +234,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.41.0




[PULL 46/51] bsd-user: Implement do_obreak function

2023-10-03 Thread Warner Losh
From: Stacey Son 

Match linux-user, by manually applying the following commits, in order:

d28b3c90cfad1a7e211ae2bce36ecb9071086129   linux-user: Make sure initial brk(0) 
is page-aligned
15ad98536ad9410fb32ddf1ff09389b677643faa   linux-user: Fix qemu brk() to not 
zero bytes on current page
dfe49864afb06e7e452a4366051697bc4fcfc1a5   linux-user: Prohibit brk() to to 
shrink below initial heap address
eac78a4b0b7da4de2c0a297f4d528ca9cc6256a3   linux-user: Fix signed math overflow 
in brk() syscall
c6cc059eca18d9f6e4e26bb8b6d1135ddb35d81a   linux-user: Do not call get_errno() 
in do_brk()
e69e032d1a8ee8d754ca119009a3c2c997f8bb30   linux-user: Use MAP_FIXED_NOREPLACE 
for do_brk()
cb9d5d1fda0bc2312fc0c779b4ea1d7bf826f31f   linux-user: Do nothing if too small 
brk is specified
2aea137a425a87b930a33590177b04368fd7cc12   linux-user: Do not align brk with 
host page size

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-19-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 45 +++
 bsd-user/freebsd/os-syscall.c |  7 ++
 2 files changed, 52 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 0c8d96d9a43..b296c5c6f0a 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -212,4 +212,49 @@ static inline abi_long do_bsd_mincore(abi_ulong 
target_addr, abi_ulong len,
 return ret;
 }
 
+/* do_brk() must return target values and target errnos. */
+static inline abi_long do_obreak(abi_ulong brk_val)
+{
+abi_long mapped_addr;
+abi_ulong new_brk;
+abi_ulong old_brk;
+
+/* brk pointers are always untagged */
+
+/* do not allow to shrink below initial brk value */
+if (brk_val < initial_target_brk) {
+return target_brk;
+}
+
+new_brk = TARGET_PAGE_ALIGN(brk_val);
+old_brk = TARGET_PAGE_ALIGN(target_brk);
+
+/* new and old target_brk might be on the same page */
+if (new_brk == old_brk) {
+target_brk = brk_val;
+return target_brk;
+}
+
+/* Release heap if necesary */
+if (new_brk < old_brk) {
+target_munmap(new_brk, old_brk - new_brk);
+
+target_brk = brk_val;
+return target_brk;
+}
+
+mapped_addr = target_mmap(old_brk, new_brk - old_brk,
+  PROT_READ | PROT_WRITE,
+  MAP_FIXED | MAP_EXCL | MAP_ANON | MAP_PRIVATE,
+  -1, 0);
+
+if (mapped_addr == old_brk) {
+target_brk = brk_val;
+return target_brk;
+}
+
+/* For everything else, return the previous break. */
+return target_brk;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index f054241cd62..92793ab1fb3 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -855,6 +855,13 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 break;
 #endif
 
+/*
+ * Misc
+ */
+case TARGET_FREEBSD_NR_break:
+ret = do_obreak(arg1);
+break;
+
 /*
  * sys{ctl, arch, call}
  */
-- 
2.41.0




[PULL 19/51] bsd-user: Implement get_filename_from_fd.

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-20-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/meson.build |  1 +
 bsd-user/freebsd/os-proc.c   | 82 
 2 files changed, 83 insertions(+)
 create mode 100644 bsd-user/freebsd/os-proc.c

diff --git a/bsd-user/freebsd/meson.build b/bsd-user/freebsd/meson.build
index f2f047cca31..8fd6c7cfb82 100644
--- a/bsd-user/freebsd/meson.build
+++ b/bsd-user/freebsd/meson.build
@@ -1,5 +1,6 @@
 bsd_user_ss.add(files(
   'os-stat.c',
+  'os-proc.c',
   'os-sys.c',
   'os-syscall.c',
 ))
diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
new file mode 100644
index 000..2603c5c6538
--- /dev/null
+++ b/bsd-user/freebsd/os-proc.c
@@ -0,0 +1,82 @@
+/*
+ *  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 
+#include 
+#include 
+struct kinfo_proc;
+#include 
+
+#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.
+ */
+char *
+get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len);
+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, );
+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.41.0




[PULL 30/51] bsd-user: Implement struct target_shmid_ds

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-3-kariem.taha...@gmail.com>
---
 bsd-user/syscall_defs.h | 20 
 1 file changed, 20 insertions(+)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index 0e54d7df690..ff692814333 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -72,6 +72,26 @@ struct target_ipc_perm {
 #define TARGET_IPC_SET  1   /* set options */
 #define TARGET_IPC_STAT 2   /* get options */
 
+/*
+ * sys/shm.h
+ */
+struct target_shmid_ds {
+struct  target_ipc_perm shm_perm; /* peration permission structure */
+abi_ulong   shm_segsz;  /* size of segment in bytes */
+int32_t shm_lpid;   /* process ID of last shared memory op */
+int32_t shm_cpid;   /* process ID of creator */
+int32_t shm_nattch; /* number of current attaches */
+target_time_t shm_atime;  /* time of last shmat() */
+target_time_t shm_dtime;  /* time of last shmdt() */
+target_time_t shm_ctime;  /* time of last change by shmctl() */
+};
+
+#define N_BSD_SHM_REGIONS   32
+struct bsd_shm_regions {
+abi_long start;
+abi_long size;
+};
+
 /*
  *  sys/mman.h
  */
-- 
2.41.0




[PULL 15/51] bsd-user: Implement several get/set system calls:

2023-10-03 Thread Warner Losh
From: Stacey Son 

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 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-16-kariem.taha...@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 38d1324034c..6ff07c0ac36 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 5cb60862303..7565e69e76d 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -255,6 +255,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.41.0




[PULL 25/51] bsd-user: Implement pdgetpid(2) and the undocumented setugid.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-26-kariem.taha...@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 2eaba141dcd..42bdd61904b 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)
@@ -162,4 +164,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, ));
+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 -TARGET_ENOSYS;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index d614409e694..99af0f6b156 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -383,6 +383,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.41.0




[PULL 47/51] bsd-user: Implement shm_open(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Co-authored-by: Kyle Evans 

Signed-off-by: Stacey Son 
Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-20-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 25 +
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 29 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index b296c5c6f0a..f8dc943c234 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -257,4 +257,29 @@ static inline abi_long do_obreak(abi_ulong brk_val)
 return target_brk;
 }
 
+/* shm_open(2) */
+static inline abi_long do_bsd_shm_open(abi_ulong arg1, abi_long arg2,
+abi_long arg3)
+{
+int ret;
+void *p;
+
+if (arg1 == (uintptr_t)SHM_ANON) {
+p = SHM_ANON;
+} else {
+p = lock_user_string(arg1);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+}
+ret = get_errno(shm_open(p, target_to_host_bitmask(arg2, fcntl_flags_tbl),
+ arg3));
+
+if (p != SHM_ANON) {
+unlock_user(p, arg1, 0);
+}
+
+return ret;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 92793ab1fb3..0d4c3118f0d 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -843,6 +843,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_mincore(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_freebsd12_shm_open: /* shm_open(2) */
+ret = do_bsd_shm_open(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 16/51] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-17-kariem.taha...@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 6ff07c0ac36..a5f301c72ff 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(, , ));
+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(, , ));
+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 7565e69e76d..7b51f4f16e4 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -315,6 +315,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.41.0




[PULL 08/51] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-9-kariem.taha...@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 19e39a2f764..aa386ff4820 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -48,3 +48,57 @@ abi_llong host_to_target_rlim(rlim_t rlim)
 return tswap64(rlim);
 }
 
+void h2g_rusage(const struct rusage *rusage,
+struct target_freebsd_rusage *target_rusage)
+{
+__put_user(rusage->ru_utime.tv_sec, _rusage->ru_utime.tv_sec);
+__put_user(rusage->ru_utime.tv_usec, _rusage->ru_utime.tv_usec);
+
+__put_user(rusage->ru_stime.tv_sec, _rusage->ru_stime.tv_sec);
+__put_user(rusage->ru_stime.tv_usec, _rusage->ru_stime.tv_usec);
+
+__put_user(rusage->ru_maxrss, _rusage->ru_maxrss);
+__put_user(rusage->ru_idrss, _rusage->ru_idrss);
+__put_user(rusage->ru_idrss, _rusage->ru_idrss);
+__put_user(rusage->ru_isrss, _rusage->ru_isrss);
+__put_user(rusage->ru_minflt, _rusage->ru_minflt);
+__put_user(rusage->ru_majflt, _rusage->ru_majflt);
+__put_user(rusage->ru_nswap, _rusage->ru_nswap);
+__put_user(rusage->ru_inblock, _rusage->ru_inblock);
+__put_user(rusage->ru_oublock, _rusage->ru_oublock);
+__put_user(rusage->ru_msgsnd, _rusage->ru_msgsnd);
+__put_user(rusage->ru_msgrcv, _rusage->ru_msgrcv);
+__put_user(rusage->ru_nsignals, _rusage->ru_nsignals);
+__put_user(rusage->ru_nvcsw, _rusage->ru_nvcsw);
+__put_user(rusage->ru_nivcsw, _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(>wru_self, _wrusage->wru_self);
+h2g_rusage(>wru_children, _wrusage->wru_children);
+unlock_user_struct(target_wrusage, target_addr, 1);
+
+return 0;
+}
+
-- 
2.41.0




[PULL 33/51] bsd-user: Implement shm_open2(2) system call

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-6-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-misc.h| 46 +++
 bsd-user/freebsd/os-syscall.c | 22 -
 2 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/bsd-user/freebsd/os-misc.h b/bsd-user/freebsd/os-misc.h
index 8436ccb2f7d..d5e8b5484c8 100644
--- a/bsd-user/freebsd/os-misc.h
+++ b/bsd-user/freebsd/os-misc.h
@@ -24,5 +24,51 @@
 #include 
 #include 
 
+/*
+ * shm_open2 isn't exported, but the __sys_ alias is. We can use either for the
+ * static version, but to dynamically link we have to use the sys version.
+ */
+int __sys_shm_open2(const char *path, int flags, mode_t mode, int shmflags,
+const char *);
+
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
+/* shm_open2(2) */
+static inline abi_long do_freebsd_shm_open2(abi_ulong pathptr, abi_ulong flags,
+abi_long mode, abi_ulong shmflags, abi_ulong nameptr)
+{
+int ret;
+void *uname, *upath;
+
+if (pathptr == (uintptr_t)SHM_ANON) {
+upath = SHM_ANON;
+} else {
+upath = lock_user_string(pathptr);
+if (upath == NULL) {
+return -TARGET_EFAULT;
+}
+}
+
+uname = NULL;
+if (nameptr != 0) {
+uname = lock_user_string(nameptr);
+if (uname == NULL) {
+unlock_user(upath, pathptr, 0);
+return -TARGET_EFAULT;
+}
+}
+ret = get_errno(__sys_shm_open2(upath,
+target_to_host_bitmask(flags, fcntl_flags_tbl), mode,
+target_to_host_bitmask(shmflags, shmflag_flags_tbl), uname));
+
+if (upath != SHM_ANON) {
+unlock_user(upath, pathptr, 0);
+}
+if (uname != NULL) {
+unlock_user(uname, nameptr, 0);
+}
+return ret;
+}
+#endif /* __FreeBSD_version >= 1300048 */
+
 
 #endif /* OS_MISC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index d04712f0a7e..122e186b501 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -33,18 +33,14 @@
 #include "signal-common.h"
 #include "user/syscall-trace.h"
 
+/* BSD independent syscall shims */
 #include "bsd-file.h"
 #include "bsd-proc.h"
 
 /* BSD dependent syscall shims */
 #include "os-stat.h"
 #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);
+#include "os-misc.h"
 
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
@@ -65,6 +61,12 @@ safe_syscall3(ssize_t, writev, int, fd, const struct iovec 
*, iov, int, iovcnt);
 safe_syscall4(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, 
iovcnt,
 off_t, offset);
 
+/* 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);
+
 void target_set_brk(abi_ulong new_brk)
 {
 }
@@ -796,6 +798,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_freebsd_fcntl(arg1, arg2, arg3);
 break;
 
+/*
+ * Memory management system calls.
+ */
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
+case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
+ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
+break;
+#endif
 
 /*
  * sys{ctl, arch, call}
-- 
2.41.0




[PULL 36/51] bsd-user: Implement target_set_brk function in bsd-mem.c instead of os-syscall.c

2023-10-03 Thread Warner Losh
From: Stacey Son 

The definitions and variables names matches the corresponding ones in
linux-user/syscall.c, for making later implementation of do_obreak easier

Co-authored-by: Mikaël Urankar 
Signed-off-by: Mikaël Urankar 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-9-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.c| 32 
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/bsd-user/bsd-mem.c b/bsd-user/bsd-mem.c
index e69de29bb2d..8834ab2e588 100644
--- a/bsd-user/bsd-mem.c
+++ b/bsd-user/bsd-mem.c
@@ -0,0 +1,32 @@
+/*
+ *  memory management system conversion routines
+ *
+ *  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/>.
+ */
+#include "qemu/osdep.h"
+#include "qemu.h"
+#include "qemu-bsd.h"
+
+struct bsd_shm_regions bsd_shm_regions[N_BSD_SHM_REGIONS];
+
+abi_ulong target_brk;
+abi_ulong initial_target_brk;
+
+void target_set_brk(abi_ulong new_brk)
+{
+target_brk = TARGET_PAGE_ALIGN(new_brk);
+initial_target_brk = target_brk;
+}
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 5fb42b2c218..c9d34b59bbe 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -67,10 +67,6 @@ safe_syscall4(pid_t, wait4, pid_t, wpid, int *, status, int, 
options,
 safe_syscall6(pid_t, wait6, idtype_t, idtype, id_t, id, int *, status, int,
 options, struct __wrusage *, wrusage, siginfo_t *, infop);
 
-void target_set_brk(abi_ulong new_brk)
-{
-}
-
 /*
  * errno conversion.
  */
-- 
2.41.0




[PULL 24/51] bsd-user: Implement setloginclass(2) and getloginclass(2) system calls.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-25-kariem.taha...@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 04bce755e58..2eaba141dcd 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -130,4 +130,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(VERIFY_WRITE, arg1, arg2, 0);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(getloginclass(p, arg2));
+unlock_user(p, arg1, arg2);
+
+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 55e68e48159..d614409e694 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -375,6 +375,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.41.0




[PULL 48/51] bsd-user: Implement shm_unlink(2) and shmget(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-21-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 23 +++
 bsd-user/freebsd/os-syscall.c |  8 
 2 files changed, 31 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index f8dc943c234..c362cc07a30 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -282,4 +282,27 @@ static inline abi_long do_bsd_shm_open(abi_ulong arg1, 
abi_long arg2,
 return ret;
 }
 
+/* shm_unlink(2) */
+static inline abi_long do_bsd_shm_unlink(abi_ulong arg1)
+{
+int ret;
+void *p;
+
+p = lock_user_string(arg1);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(shm_unlink(p)); /* XXX path(p)? */
+unlock_user(p, arg1, 0);
+
+return ret;
+}
+
+/* shmget(2) */
+static inline abi_long do_bsd_shmget(abi_long arg1, abi_ulong arg2,
+abi_long arg3)
+{
+return get_errno(shmget(arg1, arg2, arg3));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 0d4c3118f0d..4f67677eb92 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -859,6 +859,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 break;
 #endif
 
+case TARGET_FREEBSD_NR_shm_unlink: /* shm_unlink(2) */
+ret = do_bsd_shm_unlink(arg1);
+break;
+
+case TARGET_FREEBSD_NR_shmget: /* shmget(2) */
+ret = do_bsd_shmget(arg1, arg2, arg3);
+break;
+
 /*
  * Misc
  */
-- 
2.41.0




[PULL 05/51] bsd-user: add extern declarations for bsd-proc.c conversion functions

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-6-kariem.taha...@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 000..b93a0b7fd5b
--- /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 
+#include 
+
+/* 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.41.0




[PULL 39/51] bsd-user: Introduce bsd-mem.h to the source tree

2023-10-03 Thread Warner Losh
From: Stacey Son 

Preserve the copyright notice and help with the 'Author' info for
subsequent changes to the file.

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-12-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 64 +++
 bsd-user/freebsd/os-syscall.c |  1 +
 2 files changed, 65 insertions(+)
 create mode 100644 bsd-user/bsd-mem.h

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
new file mode 100644
index 000..d865e0807d8
--- /dev/null
+++ b/bsd-user/bsd-mem.h
@@ -0,0 +1,64 @@
+/*
+ *  memory management system call shims and definitions
+ *
+ *  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/>.
+ */
+
+/*
+ * Copyright (c) 1982, 1986, 1993
+ *  The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *may be used to endorse or promote products derived from this software
+ *without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef BSD_USER_BSD_MEM_H
+#define BSD_USER_BSD_MEM_H
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "qemu-bsd.h"
+
+extern struct bsd_shm_regions bsd_shm_regions[];
+extern abi_ulong target_brk;
+extern abi_ulong initial_target_brk;
+
+#endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index c9d34b59bbe..7887ad4c0c6 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -35,6 +35,7 @@
 
 /* BSD independent syscall shims */
 #include "bsd-file.h"
+#include "bsd-mem.h"
 #include "bsd-proc.h"
 
 /* BSD dependent syscall shims */
-- 
2.41.0




[PULL 32/51] bsd-user: Introduce freebsd/os-misc.h to the source tree

2023-10-03 Thread Warner Losh
From: Stacey Son 

To preserve the copyright notice and help with the 'Author' info for
subsequent changes to the file.

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-5-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-misc.h | 28 
 1 file changed, 28 insertions(+)
 create mode 100644 bsd-user/freebsd/os-misc.h

diff --git a/bsd-user/freebsd/os-misc.h b/bsd-user/freebsd/os-misc.h
new file mode 100644
index 000..8436ccb2f7d
--- /dev/null
+++ b/bsd-user/freebsd/os-misc.h
@@ -0,0 +1,28 @@
+/*
+ *  miscellaneous FreeBSD system call shims
+ *
+ *  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 OS_MISC_H
+#define OS_MISC_H
+
+#include 
+#include 
+#include 
+
+
+#endif /* OS_MISC_H */
-- 
2.41.0




[PULL 03/51] bsd-user: Implement host_to_target_siginfo.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Used in wait6 system call

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-4-kariem.taha...@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 c044e811653..77d7c7a78b7 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 b6beab659e1..ea82241b70b 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.41.0




[PULL 10/51] bsd-user: Get number of cpus.

2023-10-03 Thread Warner Losh
From: Kyle Evans 

Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-11-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.c | 24 
 bsd-user/bsd-proc.h |  2 ++
 2 files changed, 26 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 19f6efe1f78..ca3c1bf94f4 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -119,3 +119,27 @@ int host_to_target_waitstatus(int status)
 return status;
 }
 
+int bsd_get_ncpu(void)
+{
+int ncpu = -1;
+cpuset_t mask;
+
+CPU_ZERO();
+
+if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
+   ) == 0) {
+ncpu = CPU_COUNT();
+}
+
+if (ncpu == -1) {
+ncpu = sysconf(_SC_NPROCESSORS_ONLN);
+}
+
+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 048773a75dd..b6225e520ea 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.41.0




[PULL 21/51] bsd-user: Implement procctl(2) along with necessary conversion functions.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Implement t2h_procctl_cmd, h2t_reaper_status, h2t_reaper_pidinfo and h2t/t2h 
reaper_kill conversion functions.

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-22-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.c| 223 ++
 bsd-user/freebsd/os-syscall.c |   3 +
 2 files changed, 226 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index 12d78b7fc98..4e67ae4d56c 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -255,3 +255,226 @@ execve_end:
 return ret;
 }
 
+#include 
+
+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, _rs->rs_flags);
+__put_user(host_rs->rs_children, _rs->rs_children);
+__put_user(host_rs->rs_descendants, _rs->rs_descendants);
+__put_user(host_rs->rs_reaper, _rs->rs_reaper);
+__put_user(host_rs->rs_pid, _rs->rs_pid);
+unlock_user_struct(target_rs, target_rs_addr, 1);
+
+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, _rk->rk_sig);
+__get_user(host_rk->rk_flags, _rk->rk_flags);
+__get_user(host_rk->rk_subtree, _rk->rk_subtree);
+__get_user(host_rk->rk_killed, _rk->rk_killed);
+__get_user(host_rk->rk_fpid, _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, _rk->rk_sig);
+__put_user(host_rk->rk_flags, _rk->rk_flags);
+__put_user(host_rk->rk_subtree, _rk->rk_subtree);
+__put_user(host_rk->rk_killed, _rk->rk_killed);
+__put_user(host_rk->rk_fpid, _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, _pi->pi_pid);
+__put_user(host_pi->pi_subtree, _pi->pi_subtree);
+__put_user(host_pi->pi_flags, _pi->pi_flags);
+unlock_user_struct(target_pi, target_pi_addr, 1);
+
+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;
+g_autofree 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, _cmd);
+if (error) {
+return error;
+}
+switch (host_cmd) {
+case PROC_SPROTECT:
+data = 
+break;
+
+case PROC_REAP_ACQUIRE:
+case PROC_REAP_RELEAS

[PULL 34/51] bsd-user: Implement shm_rename(2) system call

2023-10-03 Thread Warner Losh
From: Kyle Evans 

Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-7-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-misc.h| 24 
 bsd-user/freebsd/os-syscall.c |  6 ++
 2 files changed, 30 insertions(+)

diff --git a/bsd-user/freebsd/os-misc.h b/bsd-user/freebsd/os-misc.h
index d5e8b5484c8..71145764a4d 100644
--- a/bsd-user/freebsd/os-misc.h
+++ b/bsd-user/freebsd/os-misc.h
@@ -70,5 +70,29 @@ static inline abi_long do_freebsd_shm_open2(abi_ulong 
pathptr, abi_ulong flags,
 }
 #endif /* __FreeBSD_version >= 1300048 */
 
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300049
+/* shm_rename(2) */
+static inline abi_long do_freebsd_shm_rename(abi_ulong fromptr, abi_ulong 
toptr,
+abi_ulong flags)
+{
+int ret;
+void *ufrom, *uto;
+
+ufrom = lock_user_string(fromptr);
+if (ufrom == NULL) {
+return -TARGET_EFAULT;
+}
+uto = lock_user_string(toptr);
+if (uto == NULL) {
+unlock_user(ufrom, fromptr, 0);
+return -TARGET_EFAULT;
+}
+ret = get_errno(shm_rename(ufrom, uto, flags));
+unlock_user(ufrom, fromptr, 0);
+unlock_user(uto, toptr, 0);
+
+return ret;
+}
+#endif /* __FreeBSD_version >= 1300049 */
 
 #endif /* OS_MISC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 122e186b501..5fb42b2c218 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -807,6 +807,12 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 break;
 #endif
 
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300049
+case TARGET_FREEBSD_NR_shm_rename: /* shm_rename(2) */
+ret = do_freebsd_shm_rename(arg1, arg2, arg3);
+break;
+#endif
+
 /*
  * sys{ctl, arch, call}
  */
-- 
2.41.0




[PULL 13/51] bsd-user: Implement getrusage(2).

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-14-kariem.taha...@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 cb7c69acb0c..133c1b0eaf8 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, ));
+if (!is_error(ret)) {
+host_to_target_rusage(target_addr, );
+}
+return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 44cbf52f087..5d8693ed550 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -243,6 +243,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.41.0




[PULL 37/51] bsd-user: Implement ipc_perm conversion between host and target.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-10-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.c | 25 +
 1 file changed, 25 insertions(+)

diff --git a/bsd-user/bsd-mem.c b/bsd-user/bsd-mem.c
index 8834ab2e588..46cda8eb5ce 100644
--- a/bsd-user/bsd-mem.c
+++ b/bsd-user/bsd-mem.c
@@ -30,3 +30,28 @@ void target_set_brk(abi_ulong new_brk)
 target_brk = TARGET_PAGE_ALIGN(new_brk);
 initial_target_brk = target_brk;
 }
+
+void target_to_host_ipc_perm__locked(struct ipc_perm *host_ip,
+ struct target_ipc_perm *target_ip)
+{
+__get_user(host_ip->cuid, _ip->cuid);
+__get_user(host_ip->cgid, _ip->cgid);
+__get_user(host_ip->uid,  _ip->uid);
+__get_user(host_ip->gid,  _ip->gid);
+__get_user(host_ip->mode, _ip->mode);
+__get_user(host_ip->seq,  _ip->seq);
+__get_user(host_ip->key,  _ip->key);
+}
+
+void host_to_target_ipc_perm__locked(struct target_ipc_perm *target_ip,
+ struct ipc_perm *host_ip)
+{
+__put_user(host_ip->cuid, _ip->cuid);
+__put_user(host_ip->cgid, _ip->cgid);
+__put_user(host_ip->uid,  _ip->uid);
+__put_user(host_ip->gid,  _ip->gid);
+__put_user(host_ip->mode, _ip->mode);
+__put_user(host_ip->seq,  _ip->seq);
+__put_user(host_ip->key,  _ip->key);
+}
+
-- 
2.41.0




[PULL 07/51] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-8-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.c | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 68410a0aa9d..19e39a2f764 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -38,3 +38,13 @@ int target_to_host_resource(int code)
 return code;
 }
 
+rlim_t target_to_host_rlim(abi_llong target_rlim)
+{
+return tswap64(target_rlim);
+}
+
+abi_llong host_to_target_rlim(rlim_t rlim)
+{
+return tswap64(rlim);
+}
+
-- 
2.41.0




[PULL 12/51] bsd-user: Implement umask(2), setlogin(2) and getlogin(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-13-kariem.taha...@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 7b25aa19829..cb7c69acb0c 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(VERIFY_WRITE, arg1, arg2, 0);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(_getlogin(p, arg2));
+unlock_user(p, arg1, arg2);
+
+return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 535e6287bde..44cbf52f087 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -231,6 +231,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.41.0




[PULL 38/51] bsd-user: Implement shmid_ds conversion between host and target.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-11-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.c | 47 ++
 1 file changed, 47 insertions(+)

diff --git a/bsd-user/bsd-mem.c b/bsd-user/bsd-mem.c
index 46cda8eb5ce..2ab1334b700 100644
--- a/bsd-user/bsd-mem.c
+++ b/bsd-user/bsd-mem.c
@@ -43,6 +43,30 @@ void target_to_host_ipc_perm__locked(struct ipc_perm 
*host_ip,
 __get_user(host_ip->key,  _ip->key);
 }
 
+abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
+ abi_ulong target_addr)
+{
+struct target_shmid_ds *target_sd;
+
+if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) {
+return -TARGET_EFAULT;
+}
+
+target_to_host_ipc_perm__locked(&(host_sd->shm_perm),
+&(target_sd->shm_perm));
+
+__get_user(host_sd->shm_segsz,  _sd->shm_segsz);
+__get_user(host_sd->shm_lpid,   _sd->shm_lpid);
+__get_user(host_sd->shm_cpid,   _sd->shm_cpid);
+__get_user(host_sd->shm_nattch, _sd->shm_nattch);
+__get_user(host_sd->shm_atime,  _sd->shm_atime);
+__get_user(host_sd->shm_dtime,  _sd->shm_dtime);
+__get_user(host_sd->shm_ctime,  _sd->shm_ctime);
+unlock_user_struct(target_sd, target_addr, 0);
+
+return 0;
+}
+
 void host_to_target_ipc_perm__locked(struct target_ipc_perm *target_ip,
  struct ipc_perm *host_ip)
 {
@@ -55,3 +79,26 @@ void host_to_target_ipc_perm__locked(struct target_ipc_perm 
*target_ip,
 __put_user(host_ip->key,  _ip->key);
 }
 
+abi_long host_to_target_shmid_ds(abi_ulong target_addr,
+ struct shmid_ds *host_sd)
+{
+struct target_shmid_ds *target_sd;
+
+if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) {
+return -TARGET_EFAULT;
+}
+
+host_to_target_ipc_perm__locked(&(target_sd->shm_perm),
+&(host_sd->shm_perm));
+
+__put_user(host_sd->shm_segsz,  _sd->shm_segsz);
+__put_user(host_sd->shm_lpid,   _sd->shm_lpid);
+__put_user(host_sd->shm_cpid,   _sd->shm_cpid);
+__put_user(host_sd->shm_nattch, _sd->shm_nattch);
+__put_user(host_sd->shm_atime,  _sd->shm_atime);
+__put_user(host_sd->shm_dtime,  _sd->shm_dtime);
+__put_user(host_sd->shm_ctime,  _sd->shm_ctime);
+unlock_user_struct(target_sd, target_addr, 1);
+
+return 0;
+}
-- 
2.41.0




[PULL 06/51] bsd-user: Implement target_to_host_resource conversion function

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-7-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.c  | 40 
 bsd-user/bsd-proc.h  |  4 
 bsd-user/meson.build |  6 ++
 3 files changed, 50 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 000..68410a0aa9d
--- /dev/null
+++ b/bsd-user/bsd-proc.c
@@ -0,0 +1,40 @@
+/*
+ *  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 
+#include 
+#include 
+#include 
+#include 
+
+#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)
+{
+return code;
+}
+
diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index a1061bffb8f..048773a75dd 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -22,6 +22,10 @@
 
 #include 
 
+#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 5243122fc56..b97fce14722 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.41.0




[PULL 50/51] bsd-user: Implement shmat(2) and shmdt(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Use `WITH_MMAP_LOCK_GUARD` instead of mmap_lock() and mmap_unlock(),
to match linux-user implementation, according to the following commits:

69fa2708a216df715ba5102a0f98468b540a464e linux-user: Use WITH_MMAP_LOCK_GUARD 
in target_{shmat,shmdt}
ceda5688b650646248f269a992c06b11148c5759 linux-user: Fix shmdt

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Message-Id: <20230925182709.4834-23-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 87 +++
 bsd-user/freebsd/os-syscall.c |  8 
 bsd-user/mmap.c   |  2 +-
 bsd-user/qemu.h   |  1 +
 4 files changed, 97 insertions(+), 1 deletion(-)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index b82f3eaa253..c512a4e3756 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -344,4 +344,91 @@ static inline abi_long do_bsd_shmctl(abi_long shmid, 
abi_long cmd,
 return ret;
 }
 
+/* shmat(2) */
+static inline abi_long do_bsd_shmat(int shmid, abi_ulong shmaddr, int shmflg)
+{
+abi_ulong raddr;
+abi_long ret;
+struct shmid_ds shm_info;
+
+/* Find out the length of the shared memory segment. */
+ret = get_errno(shmctl(shmid, IPC_STAT, _info));
+if (is_error(ret)) {
+/* Can't get the length */
+return ret;
+}
+
+if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) {
+return -TARGET_EINVAL;
+}
+
+WITH_MMAP_LOCK_GUARD() {
+void *host_raddr;
+
+if (shmaddr) {
+host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg);
+} else {
+abi_ulong mmap_start;
+
+mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
+
+if (mmap_start == -1) {
+return -TARGET_ENOMEM;
+}
+host_raddr = shmat(shmid, g2h_untagged(mmap_start),
+   shmflg | SHM_REMAP);
+}
+
+if (host_raddr == (void *)-1) {
+return get_errno(-1);
+}
+raddr = h2g(host_raddr);
+
+page_set_flags(raddr, raddr + shm_info.shm_segsz - 1,
+   PAGE_VALID | PAGE_RESET | PAGE_READ |
+   (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE));
+
+for (int i = 0; i < N_BSD_SHM_REGIONS; i++) {
+if (bsd_shm_regions[i].start == 0) {
+bsd_shm_regions[i].start = raddr;
+bsd_shm_regions[i].size = shm_info.shm_segsz;
+break;
+}
+}
+}
+
+return raddr;
+}
+
+/* shmdt(2) */
+static inline abi_long do_bsd_shmdt(abi_ulong shmaddr)
+{
+abi_long ret;
+
+WITH_MMAP_LOCK_GUARD() {
+int i;
+
+for (i = 0; i < N_BSD_SHM_REGIONS; ++i) {
+if (bsd_shm_regions[i].start == shmaddr) {
+break;
+}
+}
+
+if (i == N_BSD_SHM_REGIONS) {
+return -TARGET_EINVAL;
+}
+
+ret = get_errno(shmdt(g2h_untagged(shmaddr)));
+if (ret == 0) {
+abi_ulong size = bsd_shm_regions[i].size;
+
+bsd_shm_regions[i].start = 0;
+page_set_flags(shmaddr, shmaddr + size - 1, 0);
+mmap_reserve(shmaddr, size);
+}
+}
+
+return ret;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 0512d41db7c..39e66312da1 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -871,6 +871,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_shmctl(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_shmat: /* shmat(2) */
+ret = do_bsd_shmat(arg1, arg2, arg3);
+break;
+
+case TARGET_FREEBSD_NR_shmdt: /* shmdt(2) */
+ret = do_bsd_shmdt(arg1);
+break;
+
 /*
  * Misc
  */
diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index 8e148a2ea3e..3ef11b28079 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -636,7 +636,7 @@ fail:
 return -1;
 }
 
-static void mmap_reserve(abi_ulong start, abi_ulong size)
+void mmap_reserve(abi_ulong start, abi_ulong size)
 {
 abi_ulong real_start;
 abi_ulong real_end;
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 6047805ae38..dc842fffa7d 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -233,6 +233,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong 
old_size,
 int target_msync(abi_ulong start, abi_ulong len, int flags);
 extern abi_ulong mmap_next_start;
 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
+void mmap_reserve(abi_ulong start, abi_ulong size);
 void TSA_NO_TSA mmap_fork_start(void);
 void TSA_NO_TSA mmap_fork_end(int child);
 
-- 
2.41.0




[PULL 42/51] bsd-user: Implement msync(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Co-authored-by: Kyle Evans 
Signed-off-by: Stacey Son 
Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-15-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 11 +++
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 15 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 0f9e4a1d4be..5e885823a79 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -88,4 +88,15 @@ static inline abi_long do_bsd_mprotect(abi_long arg1, 
abi_long arg2,
 return get_errno(target_mprotect(arg1, arg2, arg3));
 }
 
+/* msync(2) */
+static inline abi_long do_bsd_msync(abi_long addr, abi_long len, abi_long 
flags)
+{
+if (!guest_range_valid_untagged(addr, len)) {
+/* It seems odd, but POSIX wants this to be ENOMEM */
+return -TARGET_ENOMEM;
+}
+
+return get_errno(msync(g2h_untagged(addr), len, flags));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 2d8f1a953b2..2525e0bc316 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -811,6 +811,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_mprotect(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_msync: /* msync(2) */
+ret = do_bsd_msync(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 41/51] bsd-user: Implement mprotect(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-14-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 7 +++
 bsd-user/freebsd/os-syscall.c | 4 
 2 files changed, 11 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 76b504f70c5..0f9e4a1d4be 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -81,4 +81,11 @@ static inline abi_long do_bsd_munmap(abi_long arg1, abi_long 
arg2)
 return get_errno(target_munmap(arg1, arg2));
 }
 
+/* mprotect(2) */
+static inline abi_long do_bsd_mprotect(abi_long arg1, abi_long arg2,
+abi_long arg3)
+{
+return get_errno(target_mprotect(arg1, arg2, arg3));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index b03837d032a..2d8f1a953b2 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -807,6 +807,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_munmap(arg1, arg2);
 break;
 
+case TARGET_FREEBSD_NR_mprotect: /* mprotect(2) */
+ret = do_bsd_mprotect(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 20/51] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-21-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.c | 181 -
 bsd-user/main.c|   2 +-
 bsd-user/qemu.h|   1 +
 3 files changed, 180 insertions(+), 4 deletions(-)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index 2603c5c6538..12d78b7fc98 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -30,9 +30,7 @@ struct kinfo_proc;
  * Get the filename for the given file descriptor.
  * Note that this may return NULL (fail) if no longer cached in the kernel.
  */
-char *
-get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len);
-char *
+static char *
 get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
 {
 char *ret = NULL;
@@ -80,3 +78,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) {
+

[PULL 18/51] bsd-user: Implement getpriority(2) and setpriority(2).

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-19-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 24 
 bsd-user/freebsd/os-syscall.c |  8 
 2 files changed, 32 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 2c1a9ae22fa..9a8912361f6 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -390,4 +390,28 @@ 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) {
+return -host_to_target_errno(errno);
+}
+
+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 1a760b13808..71a2657dd0f 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -359,6 +359,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.41.0




[PULL 45/51] bsd-user: Implement mincore(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-18-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 23 +++
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 27 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index b00ab3aed8e..0c8d96d9a43 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -189,4 +189,27 @@ static inline abi_long do_bsd_minherit(abi_long addr, 
abi_long len,
 return get_errno(minherit(g2h_untagged(addr), len, inherit));
 }
 
+/* mincore(2) */
+static inline abi_long do_bsd_mincore(abi_ulong target_addr, abi_ulong len,
+abi_ulong target_vec)
+{
+abi_long ret;
+void *p;
+abi_ulong vec_len = DIV_ROUND_UP(len, TARGET_PAGE_SIZE);
+
+if (!guest_range_valid_untagged(target_addr, len)
+|| !page_check_range(target_addr, len, PAGE_VALID)) {
+return -TARGET_EFAULT;
+}
+
+p = lock_user(VERIFY_WRITE, target_vec, vec_len, 0);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(mincore(g2h_untagged(target_addr), len, p));
+unlock_user(p, target_vec, vec_len);
+
+return ret;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index b8c44cea0ff..f054241cd62 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -839,6 +839,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_minherit(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_mincore: /* mincore(2) */
+ret = do_bsd_mincore(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 26/51] bsd-user: Implement fork(2) and vfork(2) system calls.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-27-kariem.taha...@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 42bdd61904b..7b2e6a9f796 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -185,4 +185,38 @@ static inline abi_long do_freebsd___setugid(abi_long arg1)
 return -TARGET_ENOSYS;
 }
 
+/* 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 99af0f6b156..cb9425c9bab 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -226,6 +226,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.41.0




[PULL 01/51] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics, and fix RLIM_INFINITY

2023-10-03 Thread Warner Losh
From: Karim Taha 

RLIM_INFINITY on FreeBSD, OpenBSD and NetBSD has value of ~(1<<63), caculated
one way or another.

Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-2-kariem.taha...@gmail.com>
---
 bsd-user/syscall_defs.h | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index 9c90616baae..ddd38c13e08 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -130,11 +130,7 @@ struct target_freebsd_timeval {
 /*
  *  sys/resource.h
  */
-#if defined(__FreeBSD__)
 #define TARGET_RLIM_INFINITYRLIM_INFINITY
-#else
-#define TARGET_RLIM_INFINITY((abi_ulong)-1)
-#endif
 
 #define TARGET_RLIMIT_CPU   0
 #define TARGET_RLIMIT_FSIZE 1
@@ -390,6 +386,10 @@ struct target_freebsd_flock {
 int32_t l_sysid;
 } QEMU_PACKED;
 
+/* sys/unistd.h */
+/* user: vfork(2) semantics, clear signals */
+#define TARGET_RFSPAWN (1U << 31)
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
-- 
2.41.0




[PULL 04/51] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-5-kariem.taha...@gmail.com>
---
 bsd-user/qemu.h | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index d9507137cca..41c7bd31d3c 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -249,6 +249,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.41.0




[PULL 43/51] bsd-user: Implement mlock(2), munlock(2), mlockall(2), munlockall(2), minherit(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-16-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 37 +++
 bsd-user/freebsd/os-syscall.c | 20 +++
 2 files changed, 57 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 5e885823a79..16c22593bfd 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -99,4 +99,41 @@ static inline abi_long do_bsd_msync(abi_long addr, abi_long 
len, abi_long flags)
 return get_errno(msync(g2h_untagged(addr), len, flags));
 }
 
+/* mlock(2) */
+static inline abi_long do_bsd_mlock(abi_long arg1, abi_long arg2)
+{
+if (!guest_range_valid_untagged(arg1, arg2)) {
+return -TARGET_EINVAL;
+}
+return get_errno(mlock(g2h_untagged(arg1), arg2));
+}
+
+/* munlock(2) */
+static inline abi_long do_bsd_munlock(abi_long arg1, abi_long arg2)
+{
+if (!guest_range_valid_untagged(arg1, arg2)) {
+return -TARGET_EINVAL;
+}
+return get_errno(munlock(g2h_untagged(arg1), arg2));
+}
+
+/* mlockall(2) */
+static inline abi_long do_bsd_mlockall(abi_long arg1)
+{
+return get_errno(mlockall(arg1));
+}
+
+/* munlockall(2) */
+static inline abi_long do_bsd_munlockall(void)
+{
+return get_errno(munlockall());
+}
+
+/* minherit(2) */
+static inline abi_long do_bsd_minherit(abi_long addr, abi_long len,
+abi_long inherit)
+{
+return get_errno(minherit(g2h_untagged(addr), len, inherit));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 2525e0bc316..7a7ae26793f 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -815,6 +815,26 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_msync(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_mlock: /* mlock(2) */
+ret = do_bsd_mlock(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_munlock: /* munlock(2) */
+ret = do_bsd_munlock(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_mlockall: /* mlockall(2) */
+ret = do_bsd_mlockall(arg1);
+break;
+
+case TARGET_FREEBSD_NR_munlockall: /* munlockall(2) */
+ret = do_bsd_munlockall();
+break;
+
+case TARGET_FREEBSD_NR_minherit: /* minherit(2) */
+ret = do_bsd_minherit(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 29/51] bsd-user: Implement struct target_ipc_perm

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-2-kariem.taha...@gmail.com>
---
 bsd-user/syscall_defs.h | 17 +
 1 file changed, 17 insertions(+)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index a3bc738ff89..0e54d7df690 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -55,6 +55,23 @@ struct target_iovec {
 abi_long iov_len;   /* Number of bytes */
 };
 
+/*
+ * sys/ipc.h
+ */
+struct target_ipc_perm {
+uint32_tcuid;   /* creator user id */
+uint32_tcgid;   /* creator group id */
+uint32_tuid;/* user id */
+uint32_tgid;/* group id */
+uint16_tmode;   /* r/w permission */
+uint16_tseq;/* sequence # */
+abi_longkey;/* user specified msg/sem/shm key */
+};
+
+#define TARGET_IPC_RMID 0   /* remove identifier */
+#define TARGET_IPC_SET  1   /* set options */
+#define TARGET_IPC_STAT 2   /* get options */
+
 /*
  *  sys/mman.h
  */
-- 
2.41.0




[PULL 09/51] bsd-user: Implement host_to_target_waitstatus conversion.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-10-kariem.taha...@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 aa386ff4820..19f6efe1f78 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -102,3 +102,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.41.0




[PULL 00/51] Bsd user mmap patches

2023-10-03 Thread Warner Losh
-user: Implement shm_open(2)
  bsd-user: Implement shm_unlink(2) and shmget(2)
  bsd-user: Implement shmctl(2)
  bsd-user: Implement shmat(2) and shmdt(2)

Warner Losh (1):
  bsd-user: Add stubs for vadvise(), sbrk() and sstk()

 bsd-user/bsd-mem.c| 104 
 bsd-user/bsd-mem.h| 452 
 bsd-user/bsd-proc.c   | 145 ++
 bsd-user/bsd-proc.h   | 379 +++
 bsd-user/freebsd/meson.build  |   1 +
 bsd-user/freebsd/os-misc.h|  98 +++
 bsd-user/freebsd/os-proc.c| 480 ++
 bsd-user/freebsd/os-proc.h| 293 +
 bsd-user/freebsd/os-syscall.c | 313 +-
 bsd-user/main.c   |   2 +-
 bsd-user/meson.build  |   7 +
 bsd-user/mmap.c   |   2 +-
 bsd-user/qemu-bsd.h   |  58 
 bsd-user/qemu.h   |   8 +
 bsd-user/signal-common.h  |   1 +
 bsd-user/signal.c |   6 +
 bsd-user/syscall_defs.h   |  89 ++-
 17 files changed, 2428 insertions(+), 10 deletions(-)
 create mode 100644 bsd-user/bsd-mem.c
 create mode 100644 bsd-user/bsd-mem.h
 create mode 100644 bsd-user/bsd-proc.c
 create mode 100644 bsd-user/freebsd/os-misc.h
 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.41.0




[PULL 17/51] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2).

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-18-kariem.taha...@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 a5f301c72ff..2c1a9ae22fa 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 7b51f4f16e4..1a760b13808 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -343,6 +343,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.41.0




[PULL 11/51] bsd-user: Implement getgroups(2) and setgroups(2) system calls.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-12-kariem.taha...@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 b6225e520ea..7b25aa19829 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;
+g_autofree gid_t *grouplist;
+int i;
+
+grouplist = g_try_new(gid_t, gidsetsize);
+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;
+g_autofree gid_t *grouplist;
+int i;
+
+grouplist = g_try_new(gid_t, gidsetsize);
+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 fa60df529ef..535e6287bde 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -223,6 +223,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.41.0




[PULL 14/51] bsd-user: Implement getrlimit(2) and setrlimit(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-15-kariem.taha...@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 133c1b0eaf8..38d1324034c 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, ));
+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, ));
+}
+return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 5d8693ed550..5cb60862303 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -247,6 +247,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.41.0




[PULL 22/51] bsd-user: Implement execve(2) and fexecve(2) system calls.

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-23-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 49 +++
 bsd-user/freebsd/os-syscall.c | 11 +++-
 2 files changed, 59 insertions(+), 1 deletion(-)
 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 000..75ed39f8ddd
--- /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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#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 b7bd0b92a65..515eaaf31f1 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -36,8 +36,9 @@
 #include "bsd-file.h"
 #include "bsd-proc.h"
 
-/* *BSD dependent syscall shims */
+/* BSD dependent syscall shims */
 #include "os-stat.h"
+#include "os-proc.h"
 
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
@@ -219,6 +220,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.41.0




[PULL 23/51] bsd-user: Implement wait4(2) and wait6(2) system calls.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-24-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 84 +++
 bsd-user/freebsd/os-syscall.c | 15 +++
 2 files changed, 99 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 75ed39f8ddd..04bce755e58 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,84 @@ 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 = 
+}
+ret = get_errno(safe_wait4(arg1, , arg3, rusage_ptr));
+
+if (ret < 0) {
+return ret;
+}
+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, );
+}
+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 = 
+}
+ret = get_errno(safe_wait6(idtype, target_arg64(id1, id2),
+   , options, wrusage_ptr, ));
+
+if (ret < 0) {
+return ret;
+}
+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, );
+}
+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, );
+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 515eaaf31f1..55e68e48159 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -40,6 +40,12 @@
 #include "os-stat.h"
 #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,
@@ -228,6 +234,15 @@ 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.41.0




Re: [PATCH v6 00/23] bsd-user: Implement mmap related system calls for FreeBSD.

2023-09-26 Thread Warner Losh
On Mon, Sep 25, 2023 at 12:28 PM Karim Taha 
wrote:

> Upstream the implementation of the following mmap system calls, from the
> qemu-bsd-user fork:
>mmap(2), munmap(2),
>mprotect(2),
>msync(2),
>mlock(2), munlock(2), mlockall(2), munlockall(2), mincore(2),
>madvise(2),
>minherit(2),
>shm_open(2),shm_open2(2), shm_rename2(2), shm_unlink(2), shmget(2),
> shmctl(2), shmat(2),
>shmdt(2)
>brk(2)
>
> Karim Taha (3):
>   bsd-user: Implement shm_open2(2) system call
>   bsd-user: Add bsd-mem.c to meson.build
>   bsd-user: Implment madvise(2) to match the linux-user implementation.
>
> Kyle Evans (1):
>   bsd-user: Implement shm_rename(2) system call
>
> Stacey Son (18):
>   bsd-user: Implement struct target_ipc_perm
>   bsd-user: Implement struct target_shmid_ds
>   bsd-user: Declarations for ipc_perm and shmid_ds conversion functions
>   bsd-user: Introduce freebsd/os-misc.h to the source tree
>   bsd-user: Implement target_set_brk function in bsd-mem.c instead of
> os-syscall.c
>   bsd-user: Implement ipc_perm conversion between host and target.
>   bsd-user: Implement shmid_ds conversion between host and target.
>   bsd-user: Introduce bsd-mem.h to the source tree
>   bsd-user: Implement mmap(2) and munmap(2)
>   bsd-user: Implement mprotect(2)
>   bsd-user: Implement msync(2)
>   bsd-user: Implement mlock(2), munlock(2), mlockall(2), munlockall(2),
> minherit(2)
>   bsd-user: Implement mincore(2)
>   bsd-user: Implement do_obreak function
>   bsd-user: Implement shm_open(2)
>   bsd-user: Implement shm_unlink(2) and shmget(2)
>   bsd-user: Implement shmctl(2)
>   bsd-user: Implement shmat(2) and shmdt(2)
>
> Warner Losh (1):
>   bsd-user: Add stubs for vadvise(), sbrk() and sstk()
>
>  bsd-user/bsd-mem.c| 104 
>  bsd-user/bsd-mem.h| 452 ++
>  bsd-user/freebsd/os-misc.h|  94 +++
>  bsd-user/freebsd/os-syscall.c | 109 +++-
>  bsd-user/meson.build  |   1 +
>  bsd-user/mmap.c   |   2 +-
>  bsd-user/qemu-bsd.h   |  45 
>  bsd-user/qemu.h   |   1 +
>  bsd-user/syscall_defs.h   |  39 +++
>  9 files changed, 842 insertions(+), 5 deletions(-)
>  create mode 100644 bsd-user/bsd-mem.c
>  create mode 100644 bsd-user/bsd-mem.h
>  create mode 100644 bsd-user/freebsd/os-misc.h
>  create mode 100644 bsd-user/qemu-bsd.h
>

queued to bsd-user-topo

with minor conflicts from rebasing it and the proc system call changes onto
one branch.

I did fix one issue: in the blitz bsd-user fork branch, we called shm_open2
directly, which
you copied to this patch series. This works when compiling static, but not
when compiling
dynamically. In the blitz branch, we always do static building. But since
qemu's CI process
uses dynamic, there was an error. Turns out that shm_open2 is a 'hidden'
system call that's
used to implement other pseudo system calls. As such, it was purposely
hidden in the dynamic
case, exporting only the __sys_shm_open2 system call (normally there're
several ways to get
to these symbols for different threading models that aren't relevant for
this).  By the time I figured
out why it was failing, the history here, etc, it was easier to just make
the minor correction rather
than send it back to you for this one last trivial change. Especially since
the directions for
building bsd-user are recommend --static.

With these changes, I'm able to execute dynamically compiled hello-world:
% ./qemu-arm -L /vidpool/qemu/jails/jails/131armv7 ~/hello-13
Hello
%
which is the first time ever I've been able to run even a full trivial
program that's dynamically
linked. Well done!

Once I get these through the CI pipeline, I'll submit the pull request. And
then learn how to edit
the wiki page for the release notes :)


On Mon, Sep 25, 2023 at 12:28 PM Karim Taha 
wrote:

> Upstream the implementation of the following mmap system calls, from the
> qemu-bsd-user fork:
>mmap(2), munmap(2),
>mprotect(2),
>msync(2),
>mlock(2), munlock(2), mlockall(2), munlockall(2), mincore(2),
>madvise(2),
>minherit(2),
>shm_open(2),shm_open2(2), shm_rename2(2), shm_unlink(2), shmget(2),
> shmctl(2), shmat(2),
>shmdt(2)
>brk(2)
>
> Karim Taha (3):
>   bsd-user: Implement shm_open2(2) system call
>   bsd-user: Add bsd-mem.c to meson.build
>   bsd-user: Implment madvise(2) to match the linux-user implementation.
>
> Kyle Evans (1):
>   bsd-user: Implement shm_rename(2) system call
>
> Stacey Son (18):
>   bsd-user: Implement struct target_ipc_perm
>   bsd-user: Implement struct target_shmid_ds
>   bsd-user: Declarations for ipc_perm and shmid_ds conversion functions
>   bsd-user: Introduce freebsd/os-m

Re: [PATCH v5 00/28] bsd-user: Implement freebsd process related system calls.

2023-09-26 Thread Warner Losh
On Mon, Sep 25, 2023 at 12:25 PM Karim Taha 
wrote:

>
>
> Karim Taha (3):
>   bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics,
> and fix RLIM_INFINITY
>   bsd-user: Implement get_filename_from_fd.
>   bsd-user: Implement execve(2) and fexecve(2) system calls.
>
> Kyle Evans (1):
>   bsd-user: Get number of cpus.
>
> Stacey Son (24):
>   bsd-user: Define procctl(2) related structs
>   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 freebsd_exec_common, used in implementing
> execve/fexecve.
>   bsd-user: Implement procctl(2) along with necessary conversion
> functions.
>   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.
>
>  bsd-user/bsd-proc.c   | 145 ++
>  bsd-user/bsd-proc.h   | 379 +++
>  bsd-user/freebsd/meson.build  |   1 +
>  bsd-user/freebsd/os-proc.c| 479 ++
>  bsd-user/freebsd/os-proc.h| 293 +
>  bsd-user/freebsd/os-syscall.c | 206 ++-
>  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   |  50 +++-
>  13 files changed, 1607 insertions(+), 6 deletions(-)
>  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
>

queued to bsd-user-trial


Re: [v2] Help wanted for enabling -Wshadow=local

2023-09-26 Thread Warner Losh
On Tue, Sep 26, 2023 at 8:43 AM Markus Armbruster  wrote:

> Brian, Gerd, Jason, Marc-André, Michael, we need your help to enable
> -Wshadow=local.
>
> Paolo, you already took care of several subsystems (thanks!), except you
> left a few warnings in target/i386/tcg/seg_helper.c.
>
>
> Local variables shadowing other local variables or parameters make the
> code needlessly hard to understand.  Bugs love to hide in such code.
> Evidence: "[PATCH v3 1/7] migration/rdma: Fix save_page method to fail
> on polling error".
>
> Enabling -Wshadow would prevent bugs like this one.  But we have to
> clean up all the offenders first.
>
> People responded quickly to my first call for help.  Thank you so much!
>
> I'm collecting patches in my git repo at
> https://repo.or.cz/qemu/armbru.git in branch shadow-next, output of
> git-shortlog appended.  I'm happy to do pull requests.  I don't mind
> maintainers merging patches for their subsystems; interference should be
> minimal.
>
> My test build is down to 19 files with warnings.  Sorted by subsystems,
> files covered by multiple subsystems marked "(*NUMBER*)":
>

Please make sure it's disabled for the bsd-user build. I have 3 patches in
my queue
to fix that, but I'm recovering from an illness and trying to land a large
number of patches
from my gsoc student Karim and git publish is being a pain. If this can
wait a week, I'll
likely be better enough by then and can submit the patches. They are all
trivial, but one
depends on the tcg header cleanups.

Thanks

Warner


> Guest CPU cores (TCG)
> -
> Hexagon TCG CPUs
> M: Brian Cain 
> target/hexagon/gen_helper_funcs.py
> target/hexagon/mmvec/macros.h
> target/hexagon/op_helper.c
> target/hexagon/translate.c
>
> X86 TCG CPUs
> M: Paolo Bonzini 
> M: Richard Henderson 
> M: Eduardo Habkost 
> target/i386/tcg/seg_helper.c
>
> Devices
> ---
> Network devices
> M: Jason Wang 
> hw/net/vhost_net.c(*2*)
>
> USB
> M: Gerd Hoffmann 
> hw/usb/desc.c
> hw/usb/dev-hub.c
> hw/usb/dev-storage.c
> hw/usb/hcd-xhci.c
> hw/usb/host-libusb.c
>
> vhost
> M: Michael S. Tsirkin 
> contrib/vhost-user-gpu/vhost-user-gpu.c(*2*)
> contrib/vhost-user-gpu/vugpu.h(*2*)
> hw/net/vhost_net.c(*2*)
> hw/virtio/vhost.c
>
> virtio
> M: Michael S. Tsirkin 
> hw/virtio/virtio-pci.c
> include/hw/virtio/virtio-gpu.h(*2*)
>
> virtio-gpu
> M: Gerd Hoffmann 
> include/hw/virtio/virtio-gpu.h(*2*)
>
> vhost-user-gpu
> M: Marc-André Lureau 
> R: Gerd Hoffmann 
> contrib/vhost-user-gpu/vhost-user-gpu.c(*2*)
> contrib/vhost-user-gpu/vugpu.h(*2*)
>
> Subsystems
> --
> Overall Audio backends
> M: Gerd Hoffmann 
> M: Marc-André Lureau 
> audio/audio.c
>
> Open Sound System (OSS) Audio backend
> M: Gerd Hoffmann 
> audio/ossaudio.c
>
> Dump
> M: Marc-André Lureau 
> dump/dump.c
>
>
> Patches collected so far:
>
> Alberto Garcia (1):
>   test-throttle: don't shadow 'index' variable in do_test_accounting()
>
> Alistair Francis (4):
>   hw/riscv: opentitan: Fixup local variables shadowing
>   target/riscv: cpu: Fixup local variables shadowing
>   target/riscv: vector_helper: Fixup local variables shadowing
>   softmmu/device_tree: Fixup local variables shadowing
>
> Ani Sinha (1):
>   hw/acpi: changes towards enabling -Wshadow=local
>
> Cédric Le Goater (13):
>   hw/ppc: Clean up local variable shadowing in _FDT helper routine
>   pnv/psi: Clean up local variable shadowing
>   spapr: Clean up local variable shadowing in spapr_dt_cpus()
>   spapr: Clean up local variable shadowing in spapr_init_cpus()
>   spapr: Clean up local variable shadowing in spapr_get_fw_dev_path()
>   spapr/drc: Clean up local variable shadowing in
> rtas_ibm_configure_connector()
>   spapr/pci: Clean up local variable shadowing in spapr_phb_realize()
>   spapr/drc: Clean up local variable shadowing in prop_get_fdt()
>   aspeed/i2c: Clean up local variable shadowing
>   aspeed: Clean up local variable shadowing
>   aspeed/i3c: Rename variable shadowing a local
>   aspeed/timer: Clean up local variable shadowing
>   target/ppc: Rename variables to avoid local variable shadowing in
> VUPKPX
>
> Daniel P. Berrangé (2):
>   crypto: remove shadowed 'ret' variable
>   seccomp: avoid shadowing of 'action' variable
>
> Eric Blake (1):
>   qemu-nbd: changes towards enabling -Wshadow=local
>
> Klaus Jensen (1):
>   hw/nvme: Clean up local variable shadowing in nvme_ns_init()
>
> Laurent Vivier (1):
>   disas/m68k: clean up local variable shadowing
>
> Markus Armbruster (8):
>   meson: Enable -Wshadow as a warning
>   migration/rdma: Fix save_page method to fail on polling error
>   migration: Clean up local variable shadowing
>   ui: Clean up local variable shadowing
>   block/dirty-bitmap: Clean up local variable shadowing
>   block/vdi: Clean up local variable shadowing

Re: Help wanted for enabling -Wshadow=local

2023-09-25 Thread Warner Losh
On Sat, Sep 23, 2023 at 6:33 AM Peter Maydell 
wrote:

> On Fri, 22 Sept 2023 at 19:59, Warner Losh  wrote:
> > The third one, though, makes me ask the question: When should we
> > pass in cpu_env to functions and when should we use the global value?
> >
> > I have a lot of changes that look like:
> >
> > -static inline abi_long do_freebsd_thr_exit(CPUArchState *cpu_env,
> > +static inline abi_long do_freebsd_thr_exit(CPUArchState *env,
> >  abi_ulong tid_addr)
> >  {
> > -CPUState *cpu = env_cpu(cpu_env);
> > +CPUState *cpu = env_cpu(env);
> >  TaskState *ts;
> > ...
> >  env>
> >
> > Should I just drop the arg, or do the arg rename? Or "Gee, Warner,
> > that really depends since it's context sensitive" in which case
> > I'll just post a review to the list.
>
> Is this the "extern TCGv_env cpu_env;" in tcg/tcg.h ?
>

Yes.


> As a TCGv_env, that is only of any use in the TCG frontends,
> not in the bsd-user/ or linux-user/ code. In fact almost
> all of tcg/tcg.h is intended for the TCG frontends, so the
> "ideal" solution to this would be to not include it in the
> bsd-user code. This might mean figuring out what parts of
> it need to be split out into different headers. (linux-user
> also includes tcg/tcg.h.)
>

I saw your other changes, and I think that would be good for
bsd-user as well. I don't think we're using it anywhere directly.


> However, this isn't necessary for the current effort, because
> -Wshadow=local only warns about local-to-local shadowing,
> not cases where a local shadows a global.
>

Yea, clang was having heartburn with -Wshadow= so
I had to run things with -Wshadow.

Warner


> thanks
> -- PMM
>


Re: [PATCH v4 25/28] bsd-user: Implement pdgetpid(2) and the undocumented setugid.

2023-09-25 Thread Warner Losh
On Sun, Sep 24, 2023 at 8:37 PM Karim Taha  wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Reviewed-by: Richard Henderson 
> ---
>  bsd-user/freebsd/os-proc.h| 23 +++
>  bsd-user/freebsd/os-syscall.c |  8 
>  2 files changed, 31 insertions(+)
>

Reviewed-by: Warner Losh 


Re: [PATCH v4 24/28] bsd-user: Implement setloginclass(2) and getloginclass(2) system calls.

2023-09-25 Thread Warner Losh
On Sun, Sep 24, 2023 at 8:37 PM Karim Taha  wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> ---
>  bsd-user/freebsd/os-proc.h| 32 
>  bsd-user/freebsd/os-syscall.c |  8 
>  2 files changed, 40 insertions


Reviewed-by: Warner Losh 


Re: [PATCH v4 23/28] bsd-user: Implement wait4(2) and wait6(2) system calls.

2023-09-25 Thread Warner Losh
On Sun, Sep 24, 2023 at 8:36 PM Karim Taha  wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> ---
>  bsd-user/freebsd/os-proc.h| 84 +++
>  bsd-user/freebsd/os-syscall.c | 15 +++
>  2 files changed, 99 insertions(+)
>

Reviewed-by: Warner Losh 


Re: [PATCH v4 21/28] bsd-user: Implement procctl(2) along with necessary conversion functions.

2023-09-25 Thread Warner Losh
On Sun, Sep 24, 2023 at 8:36 PM Karim Taha  wrote:

> From: Stacey Son 
>
> Implement t2h_procctl_cmd, h2t_reaper_status, h2t_reaper_pidinfo and
> h2t/t2h reaper_kill conversion functions.
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> ---
>  bsd-user/freebsd/os-proc.c| 222 ++
>  bsd-user/freebsd/os-syscall.c |   3 +
>  2 files changed, 225 insertions(+)
>

Reviewed-by: Warner Losh 

This appears to line up, but if Richard has time for only one review, this
one would be the best place to spend it in case there's something both
Karim and I have overlooked.

Warner


Re: [PATCH v4 12/28] bsd-user: Implement umask(2), setlogin(2) and getlogin(2)

2023-09-25 Thread Warner Losh
On Sun, Sep 24, 2023 at 8:36 PM Karim Taha  wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> ---
>  bsd-user/bsd-proc.h   | 39 +++
>  bsd-user/freebsd/os-syscall.c | 12 +++
>  2 files changed, 51 insertions(+)
>

Reviewed-by: Warner Losh 


Re: [PATCH v4 06/28] bsd-user: Implement target_to_host_resource conversion function

2023-09-25 Thread Warner Losh
On Sun, Sep 24, 2023 at 8:36 PM Karim Taha  wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> ---
>  bsd-user/bsd-proc.c  | 40 
>  bsd-user/bsd-proc.h  |  4 
>  bsd-user/meson.build |  6 ++
>  3 files changed, 50 insertions(+)
>  create mode 100644 bsd-user/bsd-proc.c
>

This looks good. The extra libraries relative to linux-user are for the
stuff bsd-user does to support some additional things than linux-user
supports.

Reviewed-by: Warner Losh 



> diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
> new file mode 100644
> index 00..68410a0aa9
> --- /dev/null
> +++ b/bsd-user/bsd-proc.c
> @@ -0,0 +1,40 @@
> +/*
> + *  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 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#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)
> +{
> +return code;
> +}
> +
> 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 
>
> +#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.42.0
>
>


Re: Help wanted for enabling -Wshadow=local

2023-09-22 Thread Warner Losh
On Fri, Sep 22, 2023 at 11:49 AM Peter Maydell 
wrote:

> On Fri, 22 Sept 2023 at 18:43, Daniel Henrique Barboza
>  wrote:
> > Can you publish your branch with the current -Wshadow=local patches in
> > gitlab/github? I'm hitting (and fixing) a lot of errors that aren't
> listed
> > here, meaning they're either fixed already in your local branch or needs
> to
> > be fixed.
>
> Markus sent an email with the git branch, but it doesn't seem to have
> reached the list, perhaps because it also included a 10,000 line
> build log and probably hit the length limit... Anyway, to quote
> him from that email (which I got because of a direct CC):
>
> > Pushed to https://repo.or.cz/qemu/armbru.git branch shadow-next.  I'll
> > keep collecting shadow patches there, and I'll rebase as needed.
>

I have 3 changes for bsd-user. Two are trivial, hardly worth commenting on.

The third one, though, makes me ask the question: When should we pass in
cpu_env to functions and when should we use the global value?

I have a lot of changes that look like:

-static inline abi_long do_freebsd_thr_exit(CPUArchState *cpu_env,
+static inline abi_long do_freebsd_thr_exit(CPUArchState *env,
 abi_ulong tid_addr)
 {
-CPUState *cpu = env_cpu(cpu_env);
+CPUState *cpu = env_cpu(env);
 TaskState *ts;
...
 env>

Should I just drop the arg, or do the arg rename? Or "Gee, Warner, that
really depends since it's context sensitive" in which case I'll just post a
review to the list.

Warner


Re: [PATCH v2 28/28] bsd-user: Implement pdfork(2) system call.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:40 PM Karim Taha 
wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Acked-by: Richard Henderson 
> ---
>  bsd-user/freebsd/os-proc.h| 32 
>  bsd-user/freebsd/os-syscall.c |  4 
>  2 files changed, 36 insertions(+)
>

Reviewed-by: Warner Losh 

I chased down pdfork recently for other reasons, and I'm pretty sure this
is good.

Warner


Re: [PATCH v2 27/28] bsd-user: Implement rfork(2) system call.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Reviewed-by: Richard Henderson 
> ---
>  bsd-user/freebsd/os-proc.h| 39 +++
>  bsd-user/freebsd/os-syscall.c |  4 
>  2 files changed, 43 insertions(+)
>

Reviewed-by: Warner Losh 


> diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
> index 14478d4bb5..a406ef7db8 100644
> --- a/bsd-user/freebsd/os-proc.h
> +++ b/bsd-user/freebsd/os-proc.h
> @@ -212,4 +212,43 @@ 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 cb9425c9ba..4c4e773d1d 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -234,6 +234,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.42.0
>
>


Re: [PATCH v2 26/28] bsd-user: Implement fork(2) and vfork(2) system calls.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Reviewed-by: Richard Henderson 
> ---
>  bsd-user/freebsd/os-proc.h| 34 ++
>  bsd-user/freebsd/os-syscall.c |  8 
>  2 files changed, 42 insertions(+)
>

Reviewed-by: Warner Losh 

Though i have minor qualms about vfork == fork, for bsd-user it's fine
since I
don't think the performance difference will be that large for the typical
case
where vfork + exec exists for older (now kinda really old) programs that
used
to use this.

Warner


Re: [PATCH v2 25/28] bsd-user: Implement pdgetpid(2) and the undocumented setugid.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Reviewed-by: Richard Henderson 
> ---
>  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 1866f0b2d6..47bcdcf8a3 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)
> @@ -155,4 +157,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, ));
> +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));
>

This should be return -TARGET_ENOSYS since the kernel doesn't implement
it for anything except a regression test  And what it does is quite
dangerous,
so we don't want someone to think it's a good idea to implement it in the
future.

Warner


> +}
> +
>  #endif /* BSD_USER_FREEBSD_OS_PROC_H */
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index d614409e69..99af0f6b15 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -383,6 +383,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.42.0
>
>


Re: [PATCH v2 24/28] bsd-user: Implement setloginclass(2) and getloginclass(2) system calls.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Reviewed-by: Richard Henderson 
> ---
>  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 8a0b6e25bb..1866f0b2d6 100644
> --- a/bsd-user/freebsd/os-proc.h
> +++ b/bsd-user/freebsd/os-proc.h
> @@ -123,4 +123,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);
>

This has the same problem that I highlighted in _getlogin() has. The kernel
returns a string, so we have to lock the buffer for it, not the string.

Warner


> +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 55e68e4815..d614409e69 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -375,6 +375,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.42.0
>
>


Re: [PATCH v2 23/28] bsd-user: Implement wait4(2) and wait6(2) system calls.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Reviewed-by: Richard Henderson 
> ---
>  bsd-user/freebsd/os-proc.h| 77 +++
>  bsd-user/freebsd/os-syscall.c | 15 +++
>  2 files changed, 92 insertions(+)
>
> diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
> index 75ed39f8dd..8a0b6e25bb 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,77 @@ 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 = 
> +}
> +ret = get_errno(safe_wait4(arg1, , 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, );
> +}
> +return ret;
>

I think that both of these 'if' statements should only be done if ret == 0.
Otherwise
it's an error return which doesn't usually write any arguments (unless the
error
is because of a fault on trying to write a return value).

Warner


> +}
> +
> +/* 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 = 
> +}
> +ret = safe_wait6(idtype, target_arg64(id1, id2),
> + , options, wrusage_ptr, );
> +ret = get_errno(ret);
> +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, );
> +}
> +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, );
> +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 515eaaf31f..55e68e4815 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -40,6 +40,12 @@
>  #include "os-stat.h"
>  #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,
> @@ -228,6 +234,15 @@ 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);
> 

Re: [PATCH v2 22/28] bsd-user: Implement execve(2) and fexecve(2) system calls.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Reviewed-by: Richard Henderson 
> ---
>  bsd-user/freebsd/os-proc.h| 49 +++
>  bsd-user/freebsd/os-syscall.c | 11 +++-
>  2 files changed, 59 insertions(+), 1 deletion(-)
>  create mode 100644 bsd-user/freebsd/os-proc.h
>

Reviewed-by: Warner Losh 

But make sure that the guard variable name is correct, I think with scripts/
clean-header-guards.pl


> diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
> new file mode 100644
> index 00..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 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#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 b7bd0b92a6..515eaaf31f 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -36,8 +36,9 @@
>  #include "bsd-file.h"
>  #include "bsd-proc.h"
>
> -/* *BSD dependent syscall shims */
> +/* BSD dependent syscall shims */
>  #include "os-stat.h"
> +#include "os-proc.h"
>
>  /* I/O */
>  safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
> @@ -219,6 +220,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.42.0
>
>


Re: [PATCH v2 21/28] bsd-user: Implement procctl(2) along with necessary conversion functions.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> From: Stacey Son 
>
> Implement t2h_procctl_cmd, h2t_reaper_status, h2t_reaper_pidinfo and
> h2t/t2h reaper_kill conversion functions.
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> ---
>  bsd-user/freebsd/os-proc.c| 223 ++
>  bsd-user/freebsd/os-syscall.c |   3 +
>  2 files changed, 226 insertions(+)
>
> diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
> index 12d78b7fc9..6b8753f8e5 100644
> --- a/bsd-user/freebsd/os-proc.c
> +++ b/bsd-user/freebsd/os-proc.c
> @@ -255,3 +255,226 @@ execve_end:
>  return ret;
>  }
>
> +#include 
> +
> +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, _rs->rs_flags);
> +__put_user(host_rs->rs_children, _rs->rs_children);
> +__put_user(host_rs->rs_descendants, _rs->rs_descendants);
> +__put_user(host_rs->rs_reaper, _rs->rs_reaper);
> +__put_user(host_rs->rs_pid, _rs->rs_pid);
> +unlock_user_struct(target_rs, target_rs_addr, 1);
> +
> +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, _rk->rk_sig);
> +__get_user(host_rk->rk_flags, _rk->rk_flags);
> +__get_user(host_rk->rk_subtree, _rk->rk_subtree);
> +__get_user(host_rk->rk_killed, _rk->rk_killed);
> +__get_user(host_rk->rk_fpid, _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, _rk->rk_sig);
> +__put_user(host_rk->rk_flags, _rk->rk_flags);
> +__put_user(host_rk->rk_subtree, _rk->rk_subtree);
> +__put_user(host_rk->rk_killed, _rk->rk_killed);
> +__put_user(host_rk->rk_fpid, _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, _pi->pi_pid);
> +__put_user(host_pi->pi_subtree, _pi->pi_subtree);
> +__put_user(host_pi->pi_flags, _pi->pi_flags);
> +unlock_user_struct(target_pi, target_pi_addr, 1);
> +
> +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, _cmd);
> +if (error) {
> +return 

Re: [PATCH v2 20/28] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> From: Stacey Son 
>
> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> ---
>  bsd-user/freebsd/os-proc.c | 177 +
>  bsd-user/main.c|   2 +-
>  bsd-user/qemu.h|   1 +
>  3 files changed, 179 insertions(+), 1 deletion(-)
>

Reviewed-by: Warner Losh 

But see comment below.


> diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
> index cb35f29f10..12d78b7fc9 100644
> --- a/bsd-user/freebsd/os-proc.c
> +++ b/bsd-user/freebsd/os-proc.c
> @@ -78,3 +78,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.
> + */
>

So we do this fd dance so we can make things like 'qemu-arm-static
/armv7/bin/sh' work.
Doug Rabson has some changes that means we can ditch this, I think, since
the
kernel will just track it and it will default to 'what is doing the current
process'
rather than the system default for the same binfmt entry.


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

And we do this inheritance so we can pass in a non-standard library path,
maybe for testing, and have the above example also work.

Warner


> +ret = get_er

Re: [PATCH v2 19/28] bsd-user: Implement get_filename_from_fd.

2023-09-20 Thread Warner Losh
On Sun, Sep 17, 2023 at 10:39 PM Karim Taha 
wrote:

> Signed-off-by: Stacey Son 
> Signed-off-by: Karim Taha 
> Reviewed-by: Richard Henderson 
> ---
>  bsd-user/freebsd/meson.build |  1 +
>  bsd-user/freebsd/os-proc.c   | 80 
>  2 files changed, 81 insertions(+)
>  create mode 100644 bsd-user/freebsd/os-proc.c
>

Reviewed-by: Warner Losh 


  1   2   3   4   5   6   7   8   9   10   >