From d1296d7ca780daf7c54520831e6ebb5cbe0a3642 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 9 Oct 2025 17:06:22 -0400 Subject: kern: iotap: Expose I/O taps to userland Expose I/O taps to userland using a tap message interface. An application may construct a tap message to send to the kernel requesting data from a specific tap, if there is data, the buffers will be safely populated, otherwise some error returned. An example is accessing the PS/2 keyboard tap: -- char name[] = "i8042.port.0"; char buf[2] = {0, 0}; struct iotap_msg msg = { .opcode = IOTAP_OPC_READ, .buf = buf, .len = len }; ... /* muxtap() may be used */ ... -- Signed-off-by: Ian Moffett --- src/sys/include/compat/unix/syscall.h | 4 +- src/sys/include/os/iotap.h | 22 ++++++++ src/sys/include/sys/iotap.h | 58 +++++++++++++++++++ src/sys/include/sys/syscall.h | 1 + src/sys/os/os_iotap.c | 101 ++++++++++++++++++++++++++++++++++ 5 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 src/sys/include/sys/iotap.h diff --git a/src/sys/include/compat/unix/syscall.h b/src/sys/include/compat/unix/syscall.h index 5945f75..743e50c 100644 --- a/src/sys/include/compat/unix/syscall.h +++ b/src/sys/include/compat/unix/syscall.h @@ -34,6 +34,7 @@ #include #include #include +#include /* * Exit the current process - exit(2) syscall @@ -69,7 +70,8 @@ scret_t(*g_unix_sctab[])(struct syscall_args *) = { [SYS_query] = sys_query, [SYS_spawn] = sys_spawn, [SYS_mount] = sys_mount, - [SYS_open] = sys_open + [SYS_open] = sys_open, + [SYS_muxtap] = sys_muxtap }; #endif /* !_NEED_UNIX_SCTAB */ diff --git a/src/sys/include/os/iotap.h b/src/sys/include/os/iotap.h index 7421bf1..baef59c 100644 --- a/src/sys/include/os/iotap.h +++ b/src/sys/include/os/iotap.h @@ -32,6 +32,11 @@ #include #include +#include +#include +#include + +#define IOTAP_MSG_MAX 1024 struct iotap_desc; typedef int16_t iotap_t; @@ -79,4 +84,21 @@ iotap_t iotap_register(const struct iotap_desc *iotap); */ int iotap_lookup(const char *name, struct iotap_desc *dp_res); +/* + * Handle an I/O tap message and perform operations + * using specific commands. + * + * @name: Name of tap to operate on + * @msg: Message to send + * + * Returns less than zero values on failure, other + * values depend on the input command. + */ +ssize_t iotap_mux(const char *name, struct iotap_msg *msg); + +/* + * Perform an operation on an I/O tap + */ +scret_t sys_muxtap(struct syscall_args *scargs); + #endif /* !_OS_IOTAP_H_ */ diff --git a/src/sys/include/sys/iotap.h b/src/sys/include/sys/iotap.h new file mode 100644 index 0000000..10e4f82 --- /dev/null +++ b/src/sys/include/sys/iotap.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Ian Marco Moffett and L5 engineers + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_IOTAP_H_ +#define _SYS_IOTAP_H_ 1 + +#include +#include +#if !defined(_KERNEL) +#include +#include +#endif /* _KERNEL */ + +/* Valid I/O tap opcodes */ +#define IOTAP_OPC_READ 0x0000 + +/* + * An I/O tap message that can be send to + * the kernel to perform an operation on + * a tap. + * + * @opcode: Operation to be performed + * @buf: I/O buffer read/write operations + * @len: Length of I/O buffer + */ +struct __packed iotap_msg { + uint8_t opcode; + void *buf; + size_t len; +}; + +#endif /* _SYS_IOTAP_H_ */ diff --git a/src/sys/include/sys/syscall.h b/src/sys/include/sys/syscall.h index 181f734..09996ae 100644 --- a/src/sys/include/sys/syscall.h +++ b/src/sys/include/sys/syscall.h @@ -51,6 +51,7 @@ #define SYS_spawn 0x06 /* spawn a process */ #define SYS_mount 0x07 /* mount a filesystem */ #define SYS_open 0x08 /* open a file */ +#define SYS_muxtap 0x09 /* mux an I/O tap */ typedef __ssize_t scret_t; typedef __ssize_t scarg_t; diff --git a/src/sys/os/os_iotap.c b/src/sys/os/os_iotap.c index 6eb6ed2..238acfe 100644 --- a/src/sys/os/os_iotap.c +++ b/src/sys/os/os_iotap.c @@ -29,8 +29,11 @@ #include #include +#include #include #include +#include +#include #include #include #include @@ -102,3 +105,101 @@ iotap_lookup(const char *name, struct iotap_desc *dp_res) *dp_res = *tap; return 0; } + +ssize_t +iotap_mux(const char *name, struct iotap_msg *msg) +{ + struct iotap_ops *ops; + struct iotap_desc desc; + int error; + + if (msg == NULL) { + return -EINVAL; + } + + if (msg->buf == NULL || msg->len == 0) { + return -EINVAL; + } + + /* Lookup the tap */ + error = iotap_lookup(name, &desc); + if (error < 0) { + return error; + } + + ops = desc.ops; + switch (msg->opcode) { + case IOTAP_OPC_READ: + return ops->read(&desc, msg->buf, msg->len); + } + + return -EINVAL; +} + +/* + * Get an I/O TAP: + * + * ARG0: Name + * ARG1: Message + * + * RETURNED IN RAX: TAP ID + */ +scret_t +sys_muxtap(struct syscall_args *scargs) +{ + struct iotap_msg msg; + struct iotap_desc desc; + char *u_databuf; + char buf[NAME_MAX], *kbuf; + const char *u_name = SCARG(scargs, const char *, 0); + struct iotap_msg *u_msg = SCARG(scargs, struct iotap_msg *, 1); + int error; + + /* Grab the name */ + error = copyinstr(u_name, buf, sizeof(buf)); + if (error < 0) { + printf("gettap: bad address for name\n"); + return error; + } + + /* Get the message */ + error = copyin(u_msg, &msg, sizeof(msg)); + if (error < 0) { + printf("gettap: bad address for message\n"); + return error; + } + + /* Grab the actual tap */ + error = iotap_lookup(buf, &desc); + if (error < 0) { + printf("gettap: SYS_gettap lookup failure\n"); + return error; + } + + /* Truncate if needed */ + if (msg.len >= IOTAP_MSG_MAX) { + msg.len = IOTAP_MSG_MAX; + } + + /* Allocate a kernel-side buffer */ + kbuf = kalloc(msg.len); + if (kbuf == NULL) { + return -ENOMEM; + } + + /* Perform the operation */ + u_databuf = msg.buf; + msg.buf = kbuf; + error = iotap_mux(buf, &msg); + + /* + * If there are no errors, then we are free to + * copy the results back + */ + if (error > 0) { + copyout(kbuf, u_databuf, msg.len); + } + + kfree(kbuf); + return error; +} -- cgit v1.2.3