From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: Kyle Evans <kevans@freebsd.org>, Warner Losh <imp@bsdimp.com>,
Stacey Son <sson@FreeBSD.org>,
Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>,
Richard Henderson <richard.henderson@linaro.org>
Subject: [PULL 12/41] bsd-user: Add os-file.h with chflagsat, close_range, and more
Date: Thu, 17 Sep 2026 22:23:53 -0600 [thread overview]
Message-ID: <20260918042422.38713-13-imp@bsdimp.com> (raw)
In-Reply-To: <20260918042422.38713-1-imp@bsdimp.com>
Add FreeBSD-specific file operation shims: chflagsat, close_range,
copy_file_range, and __specialfd. Also add the safe_copy_file_range and
safe_ppoll syscall wrappers and the target_specialfd_eventfd type
definitions.
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Assisted-by: Claude Opus 4.6 (1M context)
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Acked-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
bsd-user/freebsd/os-file.h | 103 ++++++++++++++++++++++++++++++++++
bsd-user/freebsd/os-syscall.c | 25 +++++++++
bsd-user/syscall_defs.h | 10 ++++
3 files changed, 138 insertions(+)
create mode 100644 bsd-user/freebsd/os-file.h
diff --git a/bsd-user/freebsd/os-file.h b/bsd-user/freebsd/os-file.h
new file mode 100644
index 0000000000..d5d91336ea
--- /dev/null
+++ b/bsd-user/freebsd/os-file.h
@@ -0,0 +1,103 @@
+/*
+ * FreeBSD file related system call shims and definitions
+ *
+ * Copyright (c) 2014 Stacey D. Son
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef FREEBSD_OS_FILE_H
+#define FREEBSD_OS_FILE_H
+
+#include <sys/specialfd.h>
+
+/*
+ * Asynchronous I/O.
+ */
+
+/* chflagsat(2) */
+static inline abi_long do_bsd_chflagsat(int fd, abi_ulong path,
+ abi_ulong flags, int atflags)
+{
+ abi_long ret;
+ void *p;
+
+ LOCK_PATH(p, path);
+ ret = get_errno(chflagsat(fd, p, flags, atflags)); /* XXX path(p)? */
+ UNLOCK_PATH(p, path);
+
+ return ret;
+}
+
+/* close_range(2) */
+static inline abi_long do_freebsd_close_range(unsigned int lowfd,
+ unsigned int highfd, int flags)
+{
+
+ return get_errno(close_range(lowfd, highfd, flags));
+}
+
+ssize_t safe_copy_file_range(int, off_t *, int, off_t *, size_t, unsigned int);
+
+/* copy_file_range(2) */
+static inline abi_long do_freebsd_copy_file_range(int infd,
+ abi_ulong inofftp, int outfd, abi_ulong outofftp, size_t len,
+ unsigned int flags)
+{
+ off_t inoff, outoff, *inp, *outp;
+ abi_long ret;
+
+ inp = outp = NULL;
+ if (inofftp != 0 && !access_ok(VERIFY_WRITE, inofftp, sizeof(off_t))) {
+ return -TARGET_EFAULT;
+ } else if (inofftp != 0) {
+ inoff = tswap64(*(off_t *)g2h_untagged(inofftp));
+ inp = &inoff;
+ }
+ if (outofftp != 0 && !access_ok(VERIFY_WRITE, outofftp, sizeof(off_t))) {
+ return -TARGET_EFAULT;
+ } else if (outofftp != 0) {
+ outoff = tswap64(*(off_t *)g2h_untagged(outofftp));
+ outp = &outoff;
+ }
+
+ ret = get_errno(safe_copy_file_range(infd, inp, outfd, outp, len,
+ flags));
+
+ if (!is_error(ret)) {
+ if (inofftp != 0) {
+ *(off_t *)g2h_untagged(inofftp) = tswap64(inoff);
+ }
+ if (outofftp != 0) {
+ *(off_t *)g2h_untagged(outofftp) = tswap64(outoff);
+ }
+ }
+ return ret;
+}
+
+static inline abi_long do_freebsd___specialfd(int type, abi_ulong req,
+ size_t len)
+{
+ abi_long ret;
+
+ ret = -TARGET_EINVAL;
+ switch (type) {
+ case TARGET_SPECIALFD_EVENT: {
+ struct specialfd_eventfd evfd;
+ struct target_specialfd_eventfd *target_eventfd;
+
+ if (!lock_user_struct(VERIFY_READ, target_eventfd, req, 1)) {
+ return -TARGET_EFAULT;
+ }
+
+ evfd.initval = tswap32(target_eventfd->initval);
+ evfd.flags = tswap32(target_eventfd->flags);
+ ret = get_errno(syscall(SYS___specialfd, type, &evfd, sizeof(evfd)));
+ unlock_user_struct(target_eventfd, req, 0);
+ break;
+ }
+ }
+
+ return ret;
+}
+
+#endif /* FREEBSD_OS_FILE_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 06148fe3ce..4fc230e8e5 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -44,6 +44,7 @@
#include "os-stat.h"
#include "os-proc.h"
#include "os-signal.h"
+#include "os-file.h"
#include "os-misc.h"
/* I/O */
@@ -65,6 +66,9 @@ safe_syscall3(ssize_t, writev, int, fd, const struct iovec *, iov, int, iovcnt);
safe_syscall4(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, iovcnt,
off_t, offset);
+safe_syscall6(ssize_t, copy_file_range, int, infd, off_t *, inoffp, int, outfd,
+ off_t *, outoffp, size_t, len, unsigned int, flags);
+
/* used in os-proc */
safe_syscall4(pid_t, wait4, pid_t, wpid, int *, status, int, options,
struct rusage *, rusage);
@@ -692,6 +696,27 @@ static abi_long freebsd_syscall(CPUArchState *env, int num, abi_long arg1,
ret = do_bsd_undelete(arg1);
break;
+ case TARGET_FREEBSD_NR_chflagsat: /* chflagsat(2) */
+ ret = do_bsd_chflagsat(arg1, arg2, arg3, arg4);
+ break;
+
+ case TARGET_FREEBSD_NR_close_range: /* close_range(2) */
+ ret = do_freebsd_close_range(arg1, arg2, arg3);
+ break;
+
+ case TARGET_FREEBSD_NR___realpathat:
+ /* __realpathat(2) (XXX no realpathat()) */
+ ret = do_freebsd_realpathat(arg1, arg2, arg3, arg4, arg5);
+ break;
+
+ case TARGET_FREEBSD_NR_copy_file_range:
+ ret = do_freebsd_copy_file_range(arg1, arg2, arg3, arg4, arg5, arg6);
+ break;
+
+ case TARGET_FREEBSD_NR___specialfd:
+ ret = do_freebsd___specialfd(arg1, arg2, arg3);
+ break;
+
/*
* ioctl(2)
*/
diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index 47aa01fbb5..88dd7a763a 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -418,6 +418,16 @@ struct target_freebsd_flock {
/* user: vfork(2) semantics, clear signals */
#define TARGET_RFSPAWN (1U << 31)
+/* sys/specialfd.h */
+enum target_specialfd_type {
+ TARGET_SPECIALFD_EVENT = 1,
+};
+
+struct target_specialfd_eventfd {
+ unsigned int initval;
+ int flags;
+};
+
/*
* sys/uuid.h
*/
--
2.55.0
next prev parent reply other threads:[~2026-09-18 4:26 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-18 4:23 [PULL 00/41] Misc 2026q2 v4 patches Warner Losh
2026-09-18 4:23 ` [PULL 01/41] bsd-user: Upstream most of the remaining system calls Warner Losh
2026-09-18 4:23 ` [PULL 02/41] bsd-user: Rename cpu_env to env throughout bsd-user Warner Losh
2026-09-18 4:23 ` [PULL 03/41] bsd-user: Fix copy direciton in preadv Warner Losh
2026-09-18 4:23 ` [PULL 04/41] bsd-user: Add bsd-signal.h with sigaction through sigreturn Warner Losh
2026-09-18 4:23 ` [PULL 05/41] bsd-user: Add signal shims sigwait through kill and os-signal.h Warner Losh
2026-09-18 4:23 ` [PULL 06/41] bsd-user: Add signal system call dispatch Warner Losh
2026-09-18 4:23 ` [PULL 07/41] bsd-user: move sys/shm.h lines Warner Losh
2026-09-18 4:23 ` [PULL 08/41] bsd-user: Remove mmap defines Warner Losh
2026-09-18 4:23 ` [PULL 09/41] bsd-user: Use target_suseconds_t Warner Losh
2026-09-18 4:23 ` [PULL 10/41] bsd-user: Move sys/uuid.h defines Warner Losh
2026-09-18 4:23 ` [PULL 11/41] bsd-user: Fix realpathat for FreeBSD 15.0 and newer Warner Losh
2026-09-18 4:23 ` Warner Losh [this message]
2026-09-18 4:23 ` [PULL 13/41] bsd-user: Add poll, poll2, lseek, pipe, and swap system call shims Warner Losh
2026-09-18 4:23 ` [PULL 14/41] bsd-user: Add FreeBSD socket helpers and sockaddr conversion Warner Losh
2026-09-18 4:23 ` [PULL 15/41] bsd-user: Add bsd-socket.h with bind through getsockname Warner Losh
2026-09-18 4:23 ` [PULL 16/41] bsd-user: Add socket shims socketpair through shutdown Warner Losh
2026-09-18 4:23 ` [PULL 17/41] bsd-user: Commit the generated os-sockopt.h files Warner Losh
2026-09-18 4:23 ` [PULL 18/41] bsd-user: Implement both getsockopt and setsockopt Warner Losh
2026-09-18 4:24 ` [PULL 19/41] bsd-user: Add FreeBSD socket system calls Warner Losh
2026-09-18 4:24 ` [PULL 20/41] bsd-user: Add os-socket.[ch] sendmsg, recvmsg and cmsg Warner Losh
2026-09-18 4:24 ` [PULL 21/41] bsd-user: Add socket system call dispatch Warner Losh
2026-09-18 4:24 ` [PULL 22/41] bsd-user: Add os-time.h with clock and time-of-day functions Warner Losh
2026-09-18 4:24 ` [PULL 23/41] bsd-user: Add utimes, futimes, and ktimer functions to os-time.h Warner Losh
2026-09-18 4:24 ` [PULL 24/41] bsd-user: Add select, pselect, and ppoll " Warner Losh
2026-09-18 4:24 ` [PULL 25/41] bsd-user: Add kqueue and kevent functions " Warner Losh
2026-09-18 4:24 ` [PULL 26/41] bsd-user: Add sigtimedwait, itimer, and futimens " Warner Losh
2026-09-18 4:24 ` [PULL 27/41] bsd-user: Add os-time.c and time-related type definitions Warner Losh
2026-09-18 4:24 ` [PULL 28/41] bsd-user: Add time system call dispatch Warner Losh
2026-09-18 4:24 ` [PULL 29/41] bsd-user: Add cpu_copy and make init_task_state non-static Warner Losh
2026-09-18 4:24 ` [PULL 30/41] bsd-user: Add os-thread.h with thr and context functions Warner Losh
2026-09-18 4:24 ` [PULL 31/41] bsd-user: Add do_freebsd__umtx_op to os-thread.h Warner Losh
2026-09-18 4:24 ` [PULL 32/41] bsd-user: Add thread system call dispatch Warner Losh
2026-09-18 4:24 ` [PULL 33/41] bsd-user: Add os-extattr.h with file and fd extattr functions Warner Losh
2026-09-18 4:24 ` [PULL 34/41] bsd-user: Add link and list extattr functions to os-extattr.h Warner Losh
2026-09-18 4:24 ` [PULL 35/41] bsd-user: Add ACL system call shims " Warner Losh
2026-09-18 4:24 ` [PULL 36/41] bsd-user: Add os-extattr.c with ACL conversion functions Warner Losh
2026-09-18 4:24 ` [PULL 37/41] bsd-user: Add extended attribute and ACL system call dispatch Warner Losh
2026-09-18 4:24 ` [PULL 38/41] bsd-user: Add scheduler and cpuset functions to os-misc.h Warner Losh
2026-09-18 4:24 ` [PULL 39/41] bsd-user: Add scheduler, cpuset, kmod, and misc syscall dispatch Warner Losh
2026-09-18 4:24 ` [PULL 40/41] bsd-user: Move qemu_guest_random_seed_main to same as linux-user Warner Losh
2026-09-18 4:24 ` [PULL 41/41] bsd-user Update random_seed on thread creation Warner Losh
2026-09-18 19:19 ` [PULL 00/41] Misc 2026q2 v4 patches Richard Henderson
2026-09-19 16:49 ` Warner Losh
-- strict thread matches above, loose matches on Subject: below --
2026-09-19 16:47 Warner Losh
2026-09-19 16:47 ` [PULL 12/41] bsd-user: Add os-file.h with chflagsat, close_range, and more 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=20260918042422.38713-13-imp@bsdimp.com \
--to=imp@bsdimp.com \
--cc=kevans@freebsd.org \
--cc=pierrick.bouvier@oss.qualcomm.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.