Browse Source

Added a few feaures to the extras

master
Ludovic 'Archivist' Lagouardette 2 years ago
parent
commit
9a788731ec
3 changed files with 64 additions and 1 deletions
  1. +1
    -1
      extra_tests/001_test/Makefile
  2. +2
    -0
      extra_tests/001_test/main.cpp
  3. +61
    -0
      extra_tests/001_test/syscall.hpp

+ 1
- 1
extra_tests/001_test/Makefile View File

@ -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)

+ 2
- 0
extra_tests/001_test/main.cpp View File

@ -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);
}
}

+ 61
- 0
extra_tests/001_test/syscall.hpp View File

@ -51,8 +51,61 @@ struct syscall {
}
};
template<int64_t syscall_id>
struct syscall<syscall_id, 4> {
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<int64_t syscall_id>
struct syscall<syscall_id, 5> {
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<int64_t syscall_id>
struct syscall<syscall_id, 6> {
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<char> buffer) {
return _read((int64_t)fd, (int64_t)buffer.begin().data, (int64_t)buffer.size());
}

Loading…
Cancel
Save