PatchworkOS
Loading...
Searching...
No Matches
syscalls.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <errno.h>
6#include <stdarg.h>
7#include <stdint.h>
8#include <sys/io.h>
9#include <sys/proc.h>
10#include <time.h>
11
12#define _SYSCALL0(retType, num) \
13 ({ \
14 register retType ret asm("rax"); \
15 asm volatile("syscall\n" : "=a"(ret) : "a"(num) : "rcx", "r11", "memory"); \
16 ret; \
17 })
18
19#define _SYSCALL1(retType, num, type1, arg1) \
20 ({ \
21 register retType ret asm("rax"); \
22 register type1 _a1 asm("rdi") = (arg1); \
23 asm volatile("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1) : "rcx", "r11", "memory"); \
24 ret; \
25 })
26
27#define _SYSCALL2(retType, num, type1, arg1, type2, arg2) \
28 ({ \
29 register retType ret asm("rax"); \
30 register type1 _a1 asm("rdi") = (arg1); \
31 register type2 _a2 asm("rsi") = (arg2); \
32 asm volatile("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1), "r"(_a2) : "rcx", "r11", "memory"); \
33 ret; \
34 })
35
36#define _SYSCALL3(retType, num, type1, arg1, type2, arg2, type3, arg3) \
37 ({ \
38 register retType ret asm("rax"); \
39 register type1 _a1 asm("rdi") = (arg1); \
40 register type2 _a2 asm("rsi") = (arg2); \
41 register type3 _a3 asm("rdx") = (arg3); \
42 asm volatile("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1), "r"(_a2), "r"(_a3) : "rcx", "r11", "memory"); \
43 ret; \
44 })
45
46#define _SYSCALL4(retType, num, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
47 ({ \
48 register retType ret asm("rax"); \
49 register type1 _a1 asm("rdi") = (arg1); \
50 register type2 _a2 asm("rsi") = (arg2); \
51 register type3 _a3 asm("rdx") = (arg3); \
52 register type4 _a4 asm("r10") = (arg4); \
53 asm volatile("syscall\n" \
54 : "=a"(ret) \
55 : "a"(num), "r"(_a1), "r"(_a2), "r"(_a3), "r"(_a4) \
56 : "rcx", "r11", "memory"); \
57 ret; \
58 })
59
60#define _SYSCALL5(retType, num, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \
61 ({ \
62 register retType ret asm("rax"); \
63 register type1 _a1 asm("rdi") = (arg1); \
64 register type2 _a2 asm("rsi") = (arg2); \
65 register type3 _a3 asm("rdx") = (arg3); \
66 register type4 _a4 asm("r10") = (arg4); \
67 register type5 _a5 asm("r8") = (arg5); \
68 asm volatile("syscall\n" \
69 : "=a"(ret) \
70 : "a"(num), "r"(_a1), "r"(_a2), "r"(_a3), "r"(_a4), "r"(_a5) \
71 : "rcx", "r11", "memory"); \
72 ret; \
73 })
74
75#define _SYSCALL6(retType, num, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5, type6, arg6) \
76 ({ \
77 register retType ret asm("rax"); \
78 register type1 _a1 asm("rdi") = (arg1); \
79 register type2 _a2 asm("rsi") = (arg2); \
80 register type3 _a3 asm("rdx") = (arg3); \
81 register type4 _a4 asm("r10") = (arg4); \
82 register type5 _a5 asm("r8") = (arg5); \
83 register type6 _a6 asm("r9") = (arg6); \
84 asm volatile("syscall\n" \
85 : "=a"(ret) \
86 : "a"(num), "r"(_a1), "r"(_a2), "r"(_a3), "r"(_a4), "r"(_a5), "r"(_a6) \
87 : "rcx", "r11", "memory"); \
88 ret; \
89 })
90
91_NORETURN static inline void _syscall_process_exit(uint64_t status)
92{
94 asm volatile("ud2");
95 while (1)
96 ;
97}
98
99_NORETURN static inline void _syscall_thread_exit(void)
100{
102 asm volatile("ud2");
103 while (1)
104 ;
105}
106
107static inline pid_t _syscall_spawn(const char** argv, const spawn_fd_t* fds, const char* cwd, spawn_attr_t* attr)
108{
109 return _SYSCALL4(pid_t, SYS_SPAWN, const char**, argv, const spawn_fd_t*, fds, const char*, cwd, spawn_attr_t*,
110 attr);
111}
112
113static inline uint64_t _syscall_nanosleep(clock_t nanoseconds)
114{
115 return _SYSCALL1(uint64_t, SYS_NANOSLEEP, clock_t, nanoseconds);
116}
117
118static inline errno_t _syscall_errno(void)
119{
120 return _SYSCALL0(errno_t, SYS_ERRNO);
121}
122
123static inline pid_t _syscall_getpid(void)
124{
125 return _SYSCALL0(pid_t, SYS_GETPID);
126}
127
128static inline tid_t _syscall_gettid(void)
129{
130 return _SYSCALL0(tid_t, SYS_GETTID);
131}
132
133static inline clock_t _syscall_uptime(void)
134{
136}
137
138static inline time_t _syscall_unix_epoch(void)
139{
141}
142
143static inline fd_t _syscall_open(const char* path)
144{
145 return _SYSCALL1(fd_t, SYS_OPEN, const char*, path);
146}
147
148static inline uint64_t _syscall_open2(const char* path, fd_t fds[2])
149{
150 return _SYSCALL2(uint64_t, SYS_OPEN2, const char*, path, fd_t*, fds);
151}
152
153static inline uint64_t _syscall_close(fd_t fd)
154{
155 return _SYSCALL1(uint64_t, SYS_CLOSE, fd_t, fd);
156}
157
159{
160 return _SYSCALL3(uint64_t, SYS_READ, fd_t, fd, void*, buffer, uint64_t, count);
161}
162
163static inline uint64_t _syscall_write(fd_t fd, const void* buffer, uint64_t count)
164{
165 return _SYSCALL3(uint64_t, SYS_WRITE, fd_t, fd, const void*, buffer, uint64_t, count);
166}
167
168static inline uint64_t _syscall_seek(fd_t fd, int64_t offset, seek_origin_t origin)
169{
170 return _SYSCALL3(uint64_t, SYS_SEEK, fd_t, fd, int64_t, offset, seek_origin_t, origin);
171}
172
173static inline uint64_t _syscall_ioctl(fd_t fd, uint64_t request, void* argp, uint64_t size)
174{
175 return _SYSCALL4(uint64_t, SYS_IOCTL, fd_t, fd, uint64_t, request, void*, argp, uint64_t, size);
176}
177
178static inline uint64_t _syscall_chdir(const char* path)
179{
180 return _SYSCALL1(uint64_t, SYS_CHDIR, const char*, path);
181}
182
183static inline uint64_t _syscall_poll(pollfd_t* fds, uint64_t amount, clock_t timeout)
184{
185 return _SYSCALL3(uint64_t, SYS_POLL, pollfd_t*, fds, uint64_t, amount, clock_t, timeout);
186}
187
188static inline uint64_t _syscall_stat(const char* path, stat_t* info)
189{
190 return _SYSCALL2(uint64_t, SYS_STAT, const char*, path, stat_t*, info);
191}
192
193static inline void* _syscall_mmap(fd_t fd, void* address, uint64_t length, prot_t prot)
194{
195 return _SYSCALL4(void*, SYS_MMAP, fd_t, fd, void*, address, uint64_t, length, prot_t, prot);
196}
197
198static inline uint64_t _syscall_munmap(void* address, uint64_t length)
199{
200 return _SYSCALL2(uint64_t, SYS_MUNMAP, void*, address, uint64_t, length);
201}
202
203static inline uint64_t _syscall_mprotect(void* address, uint64_t length, prot_t prot)
204{
205 return _SYSCALL3(uint64_t, SYS_MPROTECT, void*, address, uint64_t, length, prot_t, prot);
206}
207
212
213static inline tid_t _syscall_thread_create(void* entry, void* arg)
214{
215 return _SYSCALL2(tid_t, SYS_THREAD_CREATE, void*, entry, void*, arg);
216}
217
218static inline void _syscall_yield(void)
219{
221}
222
223static inline fd_t _syscall_dup(fd_t oldFd)
224{
225 return _SYSCALL1(fd_t, SYS_DUP, fd_t, oldFd);
226}
227
228static inline fd_t _syscall_dup2(fd_t oldFd, fd_t newFd)
229{
230 return _SYSCALL2(fd_t, SYS_DUP2, fd_t, oldFd, fd_t, newFd);
231}
232
233static inline uint64_t _syscall_futex(atomic_uint64_t* addr, uint64_t val, futex_op_t op, clock_t timeout)
234{
235 return _SYSCALL4(uint64_t, SYS_FUTEX, atomic_uint64_t*, addr, uint64_t, val, futex_op_t, op, clock_t, timeout);
236}
237
238static inline uint64_t _syscall_remove(const char* path)
239{
240 return _SYSCALL1(uint64_t, SYS_REMOVE, const char*, path);
241}
242
243static inline uint64_t _syscall_link(const char* oldPath, const char* newPath)
244{
245 return _SYSCALL2(uint64_t, SYS_LINK, const char*, oldPath, const char*, newPath);
246}
247
248static inline uint64_t _syscall_share(key_t* key, fd_t fd, clock_t timeout)
249{
250 return _SYSCALL3(uint64_t, SYS_SHARE, key_t*, key, fd_t, fd, clock_t, timeout);
251}
252
253static inline fd_t _syscall_claim(key_t* key)
254{
255 return _SYSCALL1(fd_t, SYS_CLAIM, key_t*, key);
256}
257
258static inline uint64_t _syscall_bind(fd_t source, const char* mountpoint)
259{
260 return _SYSCALL2(uint64_t, SYS_BIND, fd_t, source, const char*, mountpoint);
261}
int errno_t
Definition errno_t.h:4
#define SYS_SHARE
Definition syscalls.h:52
#define SYS_REMOVE
Definition syscalls.h:50
#define SYS_MPROTECT
Definition syscalls.h:43
#define SYS_GETPID
Definition syscalls.h:27
#define SYS_GETDENTS
Definition syscalls.h:44
#define SYS_SPAWN
Definition syscalls.h:24
#define SYS_MMAP
Definition syscalls.h:41
#define SYS_PROCESS_EXIT
Definition syscalls.h:22
#define SYS_CHDIR
Definition syscalls.h:38
#define SYS_NANOSLEEP
Definition syscalls.h:25
#define SYS_DUP
Definition syscalls.h:47
#define SYS_ERRNO
Definition syscalls.h:26
#define SYS_OPEN2
Definition syscalls.h:32
#define SYS_WRITE
Definition syscalls.h:35
#define SYS_UPTIME
Definition syscalls.h:29
#define SYS_UNIX_EPOCH
Definition syscalls.h:30
#define SYS_READ
Definition syscalls.h:34
#define SYS_OPEN
Definition syscalls.h:31
#define SYS_YIELD
Definition syscalls.h:46
#define SYS_THREAD_EXIT
Definition syscalls.h:23
#define SYS_STAT
Definition syscalls.h:40
#define SYS_SEEK
Definition syscalls.h:36
#define SYS_POLL
Definition syscalls.h:39
#define SYS_MUNMAP
Definition syscalls.h:42
#define SYS_CLAIM
Definition syscalls.h:53
#define SYS_LINK
Definition syscalls.h:51
#define SYS_FUTEX
Definition syscalls.h:49
#define SYS_THREAD_CREATE
Definition syscalls.h:45
#define SYS_CLOSE
Definition syscalls.h:33
#define SYS_BIND
Definition syscalls.h:54
#define SYS_DUP2
Definition syscalls.h:48
#define SYS_GETTID
Definition syscalls.h:28
#define SYS_IOCTL
Definition syscalls.h:37
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition io.h:262
futex_op_t
Futex operation enum.
Definition proc.h:226
prot_t
Memory protection flags.
Definition proc.h:170
__UINT64_TYPE__ tid_t
Thread Identifier.
Definition tid_t.h:12
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
__UINT64_TYPE__ pid_t
Process Identifier.
Definition pid_t.h:11
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static uintptr_t address
Definition hpet.c:12
static fb_info_t info
Definition gop.c:41
#define _NORETURN
Definition config.h:28
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static atomic_long count
Definition main.c:9
static fd_t _syscall_dup2(fd_t oldFd, fd_t newFd)
Definition syscalls.h:228
static uint64_t _syscall_share(key_t *key, fd_t fd, clock_t timeout)
Definition syscalls.h:248
#define _SYSCALL4(retType, num, type1, arg1, type2, arg2, type3, arg3, type4, arg4)
Definition syscalls.h:46
static pid_t _syscall_getpid(void)
Definition syscalls.h:123
#define _SYSCALL3(retType, num, type1, arg1, type2, arg2, type3, arg3)
Definition syscalls.h:36
#define _SYSCALL1(retType, num, type1, arg1)
Definition syscalls.h:19
static uint64_t _syscall_mprotect(void *address, uint64_t length, prot_t prot)
Definition syscalls.h:203
static uint64_t _syscall_remove(const char *path)
Definition syscalls.h:238
#define _SYSCALL0(retType, num)
Definition syscalls.h:12
static uint64_t _syscall_getdents(fd_t fd, dirent_t *buffer, uint64_t count)
Definition syscalls.h:208
static _NORETURN void _syscall_process_exit(uint64_t status)
Definition syscalls.h:91
static uint64_t _syscall_link(const char *oldPath, const char *newPath)
Definition syscalls.h:243
static _NORETURN void _syscall_thread_exit(void)
Definition syscalls.h:99
static tid_t _syscall_thread_create(void *entry, void *arg)
Definition syscalls.h:213
static fd_t _syscall_open(const char *path)
Definition syscalls.h:143
static clock_t _syscall_uptime(void)
Definition syscalls.h:133
static uint64_t _syscall_chdir(const char *path)
Definition syscalls.h:178
static uint64_t _syscall_seek(fd_t fd, int64_t offset, seek_origin_t origin)
Definition syscalls.h:168
static uint64_t _syscall_munmap(void *address, uint64_t length)
Definition syscalls.h:198
static uint64_t _syscall_nanosleep(clock_t nanoseconds)
Definition syscalls.h:113
static uint64_t _syscall_bind(fd_t source, const char *mountpoint)
Definition syscalls.h:258
static tid_t _syscall_gettid(void)
Definition syscalls.h:128
#define _SYSCALL2(retType, num, type1, arg1, type2, arg2)
Definition syscalls.h:27
static uint64_t _syscall_open2(const char *path, fd_t fds[2])
Definition syscalls.h:148
static fd_t _syscall_dup(fd_t oldFd)
Definition syscalls.h:223
static void * _syscall_mmap(fd_t fd, void *address, uint64_t length, prot_t prot)
Definition syscalls.h:193
static uint64_t _syscall_close(fd_t fd)
Definition syscalls.h:153
static uint64_t _syscall_ioctl(fd_t fd, uint64_t request, void *argp, uint64_t size)
Definition syscalls.h:173
static fd_t _syscall_claim(key_t *key)
Definition syscalls.h:253
static uint64_t _syscall_stat(const char *path, stat_t *info)
Definition syscalls.h:188
static void _syscall_yield(void)
Definition syscalls.h:218
static uint64_t _syscall_write(fd_t fd, const void *buffer, uint64_t count)
Definition syscalls.h:163
static errno_t _syscall_errno(void)
Definition syscalls.h:118
static uint64_t _syscall_poll(pollfd_t *fds, uint64_t amount, clock_t timeout)
Definition syscalls.h:183
static uint64_t _syscall_read(fd_t fd, void *buffer, uint64_t count)
Definition syscalls.h:158
static time_t _syscall_unix_epoch(void)
Definition syscalls.h:138
static pid_t _syscall_spawn(const char **argv, const spawn_fd_t *fds, const char *cwd, spawn_attr_t *attr)
Definition syscalls.h:107
static uint64_t _syscall_futex(atomic_uint64_t *addr, uint64_t val, futex_op_t op, clock_t timeout)
Definition syscalls.h:233
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
Directory entry struct.
Definition io.h:424
Key type.
Definition io.h:503
Poll file descriptor structure.
Definition io.h:307
Stucture used to duplicate fds in spawn().
Definition proc.h:55
Stat type.
Definition io.h:360
long long unsigned time_t
Definition time_t.h:4