From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: "Warner Losh" <imp@bsdimp.com>, "Kyle Evans" <kevans@freebsd.org>,
"Stacey Son" <sson@FreeBSD.org>,
"Mikaël Urankar" <mikael.urankar@gmail.com>,
"Kyle Evans" <kevans@FreeBSD.org>,
"Richard Henderson" <richard.henderson@linaro.org>
Subject: [PULL 24/41] bsd-user: Add select, pselect, and ppoll to os-time.h
Date: Sat, 19 Sep 2026 10:47:52 -0600 [thread overview]
Message-ID: <20260919164810.70151-25-imp@bsdimp.com> (raw)
In-Reply-To: <20260919164810.70151-1-imp@bsdimp.com>
Add I/O multiplexing system call shims: select, pselect, and ppoll
with proper fd_set, timespec/timeval, and signal mask conversion.
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Mikaël Urankar <mikael.urankar@gmail.com>
Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Assisted-by: Claude Opus 4.6 (1M context)
Acked-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
bsd-user/freebsd/os-time.h | 202 +++++++++++++++++++++++++++++++++++++
1 file changed, 202 insertions(+)
diff --git a/bsd-user/freebsd/os-time.h b/bsd-user/freebsd/os-time.h
index 06104a5b7d..5f537fac40 100644
--- a/bsd-user/freebsd/os-time.h
+++ b/bsd-user/freebsd/os-time.h
@@ -433,5 +433,207 @@ static inline abi_long do_freebsd_ktimer_gettime(abi_long arg1, abi_long arg2)
return ret;
}
+/* select(2) */
+static inline abi_long do_freebsd_select(CPUArchState *env, int n,
+ abi_ulong rfd_addr, abi_ulong wfd_addr, abi_ulong efd_addr,
+ abi_ulong target_tv_addr)
+{
+ fd_set rfds, wfds, efds;
+ fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
+ struct timeval tv, *tvp;
+ abi_long ret, error;
+
+ ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
+ if (ret != 0) {
+ return ret;
+ }
+ ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
+ if (ret != 0) {
+ return ret;
+ }
+ ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
+ if (ret != 0) {
+ return ret;
+ }
+
+ if (target_tv_addr != 0) {
+ if (t2h_freebsd_timeval(&tv, target_tv_addr)) {
+ return -TARGET_EFAULT;
+ }
+ tvp = &tv;
+ } else {
+ tvp = NULL;
+ }
+
+ ret = get_errno(safe_select(n, rfds_ptr, wfds_ptr, efds_ptr, tvp));
+
+ if (!is_error(ret)) {
+ if (rfd_addr != 0) {
+ error = copy_to_user_fdset(rfd_addr, &rfds, n);
+ if (error != 0) {
+ return error;
+ }
+ }
+ if (wfd_addr != 0) {
+ error = copy_to_user_fdset(wfd_addr, &wfds, n);
+ if (error != 0) {
+ return error;
+ }
+ }
+ if (efd_addr != 0) {
+ error = copy_to_user_fdset(efd_addr, &efds, n);
+ if (error != 0) {
+ return error;
+ }
+ }
+ if (target_tv_addr != 0) {
+ error = h2t_freebsd_timeval(&tv, target_tv_addr);
+ if (is_error(error)) {
+ return error;
+ }
+ }
+ }
+ return ret;
+}
+
+/* pselect(2) */
+static inline abi_long do_freebsd_pselect(CPUArchState *env, int n,
+ abi_ulong rfd_addr, abi_ulong wfd_addr, abi_ulong efd_addr,
+ abi_ulong ts_addr, abi_ulong set_addr)
+{
+ CPUState *cpu = env_cpu(env);
+ TaskState *tstate = cpu->opaque;
+ fd_set rfds, wfds, efds;
+ fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
+ sigset_t *set_ptr;
+ struct timespec ts, *ts_ptr;
+ void *p;
+ abi_long ret, error;
+
+ ret = copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n);
+ if (is_error(ret)) {
+ return ret;
+ }
+ ret = copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n);
+ if (is_error(ret)) {
+ return ret;
+ }
+ ret = copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n);
+ if (is_error(ret)) {
+ return ret;
+ }
+
+ /* Unlike select(), pselect() uses struct timespec instead of timeval */
+ if (ts_addr) {
+ if (t2h_freebsd_timespec(&ts, ts_addr)) {
+ return -TARGET_EFAULT;
+ }
+ ts_ptr = &ts;
+ } else {
+ ts_ptr = NULL;
+ }
+
+ if (set_addr != 0) {
+ p = lock_user(VERIFY_READ, set_addr, sizeof(target_sigset_t), 1);
+ if (p == NULL) {
+ return -TARGET_EFAULT;
+ }
+ target_to_host_sigset(&tstate->sigsuspend_mask, p);
+ unlock_user(p, set_addr, 0);
+ set_ptr = &tstate->sigsuspend_mask;
+ } else {
+ set_ptr = NULL;
+ }
+
+ ret = get_errno(safe_pselect(n, rfds_ptr, wfds_ptr, efds_ptr, ts_ptr,
+ set_ptr));
+ if (ret != -TARGET_ERESTART) {
+ tstate->in_sigsuspend = true;
+ }
+ if (!is_error(ret)) {
+ if (rfd_addr != 0) {
+ error = copy_to_user_fdset(rfd_addr, &rfds, n);
+ if (is_error(error)) {
+ return error;
+ }
+ }
+ if (wfd_addr != 0) {
+ error = copy_to_user_fdset(wfd_addr, &wfds, n);
+ if (is_error(error)) {
+ return error;
+ }
+ }
+ if (efd_addr != 0) {
+ error = copy_to_user_fdset(efd_addr, &efds, n);
+ if (is_error(error)) {
+ return error;
+ }
+ }
+ }
+ return ret;
+}
+
+/* ppoll(2) */
+static inline abi_long do_freebsd_ppoll(CPUArchState *env, abi_long arg1,
+ abi_long arg2, abi_ulong arg3, abi_ulong arg4)
+{
+ CPUState *cpu = env_cpu(env);
+ TaskState *tstate = cpu->opaque;
+ abi_long ret;
+ nfds_t i, nfds = arg2;
+ struct pollfd *pfd;
+ struct target_pollfd *target_pfd;
+ struct timespec ts, *ts_ptr;
+ sigset_t *set_ptr;
+ void *p;
+
+ target_pfd = lock_user(VERIFY_WRITE, arg1,
+ sizeof(struct target_pollfd) * nfds, 1);
+ if (!target_pfd) {
+ return -TARGET_EFAULT;
+ }
+ pfd = alloca(sizeof(struct pollfd) * nfds);
+ for (i = 0; i < nfds; i++) {
+ pfd[i].fd = tswap32(target_pfd[i].fd);
+ pfd[i].events = tswap16(target_pfd[i].events);
+ }
+
+ /* Unlike poll(), ppoll() uses struct timespec. */
+ if (arg3) {
+ if (t2h_freebsd_timespec(&ts, arg3)) {
+ return -TARGET_EFAULT;
+ }
+ ts_ptr = &ts;
+ } else {
+ ts_ptr = NULL;
+ }
+
+ if (arg4 != 0) {
+ p = lock_user(VERIFY_READ, arg4, sizeof(target_sigset_t), 1);
+ if (p == NULL) {
+ return -TARGET_EFAULT;
+ }
+ target_to_host_sigset(&tstate->sigsuspend_mask, p);
+ unlock_user(p, arg4, 0);
+ set_ptr = &tstate->sigsuspend_mask;
+ } else {
+ set_ptr = NULL;
+ }
+
+ ret = get_errno(ppoll(pfd, nfds, ts_ptr, set_ptr));
+ if (ret != -TARGET_ERESTART) {
+ tstate->in_sigsuspend = true;
+ }
+ if (!is_error(ret)) {
+ for (i = 0; i < nfds; i++) {
+ target_pfd[i].revents = tswap16(pfd[i].revents);
+ }
+ }
+ unlock_user(target_pfd, arg1, sizeof(struct target_pollfd) * nfds);
+
+ return ret;
+}
+
+/* kqueue(2) */
#endif /* FREEBSD_OS_TIME_H */
--
2.55.0
next prev parent reply other threads:[~2026-09-19 16:51 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-19 16:47 [PULL 00/41] Misc 2026q2 v4 patches Warner Losh
2026-09-19 16:47 ` [PULL 01/41] bsd-user: Upstream most of the remaining system calls Warner Losh
2026-09-19 16:47 ` [PULL 02/41] bsd-user: Rename cpu_env to env throughout bsd-user Warner Losh
2026-09-19 16:47 ` [PULL 03/41] bsd-user: Fix copy direciton in preadv Warner Losh
2026-09-19 16:47 ` [PULL 04/41] bsd-user: Add bsd-signal.h with sigaction through sigreturn Warner Losh
2026-09-19 16:47 ` [PULL 05/41] bsd-user: Add signal shims sigwait through kill and os-signal.h Warner Losh
2026-09-19 16:47 ` [PULL 06/41] bsd-user: Add signal system call dispatch Warner Losh
2026-09-19 16:47 ` [PULL 07/41] bsd-user: move sys/shm.h lines Warner Losh
2026-09-19 16:47 ` [PULL 08/41] bsd-user: Remove mmap defines Warner Losh
2026-09-19 16:47 ` [PULL 09/41] bsd-user: Use target_suseconds_t Warner Losh
2026-09-19 16:47 ` [PULL 10/41] bsd-user: Move sys/uuid.h defines Warner Losh
2026-09-19 16:47 ` [PULL 11/41] bsd-user: Fix realpathat for FreeBSD 15.0 and newer Warner Losh
2026-09-19 16:47 ` [PULL 12/41] bsd-user: Add os-file.h with chflagsat, close_range, and more Warner Losh
2026-09-19 16:47 ` [PULL 13/41] bsd-user: Add poll, poll2, lseek, pipe, and swap system call shims Warner Losh
2026-09-19 16:47 ` [PULL 14/41] bsd-user: Add FreeBSD socket helpers and sockaddr conversion Warner Losh
2026-09-19 16:47 ` [PULL 15/41] bsd-user: Add bsd-socket.h with bind through getsockname Warner Losh
2026-09-19 16:47 ` [PULL 16/41] bsd-user: Add socket shims socketpair through shutdown Warner Losh
2026-09-19 16:47 ` [PULL 17/41] bsd-user: Commit the generated os-sockopt.h files Warner Losh
2026-09-19 16:47 ` [PULL 18/41] bsd-user: Implement both getsockopt and setsockopt Warner Losh
2026-09-19 16:47 ` [PULL 19/41] bsd-user: Add FreeBSD socket system calls Warner Losh
2026-09-19 16:47 ` [PULL 20/41] bsd-user: Add os-socket.[ch] sendmsg, recvmsg and cmsg Warner Losh
2026-09-19 16:47 ` [PULL 21/41] bsd-user: Add socket system call dispatch Warner Losh
2026-09-19 16:47 ` [PULL 22/41] bsd-user: Add os-time.h with clock and time-of-day functions Warner Losh
2026-09-19 16:47 ` [PULL 23/41] bsd-user: Add utimes, futimes, and ktimer functions to os-time.h Warner Losh
2026-09-19 16:47 ` Warner Losh [this message]
2026-09-19 16:47 ` [PULL 25/41] bsd-user: Add kqueue and kevent " Warner Losh
2026-09-19 16:47 ` [PULL 26/41] bsd-user: Add sigtimedwait, itimer, and futimens " Warner Losh
2026-09-19 16:47 ` [PULL 27/41] bsd-user: Add os-time.c and time-related type definitions Warner Losh
2026-09-19 16:47 ` [PULL 28/41] bsd-user: Add time system call dispatch Warner Losh
2026-09-19 16:47 ` [PULL 29/41] bsd-user: Add cpu_copy and make init_task_state non-static Warner Losh
2026-09-19 16:47 ` [PULL 30/41] bsd-user: Add os-thread.h with thr and context functions Warner Losh
2026-09-19 16:47 ` [PULL 31/41] bsd-user: Add do_freebsd__umtx_op to os-thread.h Warner Losh
2026-09-19 16:48 ` [PULL 32/41] bsd-user: Add thread system call dispatch Warner Losh
2026-09-19 16:48 ` [PULL 33/41] bsd-user: Add os-extattr.h with file and fd extattr functions Warner Losh
2026-09-19 16:48 ` [PULL 34/41] bsd-user: Add link and list extattr functions to os-extattr.h Warner Losh
2026-09-19 16:48 ` [PULL 35/41] bsd-user: Add ACL system call shims " Warner Losh
2026-09-19 16:48 ` [PULL 36/41] bsd-user: Add os-extattr.c with ACL conversion functions Warner Losh
2026-09-19 16:48 ` [PULL 37/41] bsd-user: Add extended attribute and ACL system call dispatch Warner Losh
2026-09-19 16:48 ` [PULL 38/41] bsd-user: Add scheduler and cpuset functions to os-misc.h Warner Losh
2026-09-19 16:48 ` [PULL 39/41] bsd-user: Add scheduler, cpuset, kmod, and misc syscall dispatch Warner Losh
2026-09-19 16:48 ` [PULL 40/41] bsd-user: Move qemu_guest_random_seed_main to same as linux-user Warner Losh
2026-09-19 16:48 ` [PULL 41/41] bsd-user Update random_seed on thread creation Warner Losh
-- strict thread matches above, loose matches on Subject: below --
2026-09-18 4:23 [PULL 00/41] Misc 2026q2 v4 patches Warner Losh
2026-09-18 4:24 ` [PULL 24/41] bsd-user: Add select, pselect, and ppoll to os-time.h Warner Losh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260919164810.70151-25-imp@bsdimp.com \
--to=imp@bsdimp.com \
--cc=kevans@freebsd.org \
--cc=mikael.urankar@gmail.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=sson@FreeBSD.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.