aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/sysdeps/managarm/include
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mlibc/sysdeps/managarm/include')
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/access.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/auxv.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/blkcnt_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/blksize_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/clockid_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/dev_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/epoll.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/errno.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/fcntl.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/fsblkcnt_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/fsfilcnt_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/gid_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/in.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/ino_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/inotify.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/ioctls.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/limits.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/mode_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/mqueue.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/msg.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/nlink_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/packet.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/pid_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/poll.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/ptrace.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/reboot.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/resource.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/seek-whence.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/shm.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/signal.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/socket.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/socklen_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/stat.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/statfs.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/statvfs.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/suseconds_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/termios.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/time.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/uid_t.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/utsname.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/vm-flags.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/vt.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/wait.h1
l---------lib/mlibc/sysdeps/managarm/include/abi-bits/xattr.h1
-rw-r--r--lib/mlibc/sysdeps/managarm/include/mlibc/posix-pipe.hpp300
-rw-r--r--lib/mlibc/sysdeps/managarm/include/mlibc/thread-entry.hpp10
46 files changed, 354 insertions, 0 deletions
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/access.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/access.h
new file mode 120000
index 0000000..cb83931
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/access.h
@@ -0,0 +1 @@
+../../../../abis/linux/access.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/auxv.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/auxv.h
new file mode 120000
index 0000000..e760d62
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/auxv.h
@@ -0,0 +1 @@
+../../../../abis/managarm/auxv.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/blkcnt_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/blkcnt_t.h
new file mode 120000
index 0000000..0b0ec27
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/blkcnt_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/blkcnt_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/blksize_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/blksize_t.h
new file mode 120000
index 0000000..7dc8d7c
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/blksize_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/blksize_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/clockid_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/clockid_t.h
new file mode 120000
index 0000000..6a42da5
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/clockid_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/clockid_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/dev_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/dev_t.h
new file mode 120000
index 0000000..bca881e
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/dev_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/dev_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/epoll.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/epoll.h
new file mode 120000
index 0000000..eb4b76d
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/epoll.h
@@ -0,0 +1 @@
+../../../../abis/linux/epoll.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/errno.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/errno.h
new file mode 120000
index 0000000..6e507de
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/errno.h
@@ -0,0 +1 @@
+../../../../abis/linux/errno.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/fcntl.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/fcntl.h
new file mode 120000
index 0000000..463e2c9
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/fcntl.h
@@ -0,0 +1 @@
+../../../../abis/linux/fcntl.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/fsblkcnt_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/fsblkcnt_t.h
new file mode 120000
index 0000000..898dfb2
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/fsblkcnt_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/fsblkcnt_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/fsfilcnt_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/fsfilcnt_t.h
new file mode 120000
index 0000000..791755c
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/fsfilcnt_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/fsfilcnt_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/gid_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/gid_t.h
new file mode 120000
index 0000000..abce6d6
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/gid_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/gid_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/in.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/in.h
new file mode 120000
index 0000000..418d1d5
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/in.h
@@ -0,0 +1 @@
+../../../../abis/linux/in.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/ino_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/ino_t.h
new file mode 120000
index 0000000..4c20aca
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/ino_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/ino_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/inotify.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/inotify.h
new file mode 120000
index 0000000..b5cb282
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/inotify.h
@@ -0,0 +1 @@
+../../../../abis/linux/inotify.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/ioctls.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/ioctls.h
new file mode 120000
index 0000000..595106b
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/ioctls.h
@@ -0,0 +1 @@
+../../../../abis/linux/ioctls.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/limits.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/limits.h
new file mode 120000
index 0000000..6c88db2
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/limits.h
@@ -0,0 +1 @@
+../../../../abis/linux/limits.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/mode_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/mode_t.h
new file mode 120000
index 0000000..5d78fdf
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/mode_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/mode_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/mqueue.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/mqueue.h
new file mode 120000
index 0000000..fa87b07
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/mqueue.h
@@ -0,0 +1 @@
+../../../../abis/linux/mqueue.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/msg.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/msg.h
new file mode 120000
index 0000000..f402b49
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/msg.h
@@ -0,0 +1 @@
+../../../../abis/linux/msg.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/nlink_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/nlink_t.h
new file mode 120000
index 0000000..bb3b625
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/nlink_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/nlink_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/packet.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/packet.h
new file mode 120000
index 0000000..998ef1a
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/packet.h
@@ -0,0 +1 @@
+../../../../abis/linux/packet.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/pid_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/pid_t.h
new file mode 120000
index 0000000..baa90f6
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/pid_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/pid_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/poll.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/poll.h
new file mode 120000
index 0000000..8ea6a0a
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/poll.h
@@ -0,0 +1 @@
+../../../../abis/linux/poll.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/ptrace.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/ptrace.h
new file mode 120000
index 0000000..b2517b2
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/ptrace.h
@@ -0,0 +1 @@
+../../../../abis/linux/ptrace.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/reboot.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/reboot.h
new file mode 120000
index 0000000..77013a4
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/reboot.h
@@ -0,0 +1 @@
+../../../../abis/linux/reboot.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/resource.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/resource.h
new file mode 120000
index 0000000..88d7402
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/resource.h
@@ -0,0 +1 @@
+../../../../abis/linux/resource.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/seek-whence.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/seek-whence.h
new file mode 120000
index 0000000..df7bccf
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/seek-whence.h
@@ -0,0 +1 @@
+../../../../abis/linux/seek-whence.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/shm.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/shm.h
new file mode 120000
index 0000000..067d8c4
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/shm.h
@@ -0,0 +1 @@
+../../../../abis/linux/shm.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/signal.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/signal.h
new file mode 120000
index 0000000..4dcb0b7
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/signal.h
@@ -0,0 +1 @@
+../../../../abis/linux/signal.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/socket.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/socket.h
new file mode 120000
index 0000000..f1dc016
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/socket.h
@@ -0,0 +1 @@
+../../../../abis/linux/socket.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/socklen_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/socklen_t.h
new file mode 120000
index 0000000..41f3b11
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/socklen_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/socklen_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/stat.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/stat.h
new file mode 120000
index 0000000..1f63b41
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/stat.h
@@ -0,0 +1 @@
+../../../../abis/linux/stat.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/statfs.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/statfs.h
new file mode 120000
index 0000000..e3d202f
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/statfs.h
@@ -0,0 +1 @@
+../../../../abis/linux/statfs.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/statvfs.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/statvfs.h
new file mode 120000
index 0000000..1fc80c2
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/statvfs.h
@@ -0,0 +1 @@
+../../../../abis/linux/statvfs.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/suseconds_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/suseconds_t.h
new file mode 120000
index 0000000..9ed6597
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/suseconds_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/suseconds_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/termios.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/termios.h
new file mode 120000
index 0000000..ee8f0b0
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/termios.h
@@ -0,0 +1 @@
+../../../../abis/linux/termios.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/time.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/time.h
new file mode 120000
index 0000000..2a02625
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/time.h
@@ -0,0 +1 @@
+../../../../abis/linux/time.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/uid_t.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/uid_t.h
new file mode 120000
index 0000000..b306777
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/uid_t.h
@@ -0,0 +1 @@
+../../../../abis/linux/uid_t.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/utsname.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/utsname.h
new file mode 120000
index 0000000..b285754
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/utsname.h
@@ -0,0 +1 @@
+../../../../abis/linux/utsname.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/vm-flags.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/vm-flags.h
new file mode 120000
index 0000000..bbe258c
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/vm-flags.h
@@ -0,0 +1 @@
+../../../../abis/linux/vm-flags.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/vt.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/vt.h
new file mode 120000
index 0000000..5798a4a
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/vt.h
@@ -0,0 +1 @@
+../../../../abis/linux/vt.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/wait.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/wait.h
new file mode 120000
index 0000000..feb2840
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/wait.h
@@ -0,0 +1 @@
+../../../../abis/linux/wait.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/abi-bits/xattr.h b/lib/mlibc/sysdeps/managarm/include/abi-bits/xattr.h
new file mode 120000
index 0000000..66412d7
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/abi-bits/xattr.h
@@ -0,0 +1 @@
+../../../../abis/linux/xattr.h \ No newline at end of file
diff --git a/lib/mlibc/sysdeps/managarm/include/mlibc/posix-pipe.hpp b/lib/mlibc/sysdeps/managarm/include/mlibc/posix-pipe.hpp
new file mode 100644
index 0000000..4bcf1cb
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/mlibc/posix-pipe.hpp
@@ -0,0 +1,300 @@
+#ifndef MLIBC_POSIX_PIPE
+#define MLIBC_POSIX_PIPE
+
+#include <signal.h>
+#include <stdint.h>
+#include <string.h>
+#include <cstddef>
+
+#include <hel.h>
+#include <hel-syscalls.h>
+
+struct SignalGuard {
+ SignalGuard();
+
+ SignalGuard(const SignalGuard &) = delete;
+
+ ~SignalGuard();
+
+ SignalGuard &operator= (const SignalGuard &) = delete;
+};
+
+// We need an allocator for message structs in sysdeps functions; the "normal" mlibc
+// allocator cannot be used, as the sysdeps function might be called from a signal.
+MemoryAllocator &getSysdepsAllocator();
+
+struct Queue;
+
+struct ElementHandle {
+ friend void swap(ElementHandle &u, ElementHandle &v) {
+ using std::swap;
+ swap(u._queue, v._queue);
+ swap(u._n, v._n);
+ swap(u._data, v._data);
+ }
+
+ ElementHandle()
+ : _queue{nullptr}, _n{-1}, _data{nullptr} { }
+
+ ElementHandle(Queue *queue, int n, void *data)
+ : _queue{queue}, _n{n}, _data{data} { }
+
+ ElementHandle(const ElementHandle &other);
+
+ ElementHandle(ElementHandle &&other)
+ : ElementHandle{} {
+ swap(*this, other);
+ }
+
+ ~ElementHandle();
+
+ ElementHandle &operator= (ElementHandle other) {
+ swap(*this, other);
+ return *this;
+ }
+
+ void *data() {
+ return _data;
+ }
+
+ void advance(size_t size) {
+ _data = reinterpret_cast<char *>(_data) + size;
+ }
+
+private:
+ Queue *_queue;
+ int _n;
+ void *_data;
+};
+
+struct Queue {
+ Queue()
+ : _handle{kHelNullHandle} {
+ // We do not need to protect those allocations against signals as this constructor
+ // is only called during library initialization.
+ _chunks[0] = reinterpret_cast<HelChunk *>(getSysdepsAllocator().allocate(sizeof(HelChunk) + 4096));
+ _chunks[1] = reinterpret_cast<HelChunk *>(getSysdepsAllocator().allocate(sizeof(HelChunk) + 4096));
+
+ recreateQueue();
+ }
+
+ Queue(const Queue &) = delete;
+
+ Queue &operator= (const Queue &) = delete;
+
+ void recreateQueue() {
+ // Reset the internal queue state.
+ _retrieveIndex = 0;
+ _nextIndex = 0;
+ _lastProgress = 0;
+
+ // Setup the queue header.
+ HelQueueParameters params {
+ .flags = 0,
+ .ringShift = 1,
+ .numChunks = 2,
+ .chunkSize = 4096
+ };
+ HEL_CHECK(helCreateQueue(&params, &_handle));
+
+ auto chunksOffset = (sizeof(HelQueue) + (sizeof(int) << 1) + 63) & ~size_t(63);
+ auto reservedPerChunk = (sizeof(HelChunk) + params.chunkSize + 63) & ~size_t(63);
+ auto overallSize = chunksOffset + params.numChunks * reservedPerChunk;
+
+ void *mapping;
+ HEL_CHECK(helMapMemory(_handle, kHelNullHandle, nullptr,
+ 0, (overallSize + 0xFFF) & ~size_t(0xFFF),
+ kHelMapProtRead | kHelMapProtWrite, &mapping));
+
+ _queue = reinterpret_cast<HelQueue *>(mapping);
+ auto chunksPtr = reinterpret_cast<std::byte *>(mapping) + chunksOffset;
+ for(unsigned int i = 0; i < 2; ++i)
+ _chunks[i] = reinterpret_cast<HelChunk *>(chunksPtr + i * reservedPerChunk);
+
+ // Reset and enqueue the chunks.
+ _chunks[0]->progressFutex = 0;
+ _chunks[1]->progressFutex = 0;
+ _refCount[0] = 1;
+ _refCount[1] = 1;
+
+ _queue->indexQueue[0] = 0;
+ _queue->indexQueue[1] = 1;
+ _queue->headFutex = 0;
+ _nextIndex = 2;
+ _wakeHeadFutex();
+ }
+
+ HelHandle getQueue() {
+ return _handle;
+ }
+
+ void trim() { }
+
+ ElementHandle dequeueSingle() {
+ while(true) {
+ __ensure(_retrieveIndex != _nextIndex);
+
+ bool done;
+ _waitProgressFutex(&done);
+
+ auto n = _numberOf(_retrieveIndex);
+ __ensure(_refCount[n]);
+
+ if(done) {
+ retire(n);
+
+ _lastProgress = 0;
+ _retrieveIndex = ((_retrieveIndex + 1) & kHelHeadMask);
+ continue;
+ }
+
+ // Dequeue the next element.
+ auto ptr = (char *)_chunks[n] + sizeof(HelChunk) + _lastProgress;
+ auto element = reinterpret_cast<HelElement *>(ptr);
+ _lastProgress += sizeof(HelElement) + element->length;
+ _refCount[n]++;
+ return ElementHandle{this, n, ptr + sizeof(HelElement)};
+ }
+ }
+
+ void retire(int n) {
+ __ensure(_refCount[n]);
+ if(_refCount[n]-- > 1)
+ return;
+
+ // Reset and enqueue the chunk again.
+ _chunks[n]->progressFutex = 0;
+ _refCount[n] = 1;
+
+ _queue->indexQueue[_nextIndex & 1] = n;
+ _nextIndex = ((_nextIndex + 1) & kHelHeadMask);
+ _wakeHeadFutex();
+ }
+
+ void reference(int n) {
+ _refCount[n]++;
+ }
+
+private:
+ int _numberOf(int index) {
+ return _queue->indexQueue[index & 1];
+ }
+
+ HelChunk *_retrieveChunk() {
+ return _chunks[_numberOf(_retrieveIndex)];
+ }
+
+ void _wakeHeadFutex() {
+ auto futex = __atomic_exchange_n(&_queue->headFutex, _nextIndex, __ATOMIC_RELEASE);
+ if(futex & kHelHeadWaiters)
+ HEL_CHECK(helFutexWake(&_queue->headFutex));
+ }
+
+ void _waitProgressFutex(bool *done) {
+ while(true) {
+ auto futex = __atomic_load_n(&_retrieveChunk()->progressFutex, __ATOMIC_ACQUIRE);
+ __ensure(!(futex & ~(kHelProgressMask | kHelProgressWaiters | kHelProgressDone)));
+ do {
+ if(_lastProgress != (futex & kHelProgressMask)) {
+ *done = false;
+ return;
+ }else if(futex & kHelProgressDone) {
+ *done = true;
+ return;
+ }
+
+ if(futex & kHelProgressWaiters)
+ break; // Waiters bit is already set (in a previous iteration).
+ } while(!__atomic_compare_exchange_n(&_retrieveChunk()->progressFutex, &futex,
+ _lastProgress | kHelProgressWaiters,
+ false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE));
+
+ HEL_CHECK(helFutexWait(&_retrieveChunk()->progressFutex,
+ _lastProgress | kHelProgressWaiters, -1));
+ }
+ }
+
+private:
+ HelHandle _handle;
+ HelQueue *_queue;
+ HelChunk *_chunks[2];
+
+ // Index of the chunk that we are currently retrieving/inserting next.
+ int _retrieveIndex;
+ int _nextIndex;
+
+ // Progress into the current chunk.
+ int _lastProgress;
+
+ // Number of ElementHandle objects alive.
+ int _refCount[2];
+};
+
+inline ElementHandle::~ElementHandle() {
+ if(_queue)
+ _queue->retire(_n);
+}
+
+inline ElementHandle::ElementHandle(const ElementHandle &other) {
+ _queue = other._queue;
+ _n = other._n;
+ _data = other._data;
+
+ _queue->reference(_n);
+}
+
+
+inline HelSimpleResult *parseSimple(ElementHandle &element) {
+ auto result = reinterpret_cast<HelSimpleResult *>(element.data());
+ element.advance(sizeof(HelSimpleResult));
+ return result;
+}
+
+inline HelInlineResult *parseInline(ElementHandle &element) {
+ auto result = reinterpret_cast<HelInlineResult *>(element.data());
+ element.advance(sizeof(HelInlineResult) + ((result->length + 7) & ~size_t(7)));
+ return result;
+}
+
+inline HelLengthResult *parseLength(ElementHandle &element) {
+ auto result = reinterpret_cast<HelLengthResult *>(element.data());
+ element.advance(sizeof(HelLengthResult));
+ return result;
+}
+
+inline HelHandleResult *parseHandle(ElementHandle &element) {
+ auto result = reinterpret_cast<HelHandleResult *>(element.data());
+ element.advance(sizeof(HelHandleResult));
+ return result;
+}
+
+HelHandle getPosixLane();
+HelHandle *cacheFileTable();
+HelHandle getHandleForFd(int fd);
+void clearCachedInfos();
+
+extern thread_local Queue globalQueue;
+
+// This include is here because it needs ElementHandle to be declared
+#include <helix/ipc-structs.hpp>
+
+template <typename ...Args>
+auto exchangeMsgsSync(HelHandle descriptor, Args &&...args) {
+ auto results = helix_ng::createResultsTuple(args...);
+ auto actions = helix_ng::chainActionArrays(args...);
+
+ HEL_CHECK(helSubmitAsync(descriptor, actions.data(),
+ actions.size(), globalQueue.getQueue(), 0, 0));
+
+ auto element = globalQueue.dequeueSingle();
+ void *ptr = element.data();
+
+ [&]<size_t ...p>(std::index_sequence<p...>) {
+ (results.template get<p>().parse(ptr, element), ...);
+ } (std::make_index_sequence<std::tuple_size_v<decltype(results)>>{});
+
+ return results;
+}
+
+
+#endif // MLIBC_POSIX_PIPE
diff --git a/lib/mlibc/sysdeps/managarm/include/mlibc/thread-entry.hpp b/lib/mlibc/sysdeps/managarm/include/mlibc/thread-entry.hpp
new file mode 100644
index 0000000..2dd88a6
--- /dev/null
+++ b/lib/mlibc/sysdeps/managarm/include/mlibc/thread-entry.hpp
@@ -0,0 +1,10 @@
+#pragma once
+
+#include <mlibc/tcb.hpp>
+
+extern "C" void __mlibc_start_thread(void);
+extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb);
+
+namespace mlibc {
+ void *prepare_stack(void *entry, void *user_arg, void *tcb);
+}