|
| 1 | +// Minimal syscall interface required by the stub. |
| 2 | +#pragma once |
| 3 | +#include <linux/unistd.h> |
| 4 | +#include <stddef.h> |
| 5 | +#include <stdint.h> |
| 6 | + |
| 7 | + |
| 8 | +#define PROT_READ 0x1 |
| 9 | +#define PROT_WRITE 0x2 |
| 10 | +#define PROT_EXEC 0x4 |
| 11 | +#define MAP_PRIVATE 0x02 |
| 12 | +#define MAP_ANONYMOUS 0x20 |
| 13 | +#define MAP_FIXED 0x10 |
| 14 | +#define MAP_FIXED_NOREPLACE 0x100000 |
| 15 | +#define MAP_FAILED (void*)-1 |
| 16 | + |
| 17 | +#define RLIMIT_STACK 3 |
| 18 | + |
| 19 | +#define O_RDONLY 0 |
| 20 | +#define SEEK_SET 0 |
| 21 | +#define SEEK_CUR 1 |
| 22 | +#define SEEK_END 2 |
| 23 | + |
| 24 | +#define STDOUT_FILENO 1 |
| 25 | +#define STDERR_FILENO 2 |
| 26 | + |
| 27 | +typedef long off_t; |
| 28 | +typedef unsigned long __rlim_t; |
| 29 | +typedef struct { |
| 30 | + __rlim_t soft; |
| 31 | + __rlim_t hard; |
| 32 | +} rlimit_t; |
| 33 | + |
| 34 | +typedef struct { |
| 35 | + uint8_t buf[256]; |
| 36 | +} stat_t; |
| 37 | + |
| 38 | +__attribute__((naked)) |
| 39 | +static long write (int fd, const void *buf, size_t count) { |
| 40 | + register long x8 asm("x8") = __NR_write; |
| 41 | + register long x0 asm("x0") = (long)fd; |
| 42 | + register long x1 asm("x1") = (long)buf; |
| 43 | + register long x2 asm("x2") = (long)count; |
| 44 | + asm volatile( |
| 45 | + "svc 0\n\t" |
| 46 | + "ret\n\t" |
| 47 | + : |
| 48 | + : "r"(x8), "0"(x0), "r"(x1), "r"(x2) |
| 49 | + : "rcx", "r11", "memory" |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +__attribute__((naked)) |
| 54 | +static int open (const char* path, int mode) { |
| 55 | + register long x8 asm("x8") = __NR_open; |
| 56 | + register long x0 asm("x0") = (long)path; |
| 57 | + register long x1 asm("x1") = (long)mode; |
| 58 | + asm volatile( |
| 59 | + "svc 0\n\t" |
| 60 | + "ret\n\t" |
| 61 | + : |
| 62 | + : "r"(x8), "0"(x0), "r"(x1) |
| 63 | + : "memory", "cc" |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +__attribute__((naked)) |
| 68 | +static int close(int fd) { |
| 69 | + register long x8 asm("x8") = __NR_close; |
| 70 | + register long x0 asm("x0") = (long)fd; |
| 71 | + asm volatile( |
| 72 | + "svc 0\n\t" |
| 73 | + "ret\n\t" |
| 74 | + : |
| 75 | + : "r"(x8), "0"(x0) |
| 76 | + : "memory", "cc" |
| 77 | + ); |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +__attribute__((naked)) |
| 82 | +static long lseek (int fd, off_t offset, int whence) { |
| 83 | + register long x8 asm("x8") = __NR_lseek; |
| 84 | + register long x0 asm("x0") = (long)fd; |
| 85 | + register long x1 asm("x1") = (long)offset; |
| 86 | + register long x2 asm("x2") = (long)whence; |
| 87 | + asm volatile( |
| 88 | + "svc 0\n\t" |
| 89 | + "ret\n\t" |
| 90 | + : |
| 91 | + : "r"(x8), "0"(x0), "r"(x1), "r"(x2) |
| 92 | + : "memory", "cc" |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +__attribute__((naked)) |
| 97 | +static int getrlimit (int resource, rlimit_t* rlim) { |
| 98 | + register long x8 asm("x8") = __NR_getrlimit; |
| 99 | + register long x0 asm("x0") = (long)resource; |
| 100 | + register long x1 asm("x1") = (long)rlim; |
| 101 | + asm volatile( |
| 102 | + "svc 0\n\t" |
| 103 | + "ret\n\t" |
| 104 | + : |
| 105 | + : "r"(x8), "0"(x0), "r"(x1) |
| 106 | + : "memory", "cc" |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +__attribute__((naked)) |
| 111 | +static int pread64 (int fd, void* buf, size_t count, off_t offset) { |
| 112 | + register long x8 asm("x8") = __NR_pread64; |
| 113 | + register long x0 asm("x0") = (long)fd; |
| 114 | + register long x1 asm("x1") = (long)buf; |
| 115 | + register long x2 asm("x2") = (long)count; |
| 116 | + register long x1 asm("x1") = (long)offset; |
| 117 | + asm volatile( |
| 118 | + "svc 0\n\t" |
| 119 | + "ret\n\t" |
| 120 | + : |
| 121 | + : "r"(x8) |
| 122 | + : "memory", "cc" |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +__attribute__((naked)) |
| 127 | +static void exit (int status) |
| 128 | +{ |
| 129 | + register long x8 asm("x8") = __NR_exit; |
| 130 | + register long x0 asm("x0") = (long)status; |
| 131 | + asm volatile( |
| 132 | + "svc 0\n\t" |
| 133 | + "ret\n\t" |
| 134 | + : |
| 135 | + : "r"(x8), "0"(x0) |
| 136 | + : "memory", "cc" |
| 137 | + ); |
| 138 | + __builtin_unreachable(); |
| 139 | +} |
| 140 | + |
| 141 | +__attribute__((naked)) |
| 142 | +static void* mmap( |
| 143 | + void* addr, |
| 144 | + uint64_t length, |
| 145 | + uint64_t prot, |
| 146 | + uint64_t flags, |
| 147 | + int64_t fd, |
| 148 | + uint64_t offset |
| 149 | +) { |
| 150 | + register long x8 asm("x8") = __NR_mmap; |
| 151 | + register long x0 asm("x0") = (long)addr; |
| 152 | + register long x1 asm("x1") = (long)length; |
| 153 | + register long x2 asm("x2") = (long)prot; |
| 154 | + register long x3 asm("x3") = (long)flags; |
| 155 | + register long x4 asm("x4") = (long)fd; |
| 156 | + register long x5 asm("x5") = (long)offset; |
| 157 | + asm volatile( |
| 158 | + "svc 0\n\t" |
| 159 | + "ret\n\t" |
| 160 | + : |
| 161 | + : "r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) |
| 162 | + : "memory", "cc" |
| 163 | + ); |
| 164 | +} |
| 165 | + |
| 166 | +__attribute__((naked)) |
| 167 | +static int munmap( |
| 168 | + void* addr, |
| 169 | + uint64_t len |
| 170 | +) { |
| 171 | + register long x8 asm("x8") = __NR_munmap; |
| 172 | + register long x0 asm("x0") = (long)addr; |
| 173 | + register long x1 asm("x1") = (long)len; |
| 174 | + asm volatile( |
| 175 | + "svc 0\n\t" |
| 176 | + "ret\n\t" |
| 177 | + : |
| 178 | + : "r"(x8), "0"(x0), "r"(x1) |
| 179 | + : "memory", "cc" |
| 180 | + ); |
| 181 | +} |
| 182 | + |
| 183 | +__attribute__((naked)) |
| 184 | +static char* getcwd(char* buf, size_t size) { |
| 185 | + register long x8 asm("x8") = __NR_getcwd; |
| 186 | + register long x0 asm("x0") = (long)buf; |
| 187 | + register long x1 asm("x1") = (long)size; |
| 188 | + asm volatile( |
| 189 | + "svc 0\n\t" |
| 190 | + "ret\n\t" |
| 191 | + : |
| 192 | + : "r"(x8), "0"(x0), "r"(x1) |
| 193 | + : "memory", "cc" |
| 194 | + ); |
| 195 | +} |
| 196 | + |
| 197 | +__attribute__((naked)) |
| 198 | +int stat (const char* pathname, stat_t* statbuf) { |
| 199 | + register long x8 asm("x8") = __NR_stat; |
| 200 | + register long x0 asm("x0") = (long)pathname; |
| 201 | + register long x1 asm("x1") = (long)statbuf; |
| 202 | + asm volatile( |
| 203 | + "svc 0\n\t" |
| 204 | + "ret\n\t" |
| 205 | + : |
| 206 | + : "r"(x8), "0"(x0), "r"(x1) |
| 207 | + : "memory", "cc" |
| 208 | + ); |
| 209 | +} |
0 commit comments