[PATCH 22/24] bsd-user: Implement System V semaphore calls

Warner Losh posted 24 patches 3 days, 16 hours ago
Maintainers: Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>, Riku Voipio <riku.voipio@iki.fi>, Paolo Bonzini <pbonzini@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>
There is a newer version of this series
[PATCH 22/24] bsd-user: Implement System V semaphore calls
Posted by Warner Losh 3 days, 16 hours ago
From: Stacey Son <sson@FreeBSD.org>

Wire up semget(2) and semop(2) syscalls to get System V semaphore
implementation, as well the undocumented __semctl used to implement the
bits of the interface in libc.

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

diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index ca2f6fdb66..6e38007bdd 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -37,6 +37,7 @@
 #include "bsd-file.h"
 #include "bsd-mem.h"
 #include "bsd-proc.h"
+#include "bsd-misc.h"
 
 /* BSD dependent syscall shims */
 #include "os-stat.h"
@@ -879,6 +880,28 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_shmdt(arg1);
         break;
 
+        /*
+         * System V Semaphores
+         */
+    case TARGET_FREEBSD_NR_semget: /* semget(2) */
+        ret = do_bsd_semget(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_semop: /* semop(2) */
+        ret = do_bsd_semop(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR___semctl: { /* __semctl() undocumented */
+        /*
+         * The semun argument to semctl is passed by value, so dereference the
+         * ptr argument.
+         */
+        abi_ulong atptr;
+        get_user_ual(atptr, (abi_ulong)arg4);
+        ret = do_bsd___semctl(arg1, arg2, arg3,
+                (union target_semun)(abi_ulong) atptr);
+        break;
+    }
     case TARGET_FREEBSD_NR_freebsd11_vadvise:
         ret = do_bsd_vadvise();
         break;

-- 
2.52.0
Re: [PATCH 22/24] bsd-user: Implement System V semaphore calls
Posted by Richard Henderson 3 days, 6 hours ago
On 2/6/26 03:26, Warner Losh wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Wire up semget(2) and semop(2) syscalls to get System V semaphore
> implementation, as well the undocumented __semctl used to implement the
> bits of the interface in libc.
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
>   1 file changed, 23 insertions(+)
> 
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index ca2f6fdb66..6e38007bdd 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -37,6 +37,7 @@
>   #include "bsd-file.h"
>   #include "bsd-mem.h"
>   #include "bsd-proc.h"
> +#include "bsd-misc.h"
>   
>   /* BSD dependent syscall shims */
>   #include "os-stat.h"
> @@ -879,6 +880,28 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
>           ret = do_bsd_shmdt(arg1);
>           break;
>   
> +        /*
> +         * System V Semaphores
> +         */
> +    case TARGET_FREEBSD_NR_semget: /* semget(2) */
> +        ret = do_bsd_semget(arg1, arg2, arg3);
> +        break;
> +
> +    case TARGET_FREEBSD_NR_semop: /* semop(2) */
> +        ret = do_bsd_semop(arg1, arg2, arg3);
> +        break;
> +
> +    case TARGET_FREEBSD_NR___semctl: { /* __semctl() undocumented */
> +        /*
> +         * The semun argument to semctl is passed by value, so dereference the
> +         * ptr argument.
> +         */
> +        abi_ulong atptr;
> +        get_user_ual(atptr, (abi_ulong)arg4);
> +        ret = do_bsd___semctl(arg1, arg2, arg3,
> +                (union target_semun)(abi_ulong) atptr);

Eh?  Does this really compile, casting an integer to a union?
Something seems very confused here.


r~
Re: [PATCH 22/24] bsd-user: Implement System V semaphore calls
Posted by Warner Losh 2 days, 15 hours ago
On Thu, Feb 5, 2026 at 8:34 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 2/6/26 03:26, Warner Losh wrote:
> > From: Stacey Son <sson@FreeBSD.org>
> >
> > Wire up semget(2) and semop(2) syscalls to get System V semaphore
> > implementation, as well the undocumented __semctl used to implement the
> > bits of the interface in libc.
> >
> > Signed-off-by: Stacey Son <sson@FreeBSD.org>
> > Signed-off-by: Warner Losh <imp@bsdimp.com>
> > ---
> >   bsd-user/freebsd/os-syscall.c | 23 +++++++++++++++++++++++
> >   1 file changed, 23 insertions(+)
> >
> > diff --git a/bsd-user/freebsd/os-syscall.c
> b/bsd-user/freebsd/os-syscall.c
> > index ca2f6fdb66..6e38007bdd 100644
> > --- a/bsd-user/freebsd/os-syscall.c
> > +++ b/bsd-user/freebsd/os-syscall.c
> > @@ -37,6 +37,7 @@
> >   #include "bsd-file.h"
> >   #include "bsd-mem.h"
> >   #include "bsd-proc.h"
> > +#include "bsd-misc.h"
> >
> >   /* BSD dependent syscall shims */
> >   #include "os-stat.h"
> > @@ -879,6 +880,28 @@ static abi_long freebsd_syscall(void *cpu_env, int
> num, abi_long arg1,
> >           ret = do_bsd_shmdt(arg1);
> >           break;
> >
> > +        /*
> > +         * System V Semaphores
> > +         */
> > +    case TARGET_FREEBSD_NR_semget: /* semget(2) */
> > +        ret = do_bsd_semget(arg1, arg2, arg3);
> > +        break;
> > +
> > +    case TARGET_FREEBSD_NR_semop: /* semop(2) */
> > +        ret = do_bsd_semop(arg1, arg2, arg3);
> > +        break;
> > +
> > +    case TARGET_FREEBSD_NR___semctl: { /* __semctl() undocumented */
> > +        /*
> > +         * The semun argument to semctl is passed by value, so
> dereference the
> > +         * ptr argument.
> > +         */
> > +        abi_ulong atptr;
> > +        get_user_ual(atptr, (abi_ulong)arg4);
> > +        ret = do_bsd___semctl(arg1, arg2, arg3,
> > +                (union target_semun)(abi_ulong) atptr);
>
> Eh?  Does this really compile, casting an integer to a union?
> Something seems very confused here.
>

It works, but it's kinda nutso code. I wouldn't have thought you could cast
a long to a union...

It's even more nutso inside do_bsd__semctl()! I think that we need to honor
that this is really a pointer to a union and pass that in as such and then
__get/put_user rather than doing the tswap dance twice.

Warner