From 3b3325f761b09ebbfef04c44eed546cc4fdeb329 Mon Sep 17 00:00:00 2001 From: Kacper Date: Mon, 15 Dec 2025 02:01:33 +0100 Subject: Added aio and eventfd support, along with sleep and yes utilities --- lib/libc/sys/Kbuild | 6 +++ lib/libc/sys/eventfd.c | 6 +++ lib/libc/sys/eventfd_read.c | 8 ++++ lib/libc/sys/eventfd_write.c | 8 ++++ lib/libc/sys/io_uring_enter.c | 10 +++++ lib/libc/sys/io_uring_register.c | 7 ++++ lib/libc/sys/io_uring_setup.c | 81 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 126 insertions(+) create mode 100644 lib/libc/sys/eventfd.c create mode 100644 lib/libc/sys/eventfd_read.c create mode 100644 lib/libc/sys/eventfd_write.c create mode 100644 lib/libc/sys/io_uring_enter.c create mode 100644 lib/libc/sys/io_uring_register.c create mode 100644 lib/libc/sys/io_uring_setup.c (limited to 'lib/libc/sys') diff --git a/lib/libc/sys/Kbuild b/lib/libc/sys/Kbuild index 1bf1dcba..fe9d0bfc 100644 --- a/lib/libc/sys/Kbuild +++ b/lib/libc/sys/Kbuild @@ -1,3 +1,9 @@ +obj-y += eventfd_read.o +obj-y += eventfd_write.o +obj-y += eventfd.o +obj-y += io_uring_enter.o +obj-y += io_uring_register.o +obj-y += io_uring_setup.o obj-y += ioctl.o obj-y += mount.o obj-y += reboot.o diff --git a/lib/libc/sys/eventfd.c b/lib/libc/sys/eventfd.c new file mode 100644 index 00000000..b0fb5d86 --- /dev/null +++ b/lib/libc/sys/eventfd.c @@ -0,0 +1,6 @@ +#include + +int eventfd(unsigned int initval, int flags) +{ + return syscall(eventfd2, initval, flags); +} diff --git a/lib/libc/sys/eventfd_read.c b/lib/libc/sys/eventfd_read.c new file mode 100644 index 00000000..78d73623 --- /dev/null +++ b/lib/libc/sys/eventfd_read.c @@ -0,0 +1,8 @@ +#include +#include +#include + +int eventfd_read(int fildes, eventfd_t *value) +{ + return (sizeof(*value) == read(fildes, value, sizeof(*value))) ? 0 : -1; +} diff --git a/lib/libc/sys/eventfd_write.c b/lib/libc/sys/eventfd_write.c new file mode 100644 index 00000000..9c89b388 --- /dev/null +++ b/lib/libc/sys/eventfd_write.c @@ -0,0 +1,8 @@ +#include +#include +#include + +int eventfd_write(int fildes, eventfd_t value) +{ + return (sizeof(value) == write(fildes, &value, sizeof(value))) ? 0 : -1; +} diff --git a/lib/libc/sys/io_uring_enter.c b/lib/libc/sys/io_uring_enter.c new file mode 100644 index 00000000..5fe0773d --- /dev/null +++ b/lib/libc/sys/io_uring_enter.c @@ -0,0 +1,10 @@ +#include +#include + +int io_uring_enter(unsigned int fd, unsigned int to_submit, + unsigned int min_complete, unsigned int flags, sigset_t *sig, + size_t sz) +{ + return syscall(io_uring_enter, fd, to_submit, min_complete, flags, sig, + sz); +} diff --git a/lib/libc/sys/io_uring_register.c b/lib/libc/sys/io_uring_register.c new file mode 100644 index 00000000..bee2355f --- /dev/null +++ b/lib/libc/sys/io_uring_register.c @@ -0,0 +1,7 @@ +#include + +int io_uring_register(unsigned int fd, unsigned int opcode, void *arg, + unsigned int nr_args) +{ + return syscall(io_uring_register, fd, opcode, arg, nr_args); +} diff --git a/lib/libc/sys/io_uring_setup.c b/lib/libc/sys/io_uring_setup.c new file mode 100644 index 00000000..0a368a81 --- /dev/null +++ b/lib/libc/sys/io_uring_setup.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +struct io_uring __io_uring; + +int io_uring_setup(unsigned int entries, struct io_uring_params *params) +{ + return syscall(io_uring_setup, entries, params); +} + +int __io_uring_setup(void) +{ + struct io_uring_params p; + memset(&p, 0, sizeof(p)); + + __io_uring.fd = io_uring_setup(IO_URING_ENTRIES, &p); + + if (__io_uring.fd < 0) + return -1; + + __io_uring.sq.ring_size = + p.sq_off.array + p.sq_entries * sizeof(unsigned int); + __io_uring.sq.ring = mmap(NULL, __io_uring.sq.ring_size, + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, __io_uring.fd, + IORING_OFF_SQ_RING); + + if (__io_uring.sq.ring == MAP_FAILED) + return -1; + + __io_uring.sq.head = __io_uring.sq.ring + p.sq_off.head; + __io_uring.sq.tail = __io_uring.sq.ring + p.sq_off.tail; + __io_uring.sq.ring_mask = __io_uring.sq.ring + p.sq_off.ring_mask; + __io_uring.sq.ring_entries = __io_uring.sq.ring + p.sq_off.ring_entries; + __io_uring.sq.flags = __io_uring.sq.ring + p.sq_off.flags; + __io_uring.sq.dropped = __io_uring.sq.ring + p.sq_off.dropped; + __io_uring.sq.array = __io_uring.sq.ring + p.sq_off.array; + __io_uring.sq.sqes = + mmap(NULL, p.sq_entries * sizeof(struct io_uring_sqe), + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, + __io_uring.fd, IORING_OFF_SQES); + + if (__io_uring.sq.sqes == MAP_FAILED) { + munmap(__io_uring.sq.ring, __io_uring.sq.ring_size); + return -1; + } + + __io_uring.cq.ring_size = + p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe); + __io_uring.cq.ring = mmap(NULL, __io_uring.cq.ring_size, + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, __io_uring.fd, + IORING_OFF_CQ_RING); + + if (__io_uring.cq.ring == MAP_FAILED) { + munmap(__io_uring.sq.ring, __io_uring.sq.ring_size); + munmap(__io_uring.sq.sqes, + p.sq_entries * sizeof(struct io_uring_sqe)); + return -1; + } + + __io_uring.eventfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); + if (__io_uring.eventfd < 0) + return -1; + + io_uring_register(__io_uring.fd, IORING_REGISTER_EVENTFD, + &__io_uring.eventfd, 1); + + __io_uring.cq.head = __io_uring.cq.ring + p.cq_off.head; + __io_uring.cq.tail = __io_uring.cq.ring + p.cq_off.tail; + __io_uring.cq.ring_mask = __io_uring.cq.ring + p.cq_off.ring_mask; + __io_uring.cq.ring_entries = __io_uring.cq.ring + p.cq_off.ring_entries; + __io_uring.cq.overflow = __io_uring.cq.ring + p.cq_off.overflow; + __io_uring.cq.cqes = __io_uring.cq.ring + p.cq_off.cqes; + __io_uring.cq.flags = __io_uring.cq.ring + p.cq_off.flags; + + return 0; +} -- cgit v1.2.3