diff --git a/extra_tests/001_test/Makefile b/extra_tests/001_test/Makefile index 35ee71c..ac4f9ee 100644 --- a/extra_tests/001_test/Makefile +++ b/extra_tests/001_test/Makefile @@ -7,7 +7,7 @@ CXXFLAGS= --std=c++20 -target x86_64-none-linux-elf -Os -ffreestanding -nostdlib all: tests tests: bin/tests -# ./bin/tests + ./bin/tests bin/tests: main.cpp @mkdir -p $(@D) diff --git a/extra_tests/001_test/main.cpp b/extra_tests/001_test/main.cpp index 892b7d4..8c09aac 100644 --- a/extra_tests/001_test/main.cpp +++ b/extra_tests/001_test/main.cpp @@ -5,7 +5,9 @@ gp::buffer hello = "Hello world\n"; extern "C" { void _start() { + auto memory = _mmap(0, 1 << 12, 2, 2 ^ 0x20, -1, 0); write(1, hello); + _munmap(memory, 1 << 12); exit(0); } } \ No newline at end of file diff --git a/extra_tests/001_test/syscall.hpp b/extra_tests/001_test/syscall.hpp index 2620204..528848a 100644 --- a/extra_tests/001_test/syscall.hpp +++ b/extra_tests/001_test/syscall.hpp @@ -51,8 +51,61 @@ struct syscall { } }; +template +struct syscall { + int64_t operator()(int64_t p1,int64_t p2,int64_t p3,int64_t p4) const { + int64_t ret; + register long r10 asm("r10") = p4; + asm volatile + ( + "syscall" + : "=a" (ret) + : "0"(syscall_id), "D"(p1), "S"(p2), "d"(p3), "r"(r10) + : "rcx", "r11", "memory" + ); + return ret; + } +}; + +template +struct syscall { + int64_t operator()(int64_t p1,int64_t p2,int64_t p3,int64_t p4,int64_t p5) const { + int64_t ret; + register long r10 asm("r10") = p4; + register long r8 asm("r8") = p5; + asm volatile + ( + "syscall" + : "=a" (ret) + : "0"(syscall_id), "D"(p1), "S"(p2), "d"(p3), "r"(r10), "r"(r8) + : "rcx", "r11", "memory" + ); + return ret; + } +}; + +template +struct syscall { + int64_t operator()(int64_t p1,int64_t p2,int64_t p3,int64_t p4,int64_t p5,int64_t p6) const { + int64_t ret; + register long r10 asm("r10") = p4; + register long r8 asm("r8") = p5; + register long r9 asm("r9") = p6; + asm volatile + ( + "syscall" + : "=a" (ret) + : "0"(syscall_id), "D"(p1), "S"(p2), "d"(p3), "r"(r10), "r"(r8), "r"(r9) + : "rcx", "r11", "memory" + ); + return ret; + } +}; + constexpr auto _read = syscall<0, 3>{}; constexpr auto _write = syscall<1, 3>{}; +constexpr auto _mmap = syscall<9, 6>{}; +constexpr auto _munmap = syscall<11, 2>{}; constexpr auto _exit = syscall<60, 1>{}; inline int read(int fd, char* buffer, size_t sz) { @@ -63,6 +116,14 @@ inline int write(int fd, char* buffer, size_t sz) { return _write((int64_t)fd, (int64_t)buffer, (int64_t)sz); } +inline void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { + return (void*)_mmap((int64_t)addr, (int64_t)length, (int64_t)prot, (int64_t)flags, (int64_t)fd, (int64_t)offset); +} + +inline int munmap(void *addr, size_t length) { + return _munmap((int64_t)addr, (int64_t)length); +} + inline int read(int fd, gp::buffer buffer) { return _read((int64_t)fd, (int64_t)buffer.begin().data, (int64_t)buffer.size()); }