[PATCH 07/24] bsd-user: Add target_to_host_semarray for semaphore operations

Warner Losh posted 24 patches 3 days, 15 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 07/24] bsd-user: Add target_to_host_semarray for semaphore operations
Posted by Warner Losh 3 days, 15 hours ago
From: Stacey Son <sson@FreeBSD.org>

Add target_to_host_semarray() to convert target semaphore array to host
format for semctl(2) SETALL operations.

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

diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
index d2107b2f85..07d8bf1304 100644
--- a/bsd-user/bsd-misc.c
+++ b/bsd-user/bsd-misc.c
@@ -18,6 +18,11 @@
  */
 #include "qemu/osdep.h"
 
+#define _WANT_SEMUN
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <sys/sem.h>
 #include <sys/uuid.h>
 
 #include "qemu.h"
@@ -44,3 +49,33 @@ abi_long host_to_target_uuid(abi_ulong target_addr, struct uuid *host_uuid)
     unlock_user_struct(target_uuid, target_addr, 1);
     return 0;
 }
+
+abi_long target_to_host_semarray(int semid, unsigned short **host_array,
+        abi_ulong target_addr)
+{
+    abi_long ret;
+    int nsems, i;
+    unsigned short *array;
+    union semun semun;
+    struct semid_ds semid_ds;
+
+    semun.buf = &semid_ds;
+    ret = semctl(semid, 0, IPC_STAT, semun);
+    if (ret == -1) {
+        return get_errno(ret);
+    }
+    nsems = semid_ds.sem_nsems;
+    *host_array = (unsigned short *)malloc(nsems * sizeof(unsigned short));
+    array = lock_user(VERIFY_READ, target_addr,
+        nsems * sizeof(unsigned short), 1);
+    if (array == NULL) {
+        free(*host_array);
+        return -TARGET_EFAULT;
+    }
+    for (i = 0; i < nsems; i++) {
+        (*host_array)[i] = array[i];
+    }
+    unlock_user(array, target_addr, 0);
+
+    return 0;
+}

-- 
2.52.0
Re: [PATCH 07/24] bsd-user: Add target_to_host_semarray for semaphore operations
Posted by Richard Henderson 3 days, 7 hours ago
On 2/6/26 03:26, Warner Losh wrote:
> From: Stacey Son <sson@FreeBSD.org>
> 
> Add target_to_host_semarray() to convert target semaphore array to host
> format for semctl(2) SETALL operations.
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   bsd-user/bsd-misc.c | 35 +++++++++++++++++++++++++++++++++++
>   1 file changed, 35 insertions(+)
> 
> diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
> index d2107b2f85..07d8bf1304 100644
> --- a/bsd-user/bsd-misc.c
> +++ b/bsd-user/bsd-misc.c
> @@ -18,6 +18,11 @@
>    */
>   #include "qemu/osdep.h"
>   
> +#define _WANT_SEMUN
> +#include <sys/types.h>
> +#include <sys/ipc.h>
> +#include <sys/msg.h>
> +#include <sys/sem.h>
>   #include <sys/uuid.h>
>   
>   #include "qemu.h"
> @@ -44,3 +49,33 @@ abi_long host_to_target_uuid(abi_ulong target_addr, struct uuid *host_uuid)
>       unlock_user_struct(target_uuid, target_addr, 1);
>       return 0;
>   }
> +
> +abi_long target_to_host_semarray(int semid, unsigned short **host_array,
> +        abi_ulong target_addr)
> +{
> +    abi_long ret;
> +    int nsems, i;
> +    unsigned short *array;
> +    union semun semun;
> +    struct semid_ds semid_ds;
> +
> +    semun.buf = &semid_ds;
> +    ret = semctl(semid, 0, IPC_STAT, semun);
> +    if (ret == -1) {
> +        return get_errno(ret);
> +    }
> +    nsems = semid_ds.sem_nsems;
> +    *host_array = (unsigned short *)malloc(nsems * sizeof(unsigned short));

g_malloc, or test for allocation failure and return -TARGET_ENOMEM.

> +    array = lock_user(VERIFY_READ, target_addr,
> +        nsems * sizeof(unsigned short), 1);
> +    if (array == NULL) {
> +        free(*host_array);
> +        return -TARGET_EFAULT;
> +    }
> +    for (i = 0; i < nsems; i++) {
> +        (*host_array)[i] = array[i];

__get_user, for endianness issues.


r~
Re: [PATCH 07/24] bsd-user: Add target_to_host_semarray for semaphore operations
Posted by Warner Losh 3 days, 6 hours ago
On Thu, Feb 5, 2026 at 6:44 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 2/6/26 03:26, Warner Losh wrote:
> > From: Stacey Son <sson@FreeBSD.org>
> >
> > Add target_to_host_semarray() to convert target semaphore array to host
> > format for semctl(2) SETALL operations.
> >
> > Signed-off-by: Stacey Son <sson@FreeBSD.org>
> > Signed-off-by: Warner Losh <imp@bsdimp.com>
> > ---
> >   bsd-user/bsd-misc.c | 35 +++++++++++++++++++++++++++++++++++
> >   1 file changed, 35 insertions(+)
> >
> > diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
> > index d2107b2f85..07d8bf1304 100644
> > --- a/bsd-user/bsd-misc.c
> > +++ b/bsd-user/bsd-misc.c
> > @@ -18,6 +18,11 @@
> >    */
> >   #include "qemu/osdep.h"
> >
> > +#define _WANT_SEMUN
> > +#include <sys/types.h>
> > +#include <sys/ipc.h>
> > +#include <sys/msg.h>
> > +#include <sys/sem.h>
> >   #include <sys/uuid.h>
> >
> >   #include "qemu.h"
> > @@ -44,3 +49,33 @@ abi_long host_to_target_uuid(abi_ulong target_addr,
> struct uuid *host_uuid)
> >       unlock_user_struct(target_uuid, target_addr, 1);
> >       return 0;
> >   }
> > +
> > +abi_long target_to_host_semarray(int semid, unsigned short **host_array,
> > +        abi_ulong target_addr)
> > +{
> > +    abi_long ret;
> > +    int nsems, i;
> > +    unsigned short *array;
> > +    union semun semun;
> > +    struct semid_ds semid_ds;
> > +
> > +    semun.buf = &semid_ds;
> > +    ret = semctl(semid, 0, IPC_STAT, semun);
> > +    if (ret == -1) {
> > +        return get_errno(ret);
> > +    }
> > +    nsems = semid_ds.sem_nsems;
> > +    *host_array = (unsigned short *)malloc(nsems * sizeof(unsigned
> short));
>
> g_malloc, or test for allocation failure and return -TARGET_ENOMEM.
>

Oh, right. It's slowly coming back to me to look for things like this.


> > +    array = lock_user(VERIFY_READ, target_addr,
> > +        nsems * sizeof(unsigned short), 1);
> > +    if (array == NULL) {
> > +        free(*host_array);
> > +        return -TARGET_EFAULT;
> > +    }
> > +    for (i = 0; i < nsems; i++) {
> > +        (*host_array)[i] = array[i];
>
> __get_user, for endianness issues.
>

Good call. You'll find several of these...

Warner


>
> r~
>
>