1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/thread-entry.hpp>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
namespace mlibc {
void sys_libc_log(const char *message) {
unsigned long res;
asm volatile ("syscall" : "=a"(res)
: "a"(50), "D"(message)
: "rcx", "r11", "rdx");
}
void sys_libc_panic() {
mlibc::infoLogger() << "\e[31mmlibc: panic!" << frg::endlog;
asm volatile ("syscall" :
: "a"(12), "D"(1)
: "rcx", "r11", "rdx");
}
int sys_tcb_set(void *pointer) {
int res;
asm volatile ("syscall" : "=a"(res)
: "a"(300), "D"(pointer)
: "rcx", "r11", "rdx");
return res;
}
int sys_anon_allocate(size_t size, void **pointer) {
void *ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(9), "D"(0), "S"(size)
: "rcx", "r11");
if (!ret)
return sys_errno;
*pointer = ret;
return 0;
}
int sys_anon_free(void *pointer, size_t size) {
int unused_return;
int sys_errno;
asm volatile ("syscall"
: "=a"(unused_return), "=d"(sys_errno)
: "a"(11), "D"(pointer), "S"(size)
: "rcx", "r11");
if (unused_return)
return sys_errno;
return 0;
}
#ifndef MLIBC_BUILDING_RTDL
void sys_exit(int status) {
asm volatile ("syscall" :
: "a"(12), "D"(status)
: "rcx", "r11", "rdx");
}
#endif
#ifndef MLIBC_BUILDING_RTDL
int sys_clock_get(int clock, time_t *secs, long *nanos) {
return 0;
}
#endif
int sys_open(const char *path, int flags, mode_t mode, int *fd) {
int ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(2), "D"(path), "S"(flags), "d"(0)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*fd = ret;
return 0;
}
int sys_close(int fd) {
int ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(3), "D"(fd)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
return 0;
}
int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
ssize_t ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(0), "D"(fd), "S"(buf), "d"(count)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*bytes_read = ret;
return 0;
}
#ifndef MLIBC_BUILDING_RTDL
int sys_write(int fd, const void *buf, size_t count, ssize_t *bytes_written) {
ssize_t ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(1), "D"(fd), "S"(buf), "d"(count)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*bytes_written = ret;
return 0;
}
#endif
int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
off_t ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(8), "D"(fd), "S"(offset), "d"(whence)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*new_offset = ret;
return 0;
}
int sys_vm_map(void *hint, size_t size, int prot, int flags,
int fd, off_t offset, void **window) {
__ensure(flags & MAP_ANONYMOUS);
void *ret;
int sys_errno;
// mlibc::infoLogger() << "calling sys_vm_map with size: " << size << frg::endlog;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(9), "D"(hint), "S"(size)
: "rcx", "r11");
if (!ret)
return sys_errno;
*window = ret;
return 0;
}
int sys_vm_unmap(void *pointer, size_t size) {
return sys_anon_free(pointer, size);
}
int sys_futex_wait(int *pointer, int expected, const struct timespec *time) {
uint64_t err;
asm volatile ("syscall"
: "=d"(err)
: "a"(66), "D"(pointer), "S"(expected)
: "rcx", "r11");
if (err) {
return -1;
}
return 0;
}
int sys_futex_wake(int *pointer) {
uint64_t err;
asm volatile ("syscall"
: "=d"(err)
: "a"(65), "D"(pointer)
: "rcx", "r11");
if (err) {
return -1;
}
return 0;
}
// All remaining functions are disabled in ldso.
#ifndef MLIBC_BUILDING_RTDL
int sys_clone(void *tcb, pid_t *tid_out, void *stack) {
int tid;
asm volatile ("syscall"
: "=a"(tid)
: "a"(67), "D"(__mlibc_start_thread), "S"(stack), "d"(tcb)
: "rcx", "r11");
if (tid_out)
*tid_out = tid;
return 0;
}
void sys_thread_exit() {
asm volatile ("syscall"
:
: "a"(68)
: "rcx", "r11");
__builtin_trap();
}
int sys_sleep(time_t *secs, long *nanos) {
long ms = (*nanos / 1000000) + (*secs * 1000);
asm volatile ("syscall"
:
: "a"(6), "D"(ms)
: "rcx", "r11");
*secs = 0;
*nanos = 0;
return 0;
}
int sys_fork(pid_t *child) {
pid_t ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(57)
: "rcx", "r11");
if (ret == -1)
return sys_errno;
*child = ret;
return 0;
}
int sys_execve(const char *path, char *const argv[], char *const envp[]) {
int ret;
int sys_errno;
asm volatile ("syscall"
: "=a"(ret), "=d"(sys_errno)
: "a"(59), "D"(path), "S"(argv), "d"(envp)
: "rcx", "r11");
if (sys_errno != 0)
return sys_errno;
return 0;
}
pid_t sys_getpid() {
pid_t pid;
asm volatile ("syscall" : "=a"(pid)
: "a"(5)
: "rcx", "r11", "rdx");
return pid;
}
pid_t sys_getppid() {
pid_t ppid;
asm volatile ("syscall" : "=a"(ppid)
: "a"(14)
: "rcx", "r11", "rdx");
return ppid;
}
#endif // MLIBC_BUILDING_RTDL
} // namespace mlibc
|