PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
syscalls.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <stdint.h>
6#include <sys/fs.h>
7#include <sys/ioring.h>
8#include <sys/proc.h>
9#include <time.h>
10
11#define _SYSCALL0(retType, num) \
12 ({ \
13 register retType ret asm("rax"); \
14 ASM("syscall\n" : "=a"(ret) : "a"(num) : "rcx", "r11", "memory"); \
15 ret; \
16 })
17
18#define _SYSCALL1(retType, num, type1, arg1) \
19 ({ \
20 register retType ret asm("rax"); \
21 register type1 _a1 asm("rdi") = (arg1); \
22 ASM("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1) : "rcx", "r11", "memory"); \
23 ret; \
24 })
25
26#define _SYSCALL2(retType, num, type1, arg1, type2, arg2) \
27 ({ \
28 register retType ret asm("rax"); \
29 register type1 _a1 asm("rdi") = (arg1); \
30 register type2 _a2 asm("rsi") = (arg2); \
31 ASM("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1), "r"(_a2) : "rcx", "r11", "memory"); \
32 ret; \
33 })
34
35#define _SYSCALL3(retType, num, type1, arg1, type2, arg2, type3, arg3) \
36 ({ \
37 register retType ret asm("rax"); \
38 register type1 _a1 asm("rdi") = (arg1); \
39 register type2 _a2 asm("rsi") = (arg2); \
40 register type3 _a3 asm("rdx") = (arg3); \
41 ASM("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1), "r"(_a2), "r"(_a3) : "rcx", "r11", "memory"); \
42 ret; \
43 })
44
45#define _SYSCALL4(retType, num, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
46 ({ \
47 register retType ret asm("rax"); \
48 register type1 _a1 asm("rdi") = (arg1); \
49 register type2 _a2 asm("rsi") = (arg2); \
50 register type3 _a3 asm("rdx") = (arg3); \
51 register type4 _a4 asm("r10") = (arg4); \
52 ASM("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1), "r"(_a2), "r"(_a3), "r"(_a4) : "rcx", "r11", "memory"); \
53 ret; \
54 })
55
56#define _SYSCALL5(retType, num, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \
57 ({ \
58 register retType ret asm("rax"); \
59 register type1 _a1 asm("rdi") = (arg1); \
60 register type2 _a2 asm("rsi") = (arg2); \
61 register type3 _a3 asm("rdx") = (arg3); \
62 register type4 _a4 asm("r10") = (arg4); \
63 register type5 _a5 asm("r8") = (arg5); \
64 ASM("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1), "r"(_a2), "r"(_a3), "r"(_a4), "r"(_a5) : "rcx", "r11", \
65 "memory"); \
66 ret; \
67 })
68
69#define _SYSCALL6(retType, num, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5, type6, arg6) \
70 ({ \
71 register retType ret asm("rax"); \
72 register type1 _a1 asm("rdi") = (arg1); \
73 register type2 _a2 asm("rsi") = (arg2); \
74 register type3 _a3 asm("rdx") = (arg3); \
75 register type4 _a4 asm("r10") = (arg4); \
76 register type5 _a5 asm("r8") = (arg5); \
77 register type6 _a6 asm("r9") = (arg6); \
78 ASM("syscall\n" : "=a"(ret) : "a"(num), "r"(_a1), "r"(_a2), "r"(_a3), "r"(_a4), "r"(_a5), "r"(_a6) : "rcx", \
79 "r11", "memory"); \
80 ret; \
81 })
82
83_NORETURN static inline void _syscall_exits(const char* status)
84{
85 _SYSCALL1(uint64_t, SYS_EXITS, const char*, status);
86 ASM("ud2");
87 __builtin_unreachable();
88}
89
90_NORETURN static inline void _syscall_thread_exit(void)
91{
93 ASM("ud2");
94 __builtin_unreachable();
95}
96
97static inline pid_t _syscall_spawn(const char** argv, spawn_flags_t flags)
98{
99 return _SYSCALL2(pid_t, SYS_SPAWN, const char**, argv, spawn_flags_t, flags);
100}
101
102static inline uint64_t _syscall_nanosleep(clock_t nanoseconds)
103{
104 return _SYSCALL1(uint64_t, SYS_NANOSLEEP, clock_t, nanoseconds);
105}
106
107static inline errno_t _syscall_errno(void)
108{
109 return _SYSCALL0(errno_t, SYS_ERRNO);
110}
111
112static inline pid_t _syscall_getpid(void)
113{
114 return _SYSCALL0(pid_t, SYS_GETPID);
115}
116
117static inline tid_t _syscall_gettid(void)
118{
119 return _SYSCALL0(tid_t, SYS_GETTID);
120}
121
122static inline clock_t _syscall_uptime(void)
123{
125}
126
127static inline time_t _syscall_unix_epoch(void)
128{
129 return _SYSCALL0(time_t, SYS_EPOCH);
130}
131
132static inline fd_t _syscall_open(const char* path)
133{
134 return _SYSCALL1(fd_t, SYS_OPEN, const char*, path);
135}
136
137static inline uint64_t _syscall_open2(const char* path, fd_t fds[2])
138{
139 return _SYSCALL2(uint64_t, SYS_OPEN2, const char*, path, fd_t*, fds);
140}
141
142static inline uint64_t _syscall_close(fd_t fd)
143{
144 return _SYSCALL1(uint64_t, SYS_CLOSE, fd_t, fd);
145}
146
147static inline uint64_t _syscall_read(fd_t fd, void* buffer, size_t count)
148{
149 return _SYSCALL3(uint64_t, SYS_READ, fd_t, fd, void*, buffer, uint64_t, count);
150}
151
152static inline uint64_t _syscall_write(fd_t fd, const void* buffer, size_t count)
153{
154 return _SYSCALL3(uint64_t, SYS_WRITE, fd_t, fd, const void*, buffer, uint64_t, count);
155}
156
158{
159 return _SYSCALL3(uint64_t, SYS_SEEK, fd_t, fd, int64_t, offset, seek_origin_t, origin);
160}
161
162static inline uint64_t _syscall_ioctl(fd_t fd, uint64_t request, void* argp, size_t size)
163{
164 return _SYSCALL4(uint64_t, SYS_IOCTL, fd_t, fd, uint64_t, request, void*, argp, uint64_t, size);
165}
166
167static inline uint64_t _syscall_poll(pollfd_t* fds, uint64_t amount, clock_t timeout)
168{
169 return _SYSCALL3(uint64_t, SYS_POLL, pollfd_t*, fds, uint64_t, amount, clock_t, timeout);
170}
171
172static inline uint64_t _syscall_stat(const char* path, stat_t* info)
173{
174 return _SYSCALL2(uint64_t, SYS_STAT, const char*, path, stat_t*, info);
175}
176
177static inline void* _syscall_mmap(fd_t fd, void* address, size_t length, prot_t prot)
178{
179 return _SYSCALL4(void*, SYS_MMAP, fd_t, fd, void*, address, uint64_t, length, prot_t, prot);
180}
181
182static inline void* _syscall_munmap(void* address, size_t length)
183{
184 return _SYSCALL2(void*, SYS_MUNMAP, void*, address, uint64_t, length);
185}
186
187static inline void* _syscall_mprotect(void* address, size_t length, prot_t prot)
188{
189 return _SYSCALL3(void*, SYS_MPROTECT, void*, address, uint64_t, length, prot_t, prot);
190}
191
196
197static inline tid_t _syscall_thread_create(void* entry, void* arg)
198{
199 return _SYSCALL2(tid_t, SYS_THREAD_CREATE, void*, entry, void*, arg);
200}
201
202static inline void _syscall_yield(void)
203{
205}
206
207static inline fd_t _syscall_dup(fd_t oldFd)
208{
209 return _SYSCALL1(fd_t, SYS_DUP, fd_t, oldFd);
210}
211
212static inline fd_t _syscall_dup2(fd_t oldFd, fd_t newFd)
213{
214 return _SYSCALL2(fd_t, SYS_DUP2, fd_t, oldFd, fd_t, newFd);
215}
216
217static inline uint64_t _syscall_futex(atomic_uint64_t* addr, uint64_t val, futex_op_t op, clock_t timeout)
218{
219 return _SYSCALL4(uint64_t, SYS_FUTEX, atomic_uint64_t*, addr, uint64_t, val, futex_op_t, op, clock_t, timeout);
220}
221
222static inline uint64_t _syscall_remove(const char* path)
223{
224 return _SYSCALL1(uint64_t, SYS_REMOVE, const char*, path);
225}
226
227static inline uint64_t _syscall_link(const char* oldPath, const char* newPath)
228{
229 return _SYSCALL2(uint64_t, SYS_LINK, const char*, oldPath, const char*, newPath);
230}
231
232static inline uint64_t _syscall_share(char* key, uint64_t size, fd_t fd, clock_t timeout)
233{
234 return _SYSCALL4(uint64_t, SYS_SHARE, char*, key, uint64_t, size, fd_t, fd, clock_t, timeout);
235}
236
237static inline fd_t _syscall_claim(const char* key)
238{
239 return _SYSCALL1(fd_t, SYS_CLAIM, const char*, key);
240}
241
242static inline uint64_t _syscall_bind(const char* mountpoint, fd_t source)
243{
244 return _SYSCALL2(uint64_t, SYS_BIND, const char*, mountpoint, fd_t, source);
245}
246
247static inline fd_t _syscall_openat(fd_t from, const char* path)
248{
249 return _SYSCALL2(fd_t, SYS_OPENAT, fd_t, from, const char*, path);
250}
251
253{
255}
256
258{
260 ASM("ud2");
261 __builtin_unreachable();
262}
263
264static inline uint64_t _syscall_readlink(const char* path, char* buffer, uint64_t size)
265{
266 return _SYSCALL3(uint64_t, SYS_READLINK, const char*, path, char*, buffer, uint64_t, size);
267}
268
269static inline uint64_t _syscall_symlink(const char* target, const char* linkpath)
270{
271 return _SYSCALL2(uint64_t, SYS_SYMLINK, const char*, target, const char*, linkpath);
272}
273
274static inline uint64_t _syscall_mount(const char* mountpoint, const char* fs, const char* options)
275{
276 return _SYSCALL3(uint64_t, SYS_MOUNT, const char*, mountpoint, const char*, fs, const char*, options);
277}
278
279static inline uint64_t _syscall_umount(const char* mountpoint)
280{
281 return _SYSCALL1(uint64_t, SYS_UNMOUNT, const char*, mountpoint);
282}
283
285{
287}
288
289static inline uint64_t _syscall_setup(ioring_t* ring, void* address, size_t sentries, size_t centries)
290{
291 return _SYSCALL4(uint64_t, SYS_SETUP, ioring_t*, ring, void*, address, size_t, sentries, size_t, centries);
292}
293
295{
297}
298
299static inline uint64_t _syscall_enter(ioring_id_t id, size_t amount, size_t wait)
300{
301 return _SYSCALL3(uint64_t, SYS_ENTER, ioring_id_t, id, size_t, amount, size_t, wait);
302}
#define _NORETURN
Definition config.h:28
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
int errno_t
Definition errno_t.h:4
@ SYS_SHARE
Definition syscall.h:94
@ SYS_MMAP
Definition syscall.h:83
@ SYS_MOUNT
Definition syscall.h:102
@ SYS_STAT
Definition syscall.h:82
@ SYS_WRITE
Definition syscall.h:78
@ SYS_SYMLINK
Definition syscall.h:101
@ SYS_LINK
Definition syscall.h:93
@ SYS_NANOSLEEP
Definition syscall.h:68
@ SYS_SETUP
Definition syscall.h:105
@ SYS_YIELD
Definition syscall.h:88
@ SYS_GETTID
Definition syscall.h:71
@ SYS_THREAD_EXIT
Definition syscall.h:66
@ SYS_READ
Definition syscall.h:77
@ SYS_OPEN2
Definition syscall.h:75
@ SYS_CLOSE
Definition syscall.h:76
@ SYS_POLL
Definition syscall.h:81
@ SYS_UNMOUNT
Definition syscall.h:103
@ SYS_ERRNO
Definition syscall.h:69
@ SYS_EPOCH
Definition syscall.h:73
@ SYS_MUNMAP
Definition syscall.h:84
@ SYS_OPEN
Definition syscall.h:74
@ SYS_IOCTL
Definition syscall.h:80
@ SYS_TEARDOWN
Definition syscall.h:106
@ SYS_ARCH_PRCTL
Definition syscall.h:104
@ SYS_ENTER
Definition syscall.h:107
@ SYS_OPENAT
Definition syscall.h:97
@ SYS_GETPID
Definition syscall.h:70
@ SYS_NOTIFY
Definition syscall.h:98
@ SYS_GETDENTS
Definition syscall.h:86
@ SYS_BIND
Definition syscall.h:96
@ SYS_THREAD_CREATE
Definition syscall.h:87
@ SYS_DUP
Definition syscall.h:89
@ SYS_EXITS
Definition syscall.h:65
@ SYS_READLINK
Definition syscall.h:100
@ SYS_CLAIM
Definition syscall.h:95
@ SYS_FUTEX
Definition syscall.h:91
@ SYS_DUP2
Definition syscall.h:90
@ SYS_SPAWN
Definition syscall.h:67
@ SYS_MPROTECT
Definition syscall.h:85
@ SYS_NOTED
Definition syscall.h:99
@ SYS_REMOVE
Definition syscall.h:92
@ SYS_UPTIME
Definition syscall.h:72
@ SYS_SEEK
Definition syscall.h:79
static clock_source_t source
Structure to describe the HPET to the sys time subsystem.
Definition hpet.c:193
static uintptr_t address
Mapped virtual address of the HPET registers.
Definition hpet.c:96
#define ASM(...)
Inline assembly macro.
Definition defs.h:160
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition fs.h:260
uint64_t ioring_id_t
I/O ring ID type.
Definition ioring.h:195
void(* note_func_t)(char *note)
Note handler function type.
Definition proc.h:255
futex_op_t
Futex operation enum.
Definition proc.h:179
arch_prctl_t
Architecture specific thread data codes.
Definition proc.h:347
spawn_flags_t
Spawn behaviour flags.
Definition proc.h:52
prot_t
Memory protection flags.
Definition proc.h:122
__INT64_TYPE__ ssize_t
Signed size type.
Definition ssize_t.h:11
__UINT64_TYPE__ tid_t
Thread Identifier.
Definition tid_t.h:12
__UINT64_TYPE__ fd_t
File descriptor type.
Definition fd_t.h:10
__UINT64_TYPE__ pid_t
Process Identifier.
Definition pid_t.h:11
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static uint64_t offset
Definition screen.c:19
static const path_flag_t flags[]
Definition path.c:47
static atomic_long count
Definition main.c:11
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
__INT64_TYPE__ int64_t
Definition stdint.h:16
Directory entry struct.
Definition fs.h:455
User I/O ring structure.
Definition ioring.h:204
Poll file descriptor structure.
Definition fs.h:305
Stat type.
Definition fs.h:378
static fd_t _syscall_dup2(fd_t oldFd, fd_t newFd)
Definition syscalls.h:212
static uint64_t _syscall_ioctl(fd_t fd, uint64_t request, void *argp, size_t size)
Definition syscalls.h:162
static uint64_t _syscall_enter(ioring_id_t id, size_t amount, size_t wait)
Definition syscalls.h:299
static uint64_t _syscall_bind(const char *mountpoint, fd_t source)
Definition syscalls.h:242
#define _SYSCALL4(retType, num, type1, arg1, type2, arg2, type3, arg3, type4, arg4)
Definition syscalls.h:45
static uint64_t _syscall_teardown(ioring_id_t id)
Definition syscalls.h:294
static pid_t _syscall_getpid(void)
Definition syscalls.h:112
#define _SYSCALL3(retType, num, type1, arg1, type2, arg2, type3, arg3)
Definition syscalls.h:35
#define _SYSCALL1(retType, num, type1, arg1)
Definition syscalls.h:18
static uint64_t _syscall_remove(const char *path)
Definition syscalls.h:222
#define _SYSCALL0(retType, num)
Definition syscalls.h:11
static uint64_t _syscall_getdents(fd_t fd, dirent_t *buffer, uint64_t count)
Definition syscalls.h:192
static uint64_t _syscall_link(const char *oldPath, const char *newPath)
Definition syscalls.h:227
static _NORETURN void _syscall_thread_exit(void)
Definition syscalls.h:90
static uint64_t _syscall_mount(const char *mountpoint, const char *fs, const char *options)
Definition syscalls.h:274
static uint64_t _syscall_arch_prctl(arch_prctl_t code, uintptr_t addr)
Definition syscalls.h:284
static tid_t _syscall_thread_create(void *entry, void *arg)
Definition syscalls.h:197
static fd_t _syscall_open(const char *path)
Definition syscalls.h:132
static uint64_t _syscall_umount(const char *mountpoint)
Definition syscalls.h:279
static clock_t _syscall_uptime(void)
Definition syscalls.h:122
static void * _syscall_munmap(void *address, size_t length)
Definition syscalls.h:182
static void * _syscall_mprotect(void *address, size_t length, prot_t prot)
Definition syscalls.h:187
static uint64_t _syscall_share(char *key, uint64_t size, fd_t fd, clock_t timeout)
Definition syscalls.h:232
static _NORETURN void _syscall_exits(const char *status)
Definition syscalls.h:83
static _NORETURN uint64_t _syscall_noted(void)
Definition syscalls.h:257
static uint64_t _syscall_nanosleep(clock_t nanoseconds)
Definition syscalls.h:102
static tid_t _syscall_gettid(void)
Definition syscalls.h:117
static uint64_t _syscall_seek(fd_t fd, ssize_t offset, seek_origin_t origin)
Definition syscalls.h:157
#define _SYSCALL2(retType, num, type1, arg1, type2, arg2)
Definition syscalls.h:26
static pid_t _syscall_spawn(const char **argv, spawn_flags_t flags)
Definition syscalls.h:97
static uint64_t _syscall_open2(const char *path, fd_t fds[2])
Definition syscalls.h:137
static fd_t _syscall_dup(fd_t oldFd)
Definition syscalls.h:207
static uint64_t _syscall_close(fd_t fd)
Definition syscalls.h:142
static uint64_t _syscall_setup(ioring_t *ring, void *address, size_t sentries, size_t centries)
Definition syscalls.h:289
static uint64_t _syscall_stat(const char *path, stat_t *info)
Definition syscalls.h:172
static void _syscall_yield(void)
Definition syscalls.h:202
static errno_t _syscall_errno(void)
Definition syscalls.h:107
static uint64_t _syscall_symlink(const char *target, const char *linkpath)
Definition syscalls.h:269
static uint64_t _syscall_poll(pollfd_t *fds, uint64_t amount, clock_t timeout)
Definition syscalls.h:167
static fd_t _syscall_openat(fd_t from, const char *path)
Definition syscalls.h:247
static uint64_t _syscall_readlink(const char *path, char *buffer, uint64_t size)
Definition syscalls.h:264
static time_t _syscall_unix_epoch(void)
Definition syscalls.h:127
static uint64_t _syscall_read(fd_t fd, void *buffer, size_t count)
Definition syscalls.h:147
static uint64_t _syscall_write(fd_t fd, const void *buffer, size_t count)
Definition syscalls.h:152
static uint64_t _syscall_futex(atomic_uint64_t *addr, uint64_t val, futex_op_t op, clock_t timeout)
Definition syscalls.h:217
static void * _syscall_mmap(fd_t fd, void *address, size_t length, prot_t prot)
Definition syscalls.h:177
static uint64_t _syscall_notify(note_func_t func)
Definition syscalls.h:252
static fd_t _syscall_claim(const char *key)
Definition syscalls.h:237
long long unsigned time_t
Definition time_t.h:4