qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project.
@ 2023-04-21 16:53 Karim Taha
  2023-04-21 16:53 ` [PATCH v4 01/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com> Karim Taha
                   ` (10 more replies)
  0 siblings, 11 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Karim Taha

Upstream the implementations of bind(2), connect(2), accept(2) and
getpeername(2) system calls from the blitz branch of the bsd-user fork hosted at
https://github.com/qemu-bsd-user/qemu-bsd-user/tree/blitz.

Karim Taha (1):
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>

Sean Bruno (1):
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>

Stacey Son (7):
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>

Warner Losh (2):
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>

 bsd-user/bsd-socket.c         | 108 +++++++++++++++++++++++++
 bsd-user/bsd-socket.h         | 143 ++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  21 +++++
 bsd-user/main.c               |  16 +++-
 bsd-user/meson.build          |   1 +
 bsd-user/qemu-bsd.h           |  36 +++++++++
 bsd-user/syscall_defs.h       | 148 ++++++++++++++++++++++++++++++++++
 7 files changed, 472 insertions(+), 1 deletion(-)
 create mode 100644 bsd-user/bsd-socket.c
 create mode 100644 bsd-user/bsd-socket.h
 create mode 100644 bsd-user/qemu-bsd.h

-- 
2.40.0



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

* [PATCH v4 01/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-24  8:30   ` Daniel P. Berrangé
  2023-04-21 16:53 ` [PATCH v4 02/11] " Karim Taha
                   ` (9 subsequent siblings)
  10 siblings, 1 reply; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Karim Taha

From: Warner Losh <imp@bsdimp.com>

Intialize guest_base in bsd-user/main.c.

Allow guest_base to be initialized on 64-bit hosts, the initial value is used by g2h_untagged function defined in include/exec/cpu_ldst.h

Signed-off-by: Warner Losh <imp@bsdimp.com>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/main.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index babc3b009b..afdc1b5f3c 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -50,8 +50,22 @@
 #include "target_arch_cpu.h"
 
 int singlestep;
-uintptr_t guest_base;
+
+/*
+ * Going hand in hand with the va space needed (see below), we need
+ * to find a host address to map the guest to. Assume that qemu
+ * itself doesn't need memory above 32GB (or that we don't collide
+ * with anything interesting). This is selected rather arbitrarily,
+ * but seems to produce good results in tests to date.
+ */
+# if HOST_LONG_BITS >= 64
+uintptr_t guest_base = 0x800000000ul;    /* at 32GB */
+bool have_guest_base = true;
+#else
+uintptr_t guest_base;    /* TODO: use sysctl to find big enough hole */
 bool have_guest_base;
+#endif
+
 /*
  * When running 32-on-64 we should make sure we can fit all of the possible
  * guest address space into a contiguous chunk of virtual host memory.
-- 
2.40.0



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

* [PATCH v4 02/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
  2023-04-21 16:53 ` [PATCH v4 01/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com> Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 03/11] " Karim Taha
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Add the socket conversion related flags and structs.

Add the relevant definitions of struct target_sockaddr and struct
target_ip_mreq and the related flags, to be used in
bsd-user/bsd-socket.c for the socket conversion functions:
target_to_host_sockaddr, host_to_target_sockaddr, target_to_host_ip_mreq

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

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index b6d113d24a..f041245792 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -179,6 +179,116 @@ struct target_freebsd__wrusage {
     struct target_freebsd_rusage wru_children;
 };
 
+/*
+ * sys/socket.h
+ */
+
+/*
+ * Types
+ */
+#define TARGET_SOCK_STREAM      1   /* stream socket */
+#define TARGET_SOCK_DGRAM       2   /* datagram socket */
+#define TARGET_SOCK_RAW         3   /* raw-protocol interface */
+#define TARGET_SOCK_RDM         4   /* reliably-delivered message */
+#define TARGET_SOCK_SEQPACKET   5   /* sequenced packet stream */
+
+
+/*
+ * Option flags per-socket.
+ */
+
+#define TARGET_SO_DEBUG         0x0001  /* turn on debugging info recording */
+#define TARGET_SO_ACCEPTCONN    0x0002  /* socket has had listen() */
+#define TARGET_SO_REUSEADDR     0x0004  /* allow local address reuse */
+#define TARGET_SO_KEEPALIVE     0x0008  /* keep connections alive */
+#define TARGET_SO_DONTROUTE     0x0010  /* just use interface addresses */
+#define TARGET_SO_BROADCAST     0x0020  /* permit sending of broadcast msgs */
+#define TARGET_SO_USELOOPBACK   0x0040  /* bypass hardware when possible */
+#define TARGET_SO_LINGER        0x0080  /* linger on close if data present */
+#define TARGET_SO_OOBINLINE     0x0100  /* leave received OOB data in line */
+#define TARGET_SO_REUSEPORT     0x0200  /* allow local address & port reuse */
+#define TARGET_SO_TIMESTAMP     0x0400  /* timestamp received dgram traffic */
+#define TARGET_SO_NOSIGPIPE     0x0800  /* no SIGPIPE from EPIPE */
+#define TARGET_SO_ACCEPTFILTER  0x1000  /* there is an accept filter */
+#define TARGET_SO_BINTIME       0x2000  /* timestamp received dgram traffic */
+#define TARGET_SO_NO_OFFLOAD    0x4000  /* socket cannot be offloaded */
+#define TARGET_SO_NO_DDP        0x8000  /* disable direct data placement */
+
+/*
+ * Additional options, not kept in so_options.
+ */
+#define TARGET_SO_SNDBUF        0x1001  /* send buffer size */
+#define TARGET_SO_RCVBUF        0x1002  /* receive buffer size */
+#define TARGET_SO_SNDLOWAT      0x1003  /* send low-water mark */
+#define TARGET_SO_RCVLOWAT      0x1004  /* receive low-water mark */
+#define TARGET_SO_SNDTIMEO      0x1005  /* send timeout */
+#define TARGET_SO_RCVTIMEO      0x1006  /* receive timeout */
+#define TARGET_SO_ERROR         0x1007  /* get error status and clear */
+#define TARGET_SO_TYPE          0x1008  /* get socket type */
+#define TARGET_SO_LABEL         0x1009  /* socket's MAC label */
+#define TARGET_SO_PEERLABEL     0x1010  /* socket's peer's MAC label */
+#define TARGET_SO_LISTENQLIMIT  0x1011  /* socket's backlog limit */
+#define TARGET_SO_LISTENQLEN    0x1012  /* socket's complete queue length */
+#define TARGET_SO_LISTENINCQLEN 0x1013  /* socket's incomplete queue length */
+#define TARGET_SO_SETFIB        0x1014  /* use this FIB to route */
+#define TARGET_SO_USER_COOKIE   0x1015  /* user cookie (dummynet etc.) */
+#define TARGET_SO_PROTOCOL      0x1016  /* get socket protocol (Linux name) */
+
+/* alias for SO_PROTOCOL (SunOS name) */
+#define TARGET_SO_PROTOTYPE     TARGET_SO_PROTOCOL
+
+/*
+ * Level number for (get/set)sockopt() to apply to socket itself.
+ */
+#define TARGET_SOL_SOCKET       0xffff  /* options for socket level */
+
+#ifndef CMSG_ALIGN
+#define CMSG_ALIGN(len) (((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
+#endif
+
+/*
+ * sys/socket.h
+ */
+struct target_msghdr {
+    abi_long    msg_name;       /* Socket name */
+    int32_t     msg_namelen;    /* Length of name */
+    abi_long    msg_iov;        /* Data blocks */
+    int32_t     msg_iovlen;     /* Number of blocks */
+    abi_long    msg_control;    /* Per protocol magic (eg BSD fd passing) */
+    int32_t     msg_controllen; /* Length of cmsg list */
+    int32_t     msg_flags;      /* flags on received message */
+};
+
+struct target_sockaddr {
+    uint8_t sa_len;
+    uint8_t sa_family;
+    uint8_t sa_data[14];
+} QEMU_PACKED;
+
+struct target_in_addr {
+    uint32_t s_addr; /* big endian */
+};
+
+struct target_cmsghdr {
+    uint32_t    cmsg_len;
+    int32_t     cmsg_level;
+    int32_t     cmsg_type;
+};
+
+/*
+ * netinet/in.h
+ */
+struct target_ip_mreq {
+    struct target_in_addr   imr_multiaddr;
+    struct target_in_addr   imr_interface;
+};
+
+struct target_ip_mreqn {
+    struct target_in_addr   imr_multiaddr;
+    struct target_in_addr   imr_address;
+    int32_t                 imr_ifindex;
+};
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
-- 
2.40.0



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

* [PATCH v4 03/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
  2023-04-21 16:53 ` [PATCH v4 01/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com> Karim Taha
  2023-04-21 16:53 ` [PATCH v4 02/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 04/11] " Karim Taha
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Sean Bruno, Kyle Evans, Karim Taha

From: Sean Bruno <sbruno@FreeBSD.org>

Target cmsghdr struct and flags.

Add the cmsghdr struct and alignment flags.

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

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index f041245792..b594fafecc 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -275,6 +275,44 @@ struct target_cmsghdr {
     int32_t     cmsg_type;
 };
 
+/*
+ * mips32 is the exception to the general rule of long-alignment; it
+ * unconditionally uses 64-bit alignment instead.
+ */
+#if defined(TARGET_MIPS) && TARGET_ABI_BITS == 32
+#define TARGET_ALIGNBYTES   (sizeof(abi_llong) - 1)
+#else
+#define TARGET_ALIGNBYTES   (sizeof(abi_long) - 1)
+#endif
+
+#define TARGET_CMSG_NXTHDR(mhdr, cmsg, cmsg_start) \
+                               __target_cmsg_nxthdr(mhdr, cmsg, cmsg_start)
+#define TARGET_CMSG_ALIGN(len) (((len) + TARGET_ALIGNBYTES) \
+                               & (size_t) ~TARGET_ALIGNBYTES)
+#define TARGET_CMSG_DATA(cmsg) \
+    ((unsigned char *)(cmsg) + TARGET_CMSG_ALIGN(sizeof(struct target_cmsghdr)))
+#define TARGET_CMSG_SPACE(len) \
+    (TARGET_CMSG_ALIGN(sizeof(struct target_cmsghdr)) + TARGET_CMSG_ALIGN(len))
+#define TARGET_CMSG_LEN(len) \
+    (TARGET_CMSG_ALIGN(sizeof(struct target_cmsghdr)) + (len))
+
+static inline struct target_cmsghdr *
+__target_cmsg_nxthdr(struct target_msghdr *__mhdr,
+                     struct target_cmsghdr *__cmsg,
+                     struct target_cmsghdr *__cmsg_start)
+{
+    struct target_cmsghdr *__ptr;
+
+    __ptr = (struct target_cmsghdr *)((unsigned char *) __cmsg +
+        TARGET_CMSG_ALIGN(tswap32(__cmsg->cmsg_len)));
+    if ((unsigned long)((char *)(__ptr + 1) - (char *)__cmsg_start) >
+        tswap32(__mhdr->msg_controllen)) {
+        /* No more entries.  */
+        return (struct target_cmsghdr *)0;
+    }
+    return __ptr;
+}
+
 /*
  * netinet/in.h
  */
-- 
2.40.0



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

* [PATCH v4 04/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
                   ` (2 preceding siblings ...)
  2023-04-21 16:53 ` [PATCH v4 03/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 05/11] " Karim Taha
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Declaration of the socket conversion functions.

Add bsd-user/qemu-bsd.h, required by bsd-user/bsd-socket.h, contains
forward declarations of the socket conversion functions defined in bsd-user/bsd-socket.c.

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

diff --git a/bsd-user/qemu-bsd.h b/bsd-user/qemu-bsd.h
new file mode 100644
index 0000000000..a052688596
--- /dev/null
+++ b/bsd-user/qemu-bsd.h
@@ -0,0 +1,36 @@
+/*
+ *  BSD conversion extern declarations
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_BSD_H
+#define QEMU_BSD_H
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+
+/* bsd-socket.c */
+abi_long target_to_host_sockaddr(struct sockaddr *addr, abi_ulong target_addr,
+        socklen_t len);
+abi_long host_to_target_sockaddr(abi_ulong target_addr, struct sockaddr *addr,
+        socklen_t len);
+abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn, abi_ulong target_addr,
+        socklen_t len);
+
+#endif /* QEMU_BSD_H */
-- 
2.40.0



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

* [PATCH v4 05/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
                   ` (3 preceding siblings ...)
  2023-04-21 16:53 ` [PATCH v4 04/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 06/11] " Karim Taha
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

Definitions of the socket conversion functions.

Add bsd-user/bsd-socket.c, which contains the actual definitions of the
socket conversion functions.

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

diff --git a/bsd-user/bsd-socket.c b/bsd-user/bsd-socket.c
new file mode 100644
index 0000000000..8a5e44444d
--- /dev/null
+++ b/bsd-user/bsd-socket.c
@@ -0,0 +1,108 @@
+/*
+ *  BSD socket system call related helpers
+ *
+ *  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 <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+
+#include "qemu.h"
+#include "qemu-bsd.h"
+
+/*
+ * socket conversion
+ */
+abi_long target_to_host_sockaddr(struct sockaddr *addr, abi_ulong target_addr,
+                                 socklen_t len)
+{
+    const socklen_t unix_maxlen = sizeof(struct sockaddr_un);
+    sa_family_t sa_family;
+    struct target_sockaddr *target_saddr;
+
+    target_saddr = lock_user(VERIFY_READ, target_addr, len, 1);
+    if (target_saddr == 0) {
+        return -TARGET_EFAULT;
+    }
+
+    sa_family = target_saddr->sa_family;
+
+    /*
+     * Oops. The caller might send a incomplete sun_path; sun_path
+     * must be terminated by \0 (see the manual page), but unfortunately
+     * it is quite common to specify sockaddr_un length as
+     * "strlen(x->sun_path)" while it should be "strlen(...) + 1". We will
+     * fix that here if needed.
+     */
+    if (target_saddr->sa_family == AF_UNIX) {
+        if (len < unix_maxlen && len > 0) {
+            char *cp = (char *)target_saddr;
+
+            if (cp[len - 1] && !cp[len]) {
+                len++;
+            }
+        }
+        if (len > unix_maxlen) {
+            len = unix_maxlen;
+        }
+    }
+
+    memcpy(addr, target_saddr, len);
+    addr->sa_family = sa_family;        /* type uint8_t */
+    addr->sa_len = target_saddr->sa_len;    /* type uint8_t */
+    unlock_user(target_saddr, target_addr, 0);
+
+    return 0;
+}
+
+abi_long host_to_target_sockaddr(abi_ulong target_addr, struct sockaddr *addr,
+                                 socklen_t len)
+{
+    struct target_sockaddr *target_saddr;
+
+    target_saddr = lock_user(VERIFY_WRITE, target_addr, len, 0);
+    if (target_saddr == 0) {
+        return -TARGET_EFAULT;
+    }
+    memcpy(target_saddr, addr, len);
+    target_saddr->sa_family = addr->sa_family;  /* type uint8_t */
+    target_saddr->sa_len = addr->sa_len;        /* type uint8_t */
+    unlock_user(target_saddr, target_addr, len);
+
+    return 0;
+}
+
+abi_long target_to_host_ip_mreq(struct ip_mreqn *mreqn, abi_ulong target_addr,
+                                socklen_t len)
+{
+    struct target_ip_mreqn *target_smreqn;
+
+    target_smreqn = lock_user(VERIFY_READ, target_addr, len, 1);
+    if (target_smreqn == 0) {
+        return -TARGET_EFAULT;
+    }
+    mreqn->imr_multiaddr.s_addr = target_smreqn->imr_multiaddr.s_addr;
+    mreqn->imr_address.s_addr = target_smreqn->imr_address.s_addr;
+    if (len == sizeof(struct target_ip_mreqn)) {
+        mreqn->imr_ifindex = tswapal(target_smreqn->imr_ifindex);
+    }
+    unlock_user(target_smreqn, target_addr, 0);
+
+    return 0;
+}
-- 
2.40.0



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

* [PATCH v4 06/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
                   ` (4 preceding siblings ...)
  2023-04-21 16:53 ` [PATCH v4 05/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 07/11] " Karim Taha
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Karim Taha

Build bsd-user/bsd-socket.c.

Add bsd-user/bsd-socket.c to meson.build.

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

diff --git a/bsd-user/meson.build b/bsd-user/meson.build
index 5243122fc5..f648bd3554 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-socket.c',
   'bsdload.c',
   'elfload.c',
   'main.c',
-- 
2.40.0



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

* [PATCH v4 07/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
                   ` (5 preceding siblings ...)
  2023-04-21 16:53 ` [PATCH v4 06/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 08/11] " Karim Taha
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

The implementation of bind(2) syscall and socket related syscalls.

Add bsd-user/bsd-socket.h, which contains the implementation of
bind(2), and the socket related system call shims.

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

diff --git a/bsd-user/bsd-socket.h b/bsd-user/bsd-socket.h
new file mode 100644
index 0000000000..7da4cf11a0
--- /dev/null
+++ b/bsd-user/bsd-socket.h
@@ -0,0 +1,61 @@
+/*
+ *  socket related system call shims
+ *
+ *  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 BSD_SOCKET_H
+#define BSD_SOCKET_H
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+
+#include "qemu-bsd.h"
+
+ssize_t safe_recvfrom(int s, void *buf, size_t len, int flags,
+    struct sockaddr *restrict from, socklen_t *restrict fromlen);
+ssize_t safe_sendto(int s, const void *buf, size_t len, int flags,
+    const struct sockaddr *to, socklen_t tolen);
+int safe_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds,
+    struct timeval *timeout);
+int safe_pselect(int nfds, fd_set *restrict readfds,
+    fd_set *restrict writefds, fd_set *restrict exceptfds,
+    const struct timespec *restrict timeout,
+    const sigset_t *restrict newsigmask);
+
+/* bind(2) */
+static inline abi_long do_bsd_bind(int sockfd, abi_ulong target_addr,
+                                   socklen_t addrlen)
+{
+    abi_long ret;
+    void *addr;
+
+    if ((int)addrlen < 0) {
+        return -TARGET_EINVAL;
+    }
+
+    addr = alloca(addrlen + 1);
+    ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    return get_errno(bind(sockfd, addr, addrlen));
+}
+
+#endif /* BSD_SOCKET_H */
-- 
2.40.0



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

* [PATCH v4 08/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
                   ` (6 preceding siblings ...)
  2023-04-21 16:53 ` [PATCH v4 07/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 09/11] " Karim Taha
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

connect(2) syscall.

Add the connect(2) syscall to bsd-user/bsd-socket.h.

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

diff --git a/bsd-user/bsd-socket.h b/bsd-user/bsd-socket.h
index 7da4cf11a0..f191f22d63 100644
--- a/bsd-user/bsd-socket.h
+++ b/bsd-user/bsd-socket.h
@@ -58,4 +58,25 @@ static inline abi_long do_bsd_bind(int sockfd, abi_ulong target_addr,
     return get_errno(bind(sockfd, addr, addrlen));
 }
 
+/* connect(2) */
+static inline abi_long do_bsd_connect(int sockfd, abi_ulong target_addr,
+                                      socklen_t addrlen)
+{
+    abi_long ret;
+    void *addr;
+
+    if ((int)addrlen < 0) {
+        return -TARGET_EINVAL;
+    }
+    addr = alloca(addrlen + 1);
+
+    ret = target_to_host_sockaddr(addr, target_addr, addrlen);
+
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    return get_errno(connect(sockfd, addr, addrlen));
+}
+
 #endif /* BSD_SOCKET_H */
-- 
2.40.0



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

* [PATCH v4 09/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
                   ` (7 preceding siblings ...)
  2023-04-21 16:53 ` [PATCH v4 08/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 10/11] " Karim Taha
  2023-04-21 16:53 ` [PATCH v4 11/11] " Karim Taha
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

accept(2) syscall.

Add the accept(2) syscall to bsd-user/bsd-socket.h.

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

diff --git a/bsd-user/bsd-socket.h b/bsd-user/bsd-socket.h
index f191f22d63..f748266730 100644
--- a/bsd-user/bsd-socket.h
+++ b/bsd-user/bsd-socket.h
@@ -79,4 +79,37 @@ static inline abi_long do_bsd_connect(int sockfd, abi_ulong target_addr,
     return get_errno(connect(sockfd, addr, addrlen));
 }
 
+/* accept(2) */
+static inline abi_long do_bsd_accept(int fd, abi_ulong target_addr,
+                                     abi_ulong target_addrlen_addr)
+{
+    socklen_t addrlen;
+    void *addr;
+    abi_long ret;
+
+    if (target_addr == 0) {
+        return get_errno(accept(fd, NULL, NULL));
+    }
+    /* return EINVAL if addrlen pointer is invalid */
+    if (get_user_u32(addrlen, target_addrlen_addr)) {
+        return -TARGET_EINVAL;
+    }
+    if ((int)addrlen < 0) {
+        return -TARGET_EINVAL;
+    }
+    if (!access_ok(VERIFY_WRITE, target_addr, addrlen)) {
+        return -TARGET_EINVAL;
+    }
+    addr = alloca(addrlen);
+
+    ret = get_errno(accept(fd, addr, &addrlen));
+    if (!is_error(ret)) {
+        host_to_target_sockaddr(target_addr, addr, addrlen);
+        if (put_user_u32(addrlen, target_addrlen_addr)) {
+            ret = -TARGET_EFAULT;
+        }
+    }
+    return ret;
+}
+
 #endif /* BSD_SOCKET_H */
-- 
2.40.0



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

* [PATCH v4 10/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
                   ` (8 preceding siblings ...)
  2023-04-21 16:53 ` [PATCH v4 09/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 16:53 ` [PATCH v4 11/11] " Karim Taha
  10 siblings, 0 replies; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Stacey Son, Karim Taha

From: Stacey Son <sson@FreeBSD.org>

getpeername(2) syscall.

Add the getpeername(2) syscall to bsd-user/bsd-socket.h.

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

diff --git a/bsd-user/bsd-socket.h b/bsd-user/bsd-socket.h
index f748266730..16fae3752a 100644
--- a/bsd-user/bsd-socket.h
+++ b/bsd-user/bsd-socket.h
@@ -112,4 +112,32 @@ static inline abi_long do_bsd_accept(int fd, abi_ulong target_addr,
     return ret;
 }
 
+/* getpeername(2) */
+static inline abi_long do_bsd_getpeername(int fd, abi_ulong target_addr,
+                                          abi_ulong target_addrlen_addr)
+{
+    socklen_t addrlen;
+    void *addr;
+    abi_long ret;
+
+    if (get_user_u32(addrlen, target_addrlen_addr)) {
+        return -TARGET_EFAULT;
+    }
+    if ((int)addrlen < 0) {
+        return -TARGET_EINVAL;
+    }
+    if (!access_ok(VERIFY_WRITE, target_addr, addrlen)) {
+        return -TARGET_EFAULT;
+    }
+    addr = alloca(addrlen);
+    ret = get_errno(getpeername(fd, addr, &addrlen));
+    if (!is_error(ret)) {
+        host_to_target_sockaddr(target_addr, addr, addrlen);
+        if (put_user_u32(addrlen, target_addrlen_addr)) {
+            ret = -TARGET_EFAULT;
+        }
+    }
+    return ret;
+}
+
 #endif /* BSD_SOCKET_H */
-- 
2.40.0



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

* [PATCH v4 11/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
                   ` (9 preceding siblings ...)
  2023-04-21 16:53 ` [PATCH v4 10/11] " Karim Taha
@ 2023-04-21 16:53 ` Karim Taha
  2023-04-21 22:46   ` Warner Losh
  10 siblings, 1 reply; 14+ messages in thread
From: Karim Taha @ 2023-04-21 16:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: imp, Karim Taha

From: Warner Losh <imp@bsdimp.com>

Add the dispatching code of bind(2),connect(2), accpet(2), getpeername(2).

Add the bind(2), connect(2), accept(2), getpeername(2) syscalls case
statements to freebsd_syscall function defined in bsd-user/freebsd/os-syscall.c

Signed-off-by: Warner Losh <imp@bsdimp.com>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/freebsd/os-syscall.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index c8f998ecec..7f29196a05 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -44,6 +44,8 @@
 #include "signal-common.h"
 #include "user/syscall-trace.h"
 
+/* BSD independent syscall shims */
+#include "bsd-socket.h"
 #include "bsd-file.h"
 #include "bsd-proc.h"
 
@@ -508,6 +510,25 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
         break;
 
+        /*
+         * socket related system calls
+         */
+    case TARGET_FREEBSD_NR_accept: /* accept(2) */
+        ret = do_bsd_accept(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_bind: /* bind(2) */
+        ret = do_bsd_bind(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_connect: /* connect(2) */
+        ret = do_bsd_connect(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_getpeername: /* getpeername(2) */
+        ret = do_bsd_getpeername(arg1, arg2, arg3);
+        break;
+
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
         ret = -TARGET_ENOSYS;
-- 
2.40.0



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

* Re: [PATCH v4 11/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 ` [PATCH v4 11/11] " Karim Taha
@ 2023-04-21 22:46   ` Warner Losh
  0 siblings, 0 replies; 14+ messages in thread
From: Warner Losh @ 2023-04-21 22:46 UTC (permalink / raw)
  To: Karim Taha; +Cc: qemu-devel, Kyle Evans

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

Oh, I see you've uploaded an improved version of the patches, with the
fixes I'd
recommended. I'll queue that series instead.

Warner

On Fri, Apr 21, 2023 at 10:58 AM Karim Taha <kariem.taha2.7@gmail.com>
wrote:

> From: Warner Losh <imp@bsdimp.com>
>
> Add the dispatching code of bind(2),connect(2), accpet(2), getpeername(2).
>
> Add the bind(2), connect(2), accept(2), getpeername(2) syscalls case
> statements to freebsd_syscall function defined in
> bsd-user/freebsd/os-syscall.c
>
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>  bsd-user/freebsd/os-syscall.c | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>
> diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
> index c8f998ecec..7f29196a05 100644
> --- a/bsd-user/freebsd/os-syscall.c
> +++ b/bsd-user/freebsd/os-syscall.c
> @@ -44,6 +44,8 @@
>  #include "signal-common.h"
>  #include "user/syscall-trace.h"
>
> +/* BSD independent syscall shims */
> +#include "bsd-socket.h"
>  #include "bsd-file.h"
>  #include "bsd-proc.h"
>
> @@ -508,6 +510,25 @@ static abi_long freebsd_syscall(void *cpu_env, int
> num, abi_long arg1,
>          ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
>          break;
>
> +        /*
> +         * socket related system calls
> +         */
> +    case TARGET_FREEBSD_NR_accept: /* accept(2) */
> +        ret = do_bsd_accept(arg1, arg2, arg3);
> +        break;
> +
> +    case TARGET_FREEBSD_NR_bind: /* bind(2) */
> +        ret = do_bsd_bind(arg1, arg2, arg3);
> +        break;
> +
> +    case TARGET_FREEBSD_NR_connect: /* connect(2) */
> +        ret = do_bsd_connect(arg1, arg2, arg3);
> +        break;
> +
> +    case TARGET_FREEBSD_NR_getpeername: /* getpeername(2) */
> +        ret = do_bsd_getpeername(arg1, arg2, arg3);
> +        break;
> +
>      default:
>          qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
>          ret = -TARGET_ENOSYS;
> --
> 2.40.0
>
>

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

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

* Re: [PATCH v4 01/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
  2023-04-21 16:53 ` [PATCH v4 01/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com> Karim Taha
@ 2023-04-24  8:30   ` Daniel P. Berrangé
  0 siblings, 0 replies; 14+ messages in thread
From: Daniel P. Berrangé @ 2023-04-24  8:30 UTC (permalink / raw)
  To: Karim Taha; +Cc: qemu-devel, imp

You've not fully addressed the feedback on v3.  The first line
of the commit message needs to be a short sumary, not the
Signed-off-by, which should be at the end of the commit
message

On Fri, Apr 21, 2023 at 06:53:41PM +0200, Karim Taha wrote:
> From: Warner Losh <imp@bsdimp.com>
> 
> Intialize guest_base in bsd-user/main.c.
> 
> Allow guest_base to be initialized on 64-bit hosts, the initial value is used by g2h_untagged function defined in include/exec/cpu_ldst.h

You also need to put line breaks in the commit message to
keep it at around 72 chars

> 
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
> ---
>  bsd-user/main.c | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/bsd-user/main.c b/bsd-user/main.c
> index babc3b009b..afdc1b5f3c 100644
> --- a/bsd-user/main.c
> +++ b/bsd-user/main.c
> @@ -50,8 +50,22 @@
>  #include "target_arch_cpu.h"
>  
>  int singlestep;
> -uintptr_t guest_base;
> +
> +/*
> + * Going hand in hand with the va space needed (see below), we need
> + * to find a host address to map the guest to. Assume that qemu
> + * itself doesn't need memory above 32GB (or that we don't collide
> + * with anything interesting). This is selected rather arbitrarily,
> + * but seems to produce good results in tests to date.
> + */
> +# if HOST_LONG_BITS >= 64
> +uintptr_t guest_base = 0x800000000ul;    /* at 32GB */
> +bool have_guest_base = true;
> +#else
> +uintptr_t guest_base;    /* TODO: use sysctl to find big enough hole */
>  bool have_guest_base;
> +#endif
> +
>  /*
>   * When running 32-on-64 we should make sure we can fit all of the possible
>   * guest address space into a contiguous chunk of virtual host memory.
> -- 
> 2.40.0
> 
> 

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



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

end of thread, other threads:[~2023-04-24  8:31 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-21 16:53 [PATCH v4 00/11] Contribution task implementations, for the 'FreeBSD user emulation improvements' project Karim Taha
2023-04-21 16:53 ` [PATCH v4 01/11] Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com> Karim Taha
2023-04-24  8:30   ` Daniel P. Berrangé
2023-04-21 16:53 ` [PATCH v4 02/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 03/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 04/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 05/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 06/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 07/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 08/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 09/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 10/11] " Karim Taha
2023-04-21 16:53 ` [PATCH v4 11/11] " Karim Taha
2023-04-21 22:46   ` Warner Losh

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