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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
#include <euler/syscall.hpp>
#include <string>
#include <vector>
extern "C" void __euler_do_syscall(
uint64_t &rax, uint64_t &rdi, uint64_t &rsi, uint64_t &rdx);
namespace euler::syscall {
encoded_color encode_color(
uint8_t r, uint8_t g, uint8_t b) {
uint64_t rax = 0;
uint64_t rdi = (uint32_t)r | ((uint32_t)g << 8) | ((uint32_t)b << 16);
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
return (encoded_color)(rax & 0xffffffff);
}
void get_framebuffer(
encoded_color *&buffer_out, uint32_t &width_out,
uint32_t &height_out, uint32_t &pitch_out) {
uint64_t rax = 1;
uint64_t rdi;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
buffer_out = (encoded_color *)rax;
width_out = rdi & 0xffffffff;
height_out = rdi >> 32;
pitch_out = rsi & 0xffffffff;
}
stream_result open_file(
const std::string &file_path, bool allow_creation,
bool only_allow_creation, stream_handle &handle_out) {
uint64_t rax = 2;
uint64_t rdi = (uint64_t)file_path.data();
uint64_t rsi = file_path.size();
uint64_t rdx =
( allow_creation ? 0x1 : 0x0) |
(only_allow_creation ? 0x2 : 0x0);
__euler_do_syscall(rax, rdi, rsi, rdx);
handle_out = rdi;
return (stream_result)rax;
}
[[noreturn]] void end_this_thread(int32_t exit_code) {
uint64_t rax = 3;
uint64_t rdi = (uint32_t)exit_code;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
__builtin_unreachable();
}
void *get_new_pages(uint64_t n_pages) {
uint64_t rax = 4;
uint64_t rdi = n_pages;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
return (void *)rax;
}
std::variant<mouse_packet, key_packet> get_input_packet() {
uint64_t rax = 5;
uint64_t rdi;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
if (rax & 0x80)
return (mouse_packet){
. left_button_down = (rax & 0x01) != 0,
. right_button_down = (rax & 0x02) != 0,
.middle_button_down = (rax & 0x04) != 0,
.x_changed = (int16_t)(rdi & 0xffff),
.y_changed = (int16_t)(rsi & 0xffff)
};
return (key_packet){
.was_key_up_event = (rdi & 0x40000) != 0,
. num_lock = (rdi & 0x20000) != 0,
. caps_lock = (rdi & 0x10000) != 0,
. right_win = (rdi & 0x8000) != 0,
. left_win = (rdi & 0x4000) != 0,
. right_alt = (rdi & 0x2000) != 0,
. left_alt = (rdi & 0x1000) != 0,
. right_ctrl = (rdi & 0x800) != 0,
. left_ctrl = (rdi & 0x400) != 0,
. right_shift = (rdi & 0x200) != 0,
. left_shift = (rdi & 0x100) != 0,
. key_code = (uint8_t)(rdi & 0xff)
};
}
void create_private_socket(
stream_handle &end_1_out, stream_handle &end_2_out) {
uint64_t rax = 6;
uint64_t rdi;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
end_1_out = rax;
end_2_out = rdi;
}
stream_result create_socket_listener(
const std::string &id, listener_handle &handle_out) {
uint64_t rax = 7;
uint64_t rdi = (uint64_t)id.data();
uint64_t rsi = id.size();
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
handle_out = rdi;
return (stream_result)rax;
}
void stop_socket_listener(listener_handle handle) {
uint64_t rax = 8;
uint64_t rdi = handle;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
}
stream_result accept_socket_connection(
listener_handle listener, stream_handle &stream_out) {
uint64_t rax = 9;
uint64_t rdi = listener;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
stream_out = rdi;
return (stream_result)rax;
}
stream_result connect_to_socket(
const std::string &id, stream_handle &handle_out) {
uint64_t rax = 10;
uint64_t rdi = (uint64_t)id.data();
uint64_t rsi = id.size();
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
handle_out = rdi;
return (stream_result)rax;
}
void close_stream(stream_handle handle) {
uint64_t rax = 11;
uint64_t rdi = handle;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
}
stream_result seek_stream(
stream_handle handle, seek_from from, int64_t offset) {
uint64_t rax = 12;
uint64_t rdi = (uint64_t)handle;
uint64_t rsi = (uint8_t)from;
uint64_t rdx = (uint64_t)offset;
__euler_do_syscall(rax, rdi, rsi, rdx);
return (stream_result)rax;
}
stream_result read_from_stream(
stream_handle handle, uint64_t bytes, void *into) {
uint64_t rax = 13;
uint64_t rdi = (uint64_t)handle;
uint64_t rsi = bytes;
uint64_t rdx = (uint64_t)into;
__euler_do_syscall(rax, rdi, rsi, rdx);
return (stream_result)rax;
}
stream_result write_to_stream(
stream_handle handle, uint64_t bytes, const void *from) {
uint64_t rax = 14;
uint64_t rdi = (uint64_t)handle;
uint64_t rsi = bytes;
uint64_t rdx = (uint64_t)from;
__euler_do_syscall(rax, rdi, rsi, rdx);
return (stream_result)rax;
}
stream_result get_stream_length(
stream_handle handle, uint64_t &length_out) {
uint64_t rax = 15;
uint64_t rdi = (uint64_t)handle;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
length_out = rdi;
return (stream_result)rax;
}
stream_result start_process(
const std::string &file_path,
const std::vector<std::pair<std::string, std::string>> &environment_variables,
const std::vector<std::pair<stream_handle, stream_result>> &gifted_streams,
process_handle &handle_out) {
std::vector<uint64_t> ev_structs(environment_variables.size() * 4);
for (size_t i = 0; i < environment_variables.size(); ++i) {
ev_structs[i * 4] = environment_variables[i]. first.size();
ev_structs[i * 4 + 1] = (uint64_t)environment_variables[i]. first.data();
ev_structs[i * 4 + 2] = environment_variables[i].second.size();
ev_structs[i * 4 + 3] = (uint64_t)environment_variables[i].second.data();
}
std::vector<uint64_t> gs_structs(gifted_streams.size() * 2);
for (size_t i = 0; i < environment_variables.size(); ++i) {
gs_structs[i * 2] = (uint64_t)gifted_streams[i]. first;
gs_structs[i * 2 + 1] = (uint64_t)gifted_streams[i].second;
}
uint64_t psi_struct[] = {
file_path.size(), (uint64_t) file_path.data(),
environment_variables.size(), (uint64_t)ev_structs.data(),
gifted_streams.size(), (uint64_t)gs_structs.data()
};
uint64_t rax = 16;
uint64_t rdi = (uint64_t)psi_struct;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
handle_out = rdi;
return (stream_result)rax;
}
[[noreturn]] void end_this_process(int32_t exit_code) {
uint64_t rax = 17;
uint64_t rdi = (uint32_t)exit_code;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
__builtin_unreachable();
}
stream_result set_stream_length(
stream_handle handle, uint64_t new_length) {
uint64_t rax = 18;
uint64_t rdi = (uint64_t)handle;
uint64_t rsi = new_length;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
return (stream_result)rax;
}
stream_result get_other_end_process_handle(
stream_handle stream, process_handle &process_out) {
uint64_t rax = 19;
uint64_t rdi = (uint64_t)stream;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
process_out = (process_handle)rdi;
return (stream_result)rax;
}
//entry_point must not return
void start_thread(void (*entry_point)(uint64_t), uint64_t arg) {
uint64_t rax = 20;
uint64_t rdi = (uint64_t)entry_point;
uint64_t rsi = arg;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
}
//entry_point must not return
void start_thread(void (*entry_point)()) {
uint64_t rax = 20;
uint64_t rdi = (uint64_t)entry_point;
uint64_t rsi = 0;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
}
//return value is number of bytes cleared
uint64_t clear_socket_read_queue(stream_handle handle) {
uint64_t rax = 21;
uint64_t rdi = handle;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
return rax;
}
//this function has a race condition if the variable is changed
//or unset by another thread between the two syscalls.
std::optional<std::string> try_get_environment_variable(
const std::string &name) {
uint64_t rax = 22;
uint64_t rdi = (uint64_t)name.data();
uint64_t rsi = name.size();
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
if (rax == (uint64_t)-1)
return {};
std::string s;
s.resize(rax);
rax = 23;
rdi = (uint64_t)name.data();
rsi = name.size();
rdx = (uint64_t)s.data();
__euler_do_syscall(rax, rdi, rsi, rdx);
//do i need to tell gcc that s is modified?
return s;
}
void set_thread_name(const std::string &name) {
uint64_t rax = 24;
uint64_t rdi = (uint64_t)name.data();
uint64_t rsi = name.size();
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
}
void sleep(uint64_t mibiseconds) {
uint64_t rax = 25;
uint64_t rdi = mibiseconds;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
}
uint64_t get_time() {
uint64_t rax = 26;
uint64_t rdi;
uint64_t rsi;
uint64_t rdx;
__euler_do_syscall(rax, rdi, rsi, rdx);
return rax;
}
}
|