platform_linux_amd64.c (1895B)
1 /* See LICENSE for license details. */ 2 typedef unsigned char u8; 3 typedef signed long i64; 4 typedef unsigned long u64; 5 typedef signed int i32; 6 typedef unsigned int u32; 7 typedef unsigned int b32; 8 typedef unsigned short u16; 9 typedef signed long size; 10 typedef unsigned long usize; 11 typedef signed long iptr; 12 13 #ifndef asm 14 #ifdef __asm 15 #define asm __asm 16 #else 17 #define asm __asm__ 18 #endif 19 #endif 20 21 #define SYS_read 0 22 #define SYS_write 1 23 #define SYS_open 2 24 #define SYS_close 3 25 #define SYS_stat 4 26 #define SYS_mmap 9 27 #define SYS_exit 60 28 #define SYS_getdents64 217 29 30 #define PAGESIZE 4096 31 32 #define STAT_BUF_SIZE 144 33 #define STAT_SIZE_OFF 48 34 35 #define DIRENT_RECLEN_OFF 16 36 #define DIRENT_TYPE_OFF 18 37 #define DIRENT_NAME_OFF 19 38 39 #include "platform_linux.c" 40 41 static i64 42 syscall1(i64 n, i64 a1) 43 { 44 i64 result; 45 asm volatile ("syscall" 46 : "=a"(result) 47 : "a"(n), "D"(a1) 48 : "rcx", "r11", "memory" 49 ); 50 return result; 51 } 52 53 static i64 54 syscall2(i64 n, i64 a1, i64 a2) 55 { 56 i64 result; 57 asm volatile ("syscall" 58 : "=a"(result) 59 : "a"(n), "D"(a1), "S"(a2) 60 : "rcx", "r11", "memory" 61 ); 62 return result; 63 } 64 65 static i64 66 syscall3(i64 n, i64 a1, i64 a2, i64 a3) 67 { 68 i64 result; 69 asm volatile ("syscall" 70 : "=a"(result) 71 : "a"(n), "D"(a1), "S"(a2), "d"(a3) 72 : "rcx", "r11", "memory" 73 ); 74 return result; 75 } 76 77 static i64 78 syscall6(i64 n, i64 a1, i64 a2, i64 a3, i64 a4, i64 a5, i64 a6) 79 { 80 i64 result; 81 register i64 r10 asm("r10") = a4; 82 register i64 r8 asm("r8") = a5; 83 register i64 r9 asm("r9") = a6; 84 asm volatile ("syscall" 85 : "=a"(result) 86 : "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8), "r"(r9) 87 : "rcx", "r11", "memory" 88 ); 89 return result; 90 } 91 92 asm ( 93 ".intel_syntax noprefix\n" 94 ".global _start\n" 95 "_start:\n" 96 " mov edi, DWORD PTR [rsp]\n" 97 " lea rsi, [rsp+8]\n" 98 " lea rdx, [rsi+rdi*8+8]\n" 99 " call linux_main\n" 100 " ud2\n" 101 ".att_syntax\n" 102 );