[PATCH 12/24] bsd-user: Add host_to_target_msqid_ds for msgctl(2)

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 12/24] bsd-user: Add host_to_target_msqid_ds for msgctl(2)
Posted by Warner Losh 3 days, 16 hours ago
From: Stacey Son <sson@FreeBSD.org>

Add host_to_target_msqid_ds() to convert host struct msqid_ds to target
format for msgctl(2) IPC_STAT operations.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Brooks Davis <brooks@one-eyed-alien.net>
Signed-off-by: Sean Bruno <sbruno@FreeBSD.org>
Signed-off-by: Mikael Urankar <mikael.urankar@gmail.com>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/bsd-misc.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
index e7031c5264..03773e2620 100644
--- a/bsd-user/bsd-misc.c
+++ b/bsd-user/bsd-misc.c
@@ -187,3 +187,36 @@ abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
 
     return 0;
 }
+
+abi_long host_to_target_msqid_ds(abi_ulong target_addr,
+        struct msqid_ds *host_md)
+{
+    struct target_msqid_ds *target_md;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+
+    memset(target_md, 0, sizeof(struct target_msqid_ds));
+    host_to_target_ipc_perm__locked(&target_md->msg_perm,
+                                    &host_md->msg_perm);
+
+    /* msg_first and msg_last are not used by IPC_SET/IPC_STAT in kernel. */
+    target_md->msg_cbytes = tswapal(host_md->msg_cbytes);
+    target_md->msg_qnum = tswapal(host_md->msg_qnum);
+    target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
+    target_md->msg_lspid = tswapal(host_md->msg_lspid);
+    target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
+#if defined(TARGET_I386)
+    target_md->msg_stime = tswap32(host_md->msg_stime);
+    target_md->msg_rtime = tswap32(host_md->msg_rtime);
+    target_md->msg_ctime = tswap32(host_md->msg_ctime);
+#else
+    target_md->msg_stime = tswap64(host_md->msg_stime);
+    target_md->msg_rtime = tswap64(host_md->msg_rtime);
+    target_md->msg_ctime = tswap64(host_md->msg_ctime);
+#endif
+    unlock_user_struct(target_md, target_addr, 1);
+
+    return 0;
+}

-- 
2.52.0
Re: [PATCH 12/24] bsd-user: Add host_to_target_msqid_ds for msgctl(2)
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 host_to_target_msqid_ds() to convert host struct msqid_ds to target
> format for msgctl(2) IPC_STAT operations.
> 
> Signed-off-by: Stacey Son <sson@FreeBSD.org>
> Signed-off-by: Brooks Davis <brooks@one-eyed-alien.net>
> Signed-off-by: Sean Bruno <sbruno@FreeBSD.org>
> Signed-off-by: Mikael Urankar <mikael.urankar@gmail.com>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
>   bsd-user/bsd-misc.c | 33 +++++++++++++++++++++++++++++++++
>   1 file changed, 33 insertions(+)
> 
> diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
> index e7031c5264..03773e2620 100644
> --- a/bsd-user/bsd-misc.c
> +++ b/bsd-user/bsd-misc.c
> @@ -187,3 +187,36 @@ abi_long target_to_host_msqid_ds(struct msqid_ds *host_md,
>   
>       return 0;
>   }
> +
> +abi_long host_to_target_msqid_ds(abi_ulong target_addr,
> +        struct msqid_ds *host_md)
> +{
> +    struct target_msqid_ds *target_md;
> +
> +    if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0)) {
> +        return -TARGET_EFAULT;
> +    }
> +
> +    memset(target_md, 0, sizeof(struct target_msqid_ds));
> +    host_to_target_ipc_perm__locked(&target_md->msg_perm,
> +                                    &host_md->msg_perm);
> +
> +    /* msg_first and msg_last are not used by IPC_SET/IPC_STAT in kernel. */
> +    target_md->msg_cbytes = tswapal(host_md->msg_cbytes);
> +    target_md->msg_qnum = tswapal(host_md->msg_qnum);
> +    target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
> +    target_md->msg_lspid = tswapal(host_md->msg_lspid);
> +    target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
> +#if defined(TARGET_I386)
> +    target_md->msg_stime = tswap32(host_md->msg_stime);
> +    target_md->msg_rtime = tswap32(host_md->msg_rtime);
> +    target_md->msg_ctime = tswap32(host_md->msg_ctime);
> +#else
> +    target_md->msg_stime = tswap64(host_md->msg_stime);
> +    target_md->msg_rtime = tswap64(host_md->msg_rtime);
> +    target_md->msg_ctime = tswap64(host_md->msg_ctime);
> +#endif
> +    unlock_user_struct(target_md, target_addr, 1);
> +
> +    return 0;
> +}
> 

Use __get_user and you won't need the ifdef, since the size of the type is then 
automatically handled.

r~
Re: [PATCH 12/24] bsd-user: Add host_to_target_msqid_ds for msgctl(2)
Posted by Warner Losh 2 days, 16 hours ago
On Thu, Feb 5, 2026 at 7:46 PM Richard Henderson <
richard.henderson@linaro.org> wrote:

> On 2/6/26 03:26, Warner Losh wrote:
> > From: Stacey Son <sson@FreeBSD.org>
> >
> > Add host_to_target_msqid_ds() to convert host struct msqid_ds to target
> > format for msgctl(2) IPC_STAT operations.
> >
> > Signed-off-by: Stacey Son <sson@FreeBSD.org>
> > Signed-off-by: Brooks Davis <brooks@one-eyed-alien.net>
> > Signed-off-by: Sean Bruno <sbruno@FreeBSD.org>
> > Signed-off-by: Mikael Urankar <mikael.urankar@gmail.com>
> > Signed-off-by: Warner Losh <imp@bsdimp.com>
> > ---
> >   bsd-user/bsd-misc.c | 33 +++++++++++++++++++++++++++++++++
> >   1 file changed, 33 insertions(+)
> >
> > diff --git a/bsd-user/bsd-misc.c b/bsd-user/bsd-misc.c
> > index e7031c5264..03773e2620 100644
> > --- a/bsd-user/bsd-misc.c
> > +++ b/bsd-user/bsd-misc.c
> > @@ -187,3 +187,36 @@ abi_long target_to_host_msqid_ds(struct msqid_ds
> *host_md,
> >
> >       return 0;
> >   }
> > +
> > +abi_long host_to_target_msqid_ds(abi_ulong target_addr,
> > +        struct msqid_ds *host_md)
> > +{
> > +    struct target_msqid_ds *target_md;
> > +
> > +    if (!lock_user_struct(VERIFY_WRITE, target_md, target_addr, 0)) {
> > +        return -TARGET_EFAULT;
> > +    }
> > +
> > +    memset(target_md, 0, sizeof(struct target_msqid_ds));
> > +    host_to_target_ipc_perm__locked(&target_md->msg_perm,
> > +                                    &host_md->msg_perm);
> > +
> > +    /* msg_first and msg_last are not used by IPC_SET/IPC_STAT in
> kernel. */
> > +    target_md->msg_cbytes = tswapal(host_md->msg_cbytes);
> > +    target_md->msg_qnum = tswapal(host_md->msg_qnum);
> > +    target_md->msg_qbytes = tswapal(host_md->msg_qbytes);
> > +    target_md->msg_lspid = tswapal(host_md->msg_lspid);
> > +    target_md->msg_lrpid = tswapal(host_md->msg_lrpid);
> > +#if defined(TARGET_I386)
> > +    target_md->msg_stime = tswap32(host_md->msg_stime);
> > +    target_md->msg_rtime = tswap32(host_md->msg_rtime);
> > +    target_md->msg_ctime = tswap32(host_md->msg_ctime);
> > +#else
> > +    target_md->msg_stime = tswap64(host_md->msg_stime);
> > +    target_md->msg_rtime = tswap64(host_md->msg_rtime);
> > +    target_md->msg_ctime = tswap64(host_md->msg_ctime);
> > +#endif
> > +    unlock_user_struct(target_md, target_addr, 1);
> > +
> > +    return 0;
> > +}
> >
>
> Use __get_user and you won't need the ifdef, since the size of the type is
> then
> automatically handled.
>

agreed,

Warner