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>, Kyle Evans <kevans@FreeBSD.org>,
Jessica Clarke <jrtc27@jrtc27.com>,
Michal Meloun <mmel@FreeBSD.org>,
Richard Henderson <richard.henderson@linaro.org>
Subject: [PULL 30/41] bsd-user: Add os-thread.h with thr and context functions
Date: Sat, 19 Sep 2026 10:47:58 -0600 [thread overview]
Message-ID: <20260919164810.70151-31-imp@bsdimp.com> (raw)
In-Reply-To: <20260919164810.70151-1-imp@bsdimp.com>
Add thread management system call shims: thr_self, thr_exit, thr_kill,
thr_kill2, thr_suspend, thr_wake, thr_set_name, rtprio_thread,
getcontext, setcontext, and swapcontext.
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
Signed-off-by: Michal Meloun <mmel@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-syscall.c | 6 +
bsd-user/freebsd/os-thread.h | 244 ++++++++++++++++++++++++++++++++++
2 files changed, 250 insertions(+)
create mode 100644 bsd-user/freebsd/os-thread.h
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index cc66be3b7c..f2c3ff25e8 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -49,6 +49,7 @@
#include "os-file.h"
#include "os-socket.h"
#include "os-time.h"
+#include "os-thread.h"
#include "os-misc.h"
/* I/O */
@@ -90,6 +91,11 @@ safe_syscall6(ssize_t, sendto, int, fd, const void *, buf, size_t, len, int,
safe_syscall3(ssize_t, recvmsg, int, s, struct msghdr *, msg, int, flags);
safe_syscall3(ssize_t, sendmsg, int, s, const struct msghdr *, msg, int, flags);
+/* used in os-thread */
+safe_syscall1(int, thr_suspend, struct timespec *, timeout);
+safe_syscall5(int, _umtx_op, void *, obj, int, op, unsigned long, val, void *,
+ uaddr, void *, uaddr2);
+
/* used in os-time */
safe_syscall2(int, nanosleep, const struct timespec *, rqtp, struct timespec *,
rmtp);
diff --git a/bsd-user/freebsd/os-thread.h b/bsd-user/freebsd/os-thread.h
new file mode 100644
index 0000000000..d3f1f01b13
--- /dev/null
+++ b/bsd-user/freebsd/os-thread.h
@@ -0,0 +1,244 @@
+/*
+ * FreeBSD thread and user mutex related system call shims
+ *
+ * Copyright (c) 2013-2015 Stacey D. Son
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef FREEBSD_OS_THREAD_H
+#define FREEBSD_OS_THREAD_H
+
+#include <sys/thr.h>
+#include <sys/rtprio.h>
+#include <sys/umtx.h>
+
+#include "qemu.h"
+#include "qemu-os.h"
+
+int safe_thr_suspend(struct timespec *timeout);
+int safe__umtx_op(void *, int, unsigned long, void *, void *);
+
+#if defined(HOST_BIG_ENDIAN) == defined(TARGET_BIG_ENDIAN) && \
+ (TARGET_ABI_BITS == HOST_LONG_BITS || defined(UMTX_OP__32BIT))
+#define _UMTX_OPTIMIZED
+#if defined(TARGET_ABI32)
+#define QEMU_UMTX_OP(n) (UMTX_OP__32BIT | (n))
+#else
+#define QEMU_UMTX_OP(n) (n)
+#endif /* TARGET_ABI32 */
+#else
+#define QEMU_UMTX_OP(n) (n)
+#endif
+
+static inline abi_long do_freebsd_thr_self(abi_ulong target_id)
+{
+ abi_long ret;
+ long tid;
+
+ ret = get_errno(thr_self(&tid));
+ if (!is_error(ret)) {
+ if (put_user_sal(tid, target_id)) {
+ return -TARGET_EFAULT;
+ }
+ }
+ return ret;
+}
+
+static inline abi_long do_freebsd_thr_exit(CPUArchState *env,
+ abi_ulong tid_addr)
+{
+ CPUState *cpu = env_cpu(env);
+ TaskState *ts;
+
+ if (block_signals()) {
+ return -TARGET_ERESTART;
+ }
+
+ pthread_mutex_lock(new_freebsd_thread_lock_ptr);
+
+ ts = cpu->opaque;
+
+ if (tid_addr) {
+ /* Signal target userland that it can free the stack. */
+ if (!put_user_sal(1, tid_addr)) {
+ freebsd_umtx_wake_unsafe(tid_addr, INT_MAX);
+ }
+ }
+
+ object_unparent(OBJECT(env_cpu(env)));
+ object_unref(OBJECT(env_cpu(env)));
+ /*
+ * At this point the CPU should be unrealized and removed
+ * from cpu lists. We can clean-up the rest of the thread
+ * data without the lock held.
+ */
+
+ pthread_mutex_unlock(new_freebsd_thread_lock_ptr);
+
+ thread_cpu = NULL;
+ g_free(ts);
+ rcu_unregister_thread();
+ pthread_exit(NULL);
+ /* Doesn't return */
+ return 0;
+}
+
+static inline abi_long do_freebsd_thr_kill(long id, int sig)
+{
+
+ return get_errno(thr_kill(id, target_to_host_signal(sig)));
+}
+
+static inline abi_long do_freebsd_thr_kill2(pid_t pid, long id, int sig)
+{
+
+ return get_errno(thr_kill2(pid, id, target_to_host_signal(sig)));
+}
+
+static inline abi_long do_freebsd_thr_suspend(abi_ulong target_ts)
+{
+ abi_long ret;
+ struct timespec ts;
+
+ if (target_ts != 0) {
+ if (t2h_freebsd_timespec(&ts, target_ts)) {
+ return -TARGET_EFAULT;
+ }
+ ret = get_errno(safe_thr_suspend(&ts));
+ } else {
+ ret = get_errno(safe_thr_suspend(NULL));
+ }
+ return ret;
+}
+
+static inline abi_long do_freebsd_thr_wake(long tid)
+{
+
+ return get_errno(thr_wake(tid));
+}
+
+static inline abi_long do_freebsd_thr_set_name(long tid, abi_ulong target_name)
+{
+ abi_long ret;
+ void *p;
+
+ p = lock_user_string(target_name);
+ if (p == NULL) {
+ return -TARGET_EFAULT;
+ }
+ ret = get_errno(thr_set_name(tid, p));
+ unlock_user(p, target_name, 0);
+
+ return ret;
+}
+
+static inline abi_long do_freebsd_rtprio_thread(int function, lwpid_t lwpid,
+ abi_ulong target_addr)
+{
+ int ret;
+ struct rtprio rtp;
+
+ ret = t2h_freebsd_rtprio(&rtp, target_addr);
+ if (!is_error(ret)) {
+ ret = get_errno(rtprio_thread(function, lwpid, &rtp));
+ }
+ if (!is_error(ret)) {
+ ret = h2t_freebsd_rtprio(target_addr, &rtp);
+ }
+ return ret;
+}
+
+static inline abi_long do_freebsd_getcontext(CPUArchState *env, abi_ulong arg1)
+{
+ abi_long ret;
+ target_ucontext_t *ucp;
+ sigset_t sigmask;
+
+ if (arg1 == 0) {
+ return -TARGET_EINVAL;
+ }
+ ret = do_sigprocmask(0, NULL, &sigmask);
+ if (!is_error(ret)) {
+ ucp = lock_user(VERIFY_WRITE, arg1, sizeof(target_ucontext_t), 0);
+ if (ucp == 0) {
+ return -TARGET_EFAULT;
+ }
+ ret = get_mcontext(env, &ucp->uc_mcontext, TARGET_MC_GET_CLEAR_RET);
+ host_to_target_sigset(&ucp->uc_sigmask, &sigmask);
+ memset(ucp->__spare__, 0, sizeof(ucp->__spare__));
+ unlock_user(ucp, arg1, sizeof(target_ucontext_t));
+ }
+ return ret;
+}
+
+static inline abi_long do_freebsd_setcontext(CPUArchState *env, abi_ulong arg1)
+{
+ abi_long ret;
+ target_ucontext_t *ucp;
+ sigset_t sigmask;
+ if (arg1 == 0) {
+ return -TARGET_EINVAL;
+ }
+ ucp = lock_user(VERIFY_READ, arg1, sizeof(target_ucontext_t), 1);
+ if (ucp == 0) {
+ return -TARGET_EFAULT;
+ }
+ ret = set_mcontext(env, &ucp->uc_mcontext, 0);
+ target_to_host_sigset(&sigmask, &ucp->uc_sigmask);
+ unlock_user(ucp, arg1, 0);
+ if (!is_error(ret)) {
+ (void)do_sigprocmask(SIG_SETMASK, &sigmask, NULL);
+ }
+ return ret == 0 ? -TARGET_EJUSTRETURN : ret;
+}
+
+/* swapcontext(2) */
+static inline abi_long do_freebsd_swapcontext(CPUArchState *env, abi_ulong arg1,
+ abi_ulong arg2)
+{
+ abi_long ret;
+ target_ucontext_t *ucp;
+ sigset_t sigmask;
+
+ if (arg1 == 0 || arg2 == 0) {
+ return -TARGET_EINVAL;
+ }
+ /* Save current context in arg1. */
+ ret = do_sigprocmask(0, NULL, &sigmask);
+ if (!is_error(ret)) {
+ ucp = lock_user(VERIFY_WRITE, arg1, sizeof(target_ucontext_t), 0);
+ if (ucp == 0) {
+ return -TARGET_EFAULT;
+ }
+ ret = get_mcontext(env, &ucp->uc_mcontext, TARGET_MC_GET_CLEAR_RET);
+ host_to_target_sigset(&ucp->uc_sigmask, &sigmask);
+ memset(ucp->__spare__, 0, sizeof(ucp->__spare__));
+ unlock_user(ucp, arg1, sizeof(target_ucontext_t));
+ }
+ if (is_error(ret)) {
+ return ret;
+ }
+
+ /* Restore the context in arg2 to the current context. */
+ ucp = lock_user(VERIFY_READ, arg2, sizeof(target_ucontext_t), 1);
+ if (ucp == 0) {
+ return -TARGET_EFAULT;
+ }
+ ret = set_mcontext(env, &ucp->uc_mcontext, 0);
+ target_to_host_sigset(&sigmask, &ucp->uc_sigmask);
+ unlock_user(ucp, arg2, 0);
+ if (!is_error(ret)) {
+ (void)do_sigprocmask(SIG_SETMASK, &sigmask, NULL);
+ }
+ return ret == 0 ? -TARGET_EJUSTRETURN : ret;
+}
+
+
+#define safe_g2h_untagged(x) ((x) != 0 ? g2h_untagged(x) : NULL)
+
+/*
+ * undocumented _umtx_op(void *obj, int op, u_long val, void *uaddr,
+ * void *target_time);
+ */
+
+#endif /* FREEBSD_OS_THREAD_H */
--
2.55.0
next prev parent reply other threads:[~2026-09-19 16:53 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 ` [PULL 24/41] bsd-user: Add select, pselect, and ppoll " Warner Losh
2026-09-19 16:47 ` [PULL 25/41] bsd-user: Add kqueue and kevent functions " 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 ` Warner Losh [this message]
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 30/41] bsd-user: Add os-thread.h with thr and context functions 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-31-imp@bsdimp.com \
--to=imp@bsdimp.com \
--cc=jrtc27@jrtc27.com \
--cc=kevans@freebsd.org \
--cc=mmel@FreeBSD.org \
--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.