QEMU-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
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>,
	Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
Subject: [PULL 23/25] bsd-user: Add do_bsd_ioctl main function
Date: Wed,  6 May 2026 20:28:25 -0600	[thread overview]
Message-ID: <20260507022827.44499-24-imp@bsdimp.com> (raw)
In-Reply-To: <20260507022827.44499-1-imp@bsdimp.com>

From: Stacey Son <sson@FreeBSD.org>

Add main ioctl emulation dispatcher that handles table-driven
ioctl translation with thunk-based structure conversion. Supports
TYPE_NULL, TYPE_INT, and TYPE_PTR argument types with read, write,
and read-write access modes.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/bsd-ioctl.c | 100 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git a/bsd-user/bsd-ioctl.c b/bsd-user/bsd-ioctl.c
index 8d25d84dbe..3940a69260 100644
--- a/bsd-user/bsd-ioctl.c
+++ b/bsd-user/bsd-ioctl.c
@@ -292,3 +292,103 @@ static abi_long do_ioctl_in6_ifreq_sockaddr_int(const IOCTLEntry *ie,
 
     return ret;
 }
+
+abi_long do_bsd_ioctl(int fd, abi_long cmd, abi_long arg)
+{
+    const IOCTLEntry *ie;
+    const argtype *arg_type;
+    abi_long ret;
+    uint8_t buf_temp[MAX_STRUCT_SIZE];
+    int target_size;
+    void *argptr;
+
+    ie = ioctl_entries;
+    for (;;) {
+        if (ie->target_cmd == 0) {
+            gemu_log("QEMU unsupported ioctl: ");
+            log_unsupported_ioctl(cmd);
+            return -TARGET_ENOSYS;
+        }
+        if (ie->target_cmd == cmd) {
+            break;
+        }
+        ie++;
+    }
+    arg_type = ie->arg_type;
+#if defined(DEBUG)
+    gemu_log("ioctl: cmd=0x%04lx (%s)\n", (long)cmd, ie->name);
+#endif
+    if (ie->do_ioctl) {
+        return ie->do_ioctl(ie, buf_temp, fd, cmd, arg);
+    }
+
+    switch (arg_type[0]) {
+    case TYPE_NULL:
+        /* no argument */
+        ret = get_errno(safe_ioctl(fd, ie->host_cmd));
+        break;
+
+    case TYPE_PTRVOID:
+    case TYPE_INT:
+        /* int argument */
+        ret = get_errno(safe_ioctl(fd, ie->host_cmd, arg));
+        break;
+
+    case TYPE_PTR:
+        arg_type++;
+        target_size = thunk_type_size(arg_type, 0);
+        switch (ie->access) {
+        case IOC_R:
+            ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
+            if (!is_error(ret)) {
+                argptr = lock_user(VERIFY_WRITE, arg,
+                    target_size, 0);
+                if (!argptr) {
+                    return -TARGET_EFAULT;
+                }
+                thunk_convert(argptr, buf_temp, arg_type,
+                    THUNK_TARGET);
+                unlock_user(argptr, arg, target_size);
+            }
+            break;
+
+        case IOC_W:
+            argptr = lock_user(VERIFY_READ, arg, target_size, 1);
+            if (!argptr) {
+                return -TARGET_EFAULT;
+            }
+            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
+            unlock_user(argptr, arg, 0);
+            ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
+            break;
+
+        case IOC_RW:
+            /* fallthrough */
+        default:
+            argptr = lock_user(VERIFY_READ, arg, target_size, 1);
+            if (!argptr) {
+                return -TARGET_EFAULT;
+            }
+            thunk_convert(buf_temp, argptr, arg_type, THUNK_HOST);
+            unlock_user(argptr, arg, 0);
+            ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp));
+            if (!is_error(ret)) {
+                argptr = lock_user(VERIFY_WRITE, arg, target_size, 0);
+                if (!argptr) {
+                    return -TARGET_EFAULT;
+                }
+                thunk_convert(argptr, buf_temp, arg_type, THUNK_TARGET);
+                unlock_user(argptr, arg, target_size);
+            }
+            break;
+        }
+        break;
+
+    default:
+        gemu_log("QEMU unknown ioctl: type=%d ", arg_type[0]);
+        log_unsupported_ioctl(cmd);
+        ret = -TARGET_ENOSYS;
+        break;
+    }
+    return ret;
+}
-- 
2.52.0



  parent reply	other threads:[~2026-05-07  2:32 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-07  2:28 [PULL 00/25] Bsd user 2026 05 patches Warner Losh
2026-05-07  2:28 ` [PULL 01/25] bsd-user: Switch to SPDX-License-Expression Warner Losh
2026-05-07  2:28 ` [PULL 02/25] bsd-user: Add syscall header generator for FreeBSD Warner Losh
2026-05-07  2:28 ` [PULL 03/25] bsd-user: Delete sbrk and sstk system calls Warner Losh
2026-05-07  2:28 ` [PULL 04/25] bsd-user: Create os-syscall.h Warner Losh
2026-05-07  2:28 ` [PULL 05/25] bsd-user: Switch to generated syscall_nr.h Warner Losh
2026-05-07  2:28 ` [PULL 06/25] bsd-user: Copy linux-user/thunk.c to bsd-user Warner Losh
2026-05-07  2:28 ` [PULL 07/25] bsd-user: ioctl: add common definitions Warner Losh
2026-05-07  2:28 ` [PULL 08/25] bsd-user: Add FreeBSD tty ioctl definitions Warner Losh
2026-05-07  2:28 ` [PULL 09/25] bsd-user: Add FreeBSD file I/O " Warner Losh
2026-05-07  2:28 ` [PULL 10/25] bsd-user: Add FreeBSD socket " Warner Losh
2026-05-07  2:28 ` [PULL 11/25] bsd-user: Add FreeBSD cryptodev " Warner Losh
2026-05-07  2:28 ` [PULL 12/25] bsd-user: Add FreeBSD disk " Warner Losh
2026-05-07  2:28 ` [PULL 13/25] bsd-user: Add FreeBSD IPv6 " Warner Losh
2026-05-07  2:28 ` [PULL 14/25] bsd-user: Add FreeBSD ioctl type definitions Warner Losh
2026-05-07  2:28 ` [PULL 15/25] bsd-user: Add FreeBSD ioctl command table Warner Losh
2026-05-07  2:28 ` [PULL 16/25] bsd-user: Add bsd-ioctl.h header Warner Losh
2026-05-07  2:28 ` [PULL 17/25] bsd-user: Add target_sockaddr and safe_ioctl to syscall_defs.h Warner Losh
2026-05-07  2:28 ` [PULL 18/25] bsd-user: Add bsd-ioctl.c infrastructure and termios conversion Warner Losh
2026-05-07  2:28 ` [PULL 19/25] bsd-user: Add log_unsupported_ioctl function Warner Losh
2026-05-07  2:28 ` [PULL 20/25] bsd-user: Add do_ioctl_unsupported function Warner Losh
2026-05-07  2:28 ` [PULL 21/25] bsd-user: Add target_to_host_sockaddr_in6 function Warner Losh
2026-05-07  2:28 ` [PULL 22/25] bsd-user: Add do_ioctl_in6_ifreq_sockaddr_int function Warner Losh
2026-05-07  2:28 ` Warner Losh [this message]
2026-05-07  2:28 ` [PULL 24/25] bsd-user: Add init_bsd_ioctl function Warner Losh
2026-05-07  2:28 ` [PULL 25/25] bsd-user: Add call to do_bsd_ioctl and add bsd-ioctl.c to the build Warner Losh
2026-05-11 14:21 ` [PULL 00/25] Bsd user 2026 05 patches Stefan Hajnoczi

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=20260507022827.44499-24-imp@bsdimp.com \
    --to=imp@bsdimp.com \
    --cc=kevans@freebsd.org \
    --cc=pierrick.bouvier@oss.qualcomm.com \
    --cc=qemu-devel@nongnu.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox