1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | INTERNAL void * x64_syscall_mmap(void *base_addr, u64 size, u32 memory_protection, u32 mapping_visibility, s32 fd, u64 fd_offset) { s32 result = 0; __asm__ __volatile__("mov r10, %5\n" "mov r8, %6\n" "mov r9, %7\n" "syscall" : "=a" (result) : "a" (9), "D" ((u64)base_addr), "S" (size), "d" ((u64)memory_protection), "r" ((u64)mapping_visibility), "r" ((u64)fd), "r" (fd_offset) : "r10", "r8", "r9", "r11", "rcx", "memory"); void *sys_result = (void *)((u64)result); if ((u64)result >= (u64)(-MAX_ERRNO)) { breakpoint(); sys_result = NULL; } return sys_result; } typedef struct { void* base; u64 size; u64 used; } X64MemArena; GLOBAL X64MemArena platform_mem_arena = {0}; INTERNAL s32 x64_mem_arena_init(X64MemArena *mem_arena, u64 size) { mem_arena->base = x64_syscall_mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (mem_arena->base == NULL) { breakpoint(); } // I tried without this call, gives same result if (x64_syscall_mlock(mem_arena->base, mem_arena->size) < 0) { breakpoint(); } mem_arena->used = 0; mem_arena->size = size; return 0; } // ... // used here if (x64_mem_arena_init(&platform_mem_arena, 1024 * 1024 * 200) == -1) { breakpoint(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | long my_syscall(int nr, long arg0, long arg1, long arg2, long arg3, long arg4, long arg5) { register long r10 __asm__ ("r10") = arg3; register long r8 __asm__ ("r8") = arg4; register long r9 __asm__ ("r9") = arg5; long result; __asm__ __volatile__( "syscall" : "=r" (result) : "0"(nr), "D"(arg0), "S"(arg1), "d"(arg2), "r"(r10), "r"(r8), "r"(r9) : "rcx", "r11", "memory" ); return result; } |