summaryrefslogtreecommitdiff
path: root/service/lib
diff options
context:
space:
mode:
authorAda Christine <adachristine18@gmail.com>2025-02-02 15:20:52 +0000
committerAda Christine <adachristine18@gmail.com>2025-02-02 15:20:52 +0000
commit9bc0738101979b626233ee226ece3cc8cae053ec (patch)
tree33f608f59a3cfc062db0363c208f3b0ea05c62e3 /service/lib
parent3d806c9e2fcb6226915982b6514919a3b9dac4dc (diff)
full flow implemented
Diffstat (limited to 'service/lib')
-rw-r--r--service/lib/posix.c113
1 files changed, 102 insertions, 11 deletions
diff --git a/service/lib/posix.c b/service/lib/posix.c
index 9a3e70f..abc73bf 100644
--- a/service/lib/posix.c
+++ b/service/lib/posix.c
@@ -1,19 +1,110 @@
#include <unistd.h>
+#include <sys/mman.h>
+#include <kjarna/syscall.h>
+#include <stdnoreturn.h>
-ssize_t write(int fd, const void *buffer, size_t length)
+__attribute__((always_inline))
+static inline int64_t syscall1(int syscall_index, uint64_t p1)
{
- ssize_t result;
+ int64_t result;
+ __asm__ (
+ "movq %1, %%rax\n"
+ "movq %2, %%rdi\n"
+ "syscall\n"
+ : "=a"(result)
+ : "g"(syscall_index), "g"(p1)
+ : "rdi");
+ return result;
+}
- __asm__ (
- "movq %1, %%rdi\n"
- "movq %2, %%rsi\n"
- "movq %3, %%rdx\n"
- "movq %0, %%rax\n"
- "syscall"
- : "=a"(result)
- : "g"(fd), "g"(buffer), "g"(length)
- : "rdi", "rsi","rdx");
+__attribute__((always_inline))
+static inline int64_t syscall2(int syscall_index, uint64_t p1, uint64_t p2)
+{
+ int64_t result;
+ __asm__ (
+ "movq %1, %%rax\n"
+ "movq %2, %%rdi\n"
+ "movq %3, %%rsi\n"
+ "syscall\n"
+ : "=a"(result)
+ : "g"(syscall_index), "g"(p1), "g"(p2)
+ : "rdi", "rsi");
+ return result;
+}
+
+__attribute__((always_inline))
+static inline int64_t syscall3(int syscall_index, uint64_t p1, uint64_t p2, uint64_t p3)
+{
+ int64_t result;
+ __asm__ (
+ "movq %1, %%rax\n"
+ "movq %2, %%rdi\n"
+ "movq %3, %%rsi\n"
+ "movq %4, %%rdx\n"
+ "syscall\n"
+ : "=a"(result)
+ : "g"(syscall_index), "g"(p1), "g"(p2), "g"(p3)
+ : "rdi", "rsi","rdx");
+ return result;
+}
+__attribute__((always_inline))
+static inline int64_t syscall6(int syscall_index, uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, uint64_t p5, uint64_t p6)
+{
+ int64_t result;
+ __asm__ (
+ "movq %1, %%rax\n"
+ "movq %2, %%rdi\n"
+ "movq %3, %%rsi\n"
+ "movq %4, %%rdx\n"
+ "movq %5, %%r10\n"
+ "movq %6, %%r8\n"
+ "movq %7, %%r9\n"
+ "syscall\n"
+ : "=a"(result)
+ : "g"(syscall_index), "g"(p1), "g"(p2), "g"(p3), "g"(p4), "g"(p5), "g"(p6)
+ : "rdi", "rsi","rdx", "r10", "r8", "r9");
return result;
}
+int open(const char *path, int flags, int mode)
+{
+ return (int)syscall3(SYS_OPEN, (uintptr_t)path, flags, mode);
+}
+
+int close(int fd)
+{
+ return (int)syscall1(SYS_CLOSE, fd);
+}
+
+off_t lseek(int fd, off_t offset, int whence)
+{
+ return syscall3(SYS_LSEEK, fd, offset, whence);
+}
+
+ssize_t read(int fd, void *buffer, size_t length)
+{
+ return syscall3(SYS_READ, fd, (uintptr_t)buffer, length);
+}
+
+ssize_t write(int fd, const void *buffer, size_t length)
+{
+ return syscall3(SYS_WRITE, fd, (uintptr_t)buffer, length);
+}
+
+void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
+{
+ return (void *)syscall6(SYS_MMAP, (uintptr_t)addr, length, prot, flags, fd, offset);
+}
+
+int munmap(void *addr, size_t length)
+{
+ return syscall2(SYS_MUNMAP, (uintptr_t)addr, length);
+}
+
+noreturn void _exit(int status)
+{
+ syscall1(SYS_EXIT, status);
+ while(true);
+}
+