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