-
Notifications
You must be signed in to change notification settings - Fork 12
/
syscall.c
597 lines (501 loc) · 16.6 KB
/
syscall.c
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* Bindings for testing CodeGenC in -nostdlib mode
* https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md
*
* Built-in defines can be dumped via
* gcc -m32 -dM -E - < /dev/null
* gcc -dM -E - < /dev/null
* arm-linux-gnueabihf-gcc-9 -dM -E - < /dev/null
*
* This file can be tested like so
* gcc -nostdlib -static -D__test_main__=1 -m32 syscall.c
* ./a.out
* gcc -nostdlib -static -D__test_main__=1 syscall.c
* ./a.out
* aarch64-linux-gnu-gcc-8 -nostdlib -static -D__test_main__=1 syscall.c
* ./a.out
* arm-linux-gnueabihf-gcc-9 -nostdlib -static -D__test_main__=1 -marm
* -march=armv7ve syscall.c
* ./a.our
*/
#if !__linux__
#error "only linux supported"
#endif
extern int main(int argc, char** argv);
/* ============================================================ */
#if __x86_64__
/* ============================================================ */
long _x64_syscall0(long n) {
long ret;
__asm__ __volatile__("syscall" : "=a"(ret) : "a"(n) : "rcx", "r11", "memory");
return ret;
}
long _x64_syscall1(long n, long a1) {
long ret;
__asm__ __volatile__("syscall"
: "=a"(ret)
: "a"(n), "D"(a1)
: "rcx", "r11", "memory");
return ret;
}
long _x64_syscall2(long n, long a1, long a2) {
long ret;
__asm__ __volatile__("syscall"
: "=a"(ret)
: "a"(n), "D"(a1), "S"(a2)
: "rcx", "r11", "memory");
return ret;
}
long _x64_syscall3(long n, long a1, long a2, long a3) {
long ret;
__asm__ __volatile__("syscall"
: "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3)
: "rcx", "r11", "memory");
return ret;
}
long _x64_syscall4(long n, long a1, long a2, long a3, long a4) {
long ret;
register long r10 __asm__("r10") = a4;
__asm__ __volatile__("syscall"
: "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10)
: "rcx", "r11", "memory");
return ret;
}
long _x64_syscall5(long n, long a1, long a2, long a3, long a4, long a5) {
long ret;
register long r10 __asm__("r10") = a4;
register long r8 __asm__("r8") = a5;
__asm__ __volatile__("syscall"
: "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8)
: "rcx", "r11", "memory");
return ret;
}
long _x64_syscall6(long n, long a1, long a2, long a3, long a4, long a5,
long a6) {
long ret;
register long r10 __asm__("r10") = a4;
register long r8 __asm__("r8") = a5;
register long r9 __asm__("r9") = a6;
__asm__ __volatile__("syscall"
: "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8),
"r"(r9)
: "rcx", "r11", "memory");
return ret;
}
int clock_gettime(int clk_id, void* timespec) {
return _x64_syscall2(0x107, clk_id, (long)timespec);
}
int close(int fd) { return _x64_syscall1(3, fd); }
void exit(int status) {
_x64_syscall1(60, status);
while (1)
;
}
int getpid() { return _x64_syscall0(39); }
int kill(int pid, int sig) { return _x64_syscall2(62, pid, sig); }
int wait4(int pid, void* stat, int options, void* usage) {
return _x64_syscall4(61, pid, (long)stat, options, (long)usage);
}
int clone(long flags, void* stack, void* parent_tid, void* child_tid,
void* tls) {
return _x64_syscall5(56, flags, (long)stack, (long)parent_tid,
(long)child_tid, (long)tls);
}
int nanosleep(void* req, void* rem) {
return _x64_syscall2(35, (long)req, (long)rem);
}
long lseek(int fd, unsigned long offset, int whence) {
return _x64_syscall3(8, fd, offset, whence);
}
int open(const char* path, int oflag, int mode) {
return _x64_syscall3(2, (long)path, oflag, mode);
}
long read(int fd, void* buf, unsigned long n) {
return _x64_syscall3(0, fd, (long)buf, n);
}
long write(int fd, const void* buf, unsigned long n) {
return _x64_syscall3(1, fd, (long)buf, n);
}
void* xbrk(void* addr) { return (void*)_x64_syscall1(12, (long)addr); }
void _x64_main(long* args) { exit(main(args[0], (char**)&args[1])); }
/* linux has uses a non standard ABI when it starts a program */
__asm__(
".text\n"
".global _start\n"
"_start:\n"
" xor %rbp,%rbp\n"
" mov %rsp,%rdi\n"
" andq $-16,%rsp\n"
" call _x64_main\n");
/* ============================================================ */
#elif __i386__
/* ============================================================ */
long _x32_syscall0(long n) {
long ret;
__asm__ __volatile__("int $0x80" : "=a"(ret) : "a"(n) : "memory");
return ret;
}
long _x32_syscall1(long n, long a1) {
long ret;
__asm__ __volatile__("int $0x80" : "=a"(ret) : "a"(n), "b"(a1) : "memory");
return ret;
}
long _x32_syscall2(long n, long a1, long a2) {
long ret;
__asm__ __volatile__("int $0x80"
: "=a"(ret)
: "a"(n), "b"(a1), "c"(a2)
: "memory");
return ret;
}
long _x32_syscall3(long n, long a1, long a2, long a3) {
long ret;
__asm__ __volatile__("int $0x80"
: "=a"(ret)
: "a"(n), "b"(a1), "c"(a2), "d"(a3)
: "memory");
return ret;
}
long _x32_syscall4(long n, long a1, long a2, long a3, long a4) {
long ret;
__asm__ __volatile__("int $0x80"
: "=a"(ret)
: "a"(n), "b"(a1), "c"(a2), "d"(a3), "S"(a4)
: "memory");
return ret;
}
long _x32_syscall5(long n, long a1, long a2, long a3, long a4, long a5) {
long ret;
__asm__ __volatile__("int $0x80"
: "=a"(ret)
: "a"(n), "b"(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5)
: "memory");
return ret;
}
long _x32_syscall6(long n, long a1, long a2, long a3, long a4, long a5,
long a6) {
long ret;
__asm__ __volatile__(
"pushl %7;"
"push %%ebp;"
"mov 4(%%esp),%%ebp;"
"int $0x80;"
"pop %%ebp; "
"add $4,%%esp"
: "=a"(ret)
: "a"(n), "b"(a1), "c"(a2), "d"(a3), "S"(a4), "D"(a5), "g"(a6)
: "memory");
return ret;
}
int clock_gettime(int clk_id, void* timespec) {
return _x32_syscall2(0x109, clk_id, (long)timespec);
}
int close(int fd) { return _x32_syscall1(6, fd); }
void exit(int status) {
_x32_syscall1(1, status);
while (1)
;
}
int getpid() { return _x32_syscall0(20); }
int kill(int pid, int sig) { return _x32_syscall2(37, pid, sig); }
int wait4(int pid, void* stat, int options, void* usage) {
return _x32_syscall4(114, pid, (long)stat, options, (long)usage);
}
int clone(long flags, void* stack, void* parent_tid, void* child_tid,
void* tls) {
return _x32_syscall5(120, flags, (long)stack, (long)parent_tid,
(long)child_tid, (long)tls);
}
int nanosleep(void* req, void* rem) {
return _x32_syscall2(162, (long)req, (long)rem);
}
long lseek(int fd, unsigned long offset, int whence) {
return _x32_syscall3(19, fd, offset, whence);
}
int open(const char* path, int oflag, int mode) {
return _x32_syscall3(5, (long)path, oflag, mode);
}
long read(int fd, void* buf, unsigned long n) {
return _x32_syscall3(3, fd, (long)buf, n);
}
long write(int fd, const void* buf, unsigned long n) {
return _x32_syscall3(4, fd, (long)buf, n);
}
void* xbrk(void* addr) { return (void*)_x32_syscall1(45, (long)addr); }
void _x32_main(long* args) { exit(main(args[0], (char**)&args[1])); }
/* linux has uses a non standard ABI when it starts a program */
__asm__(
".text\n"
".global _start\n"
"_start:\n"
" xor %ebp,%ebp \n"
" mov %esp,%eax \n"
" and $-16,%esp \n"
" push %eax \n"
" call _x32_main\n");
/* ============================================================ */
#elif __aarch64__
/* ============================================================ */
long _a64_syscall0(long n) {
register long x8 __asm__("x8") = n;
register long x0 __asm__("x0");
__asm__ __volatile__("svc 0" : "=r"(x0) : "r"(x8) : "memory", "cc");
return x0;
}
long _a64_syscall1(long n, long a) {
register long x8 __asm__("x8") = n;
register long x0 __asm__("x0") = a;
__asm__ __volatile__("svc 0" : "=r"(x0) : "r"(x8), "0"(x0) : "memory", "cc");
return x0;
}
long _a64_syscall2(long n, long a, long b) {
register long x8 __asm__("x8") = n;
register long x0 __asm__("x0") = a;
register long x1 __asm__("x1") = b;
__asm__ __volatile__("svc 0"
: "=r"(x0)
: "r"(x8), "0"(x0), "r"(x1)
: "memory", "cc");
return x0;
}
long _a64_syscall3(long n, long a, long b, long c) {
register long x8 __asm__("x8") = n;
register long x0 __asm__("x0") = a;
register long x1 __asm__("x1") = b;
register long x2 __asm__("x2") = c;
__asm__ __volatile__("svc 0"
: "=r"(x0)
: "r"(x8), "0"(x0), "r"(x1), "r"(x2)
: "memory", "cc");
return x0;
}
long _a64_syscall4(long n, long a, long b, long c, long d) {
register long x8 __asm__("x8") = n;
register long x0 __asm__("x0") = a;
register long x1 __asm__("x1") = b;
register long x2 __asm__("x2") = c;
register long x3 __asm__("x3") = d;
__asm__ __volatile__("svc 0"
: "=r"(x0)
: "r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3)
: "memory", "cc");
return x0;
}
long _a64_syscall5(long n, long a, long b, long c, long d, long e) {
register long x8 __asm__("x8") = n;
register long x0 __asm__("x0") = a;
register long x1 __asm__("x1") = b;
register long x2 __asm__("x2") = c;
register long x3 __asm__("x3") = d;
register long x4 __asm__("x4") = e;
__asm__ __volatile__("svc 0"
: "=r"(x0)
: "r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4)
: "memory", "cc");
return x0;
}
long _a64_syscall6(long n, long a, long b, long c, long d, long e, long f) {
register long x8 __asm__("x8") = n;
register long x0 __asm__("x0") = a;
register long x1 __asm__("x1") = b;
register long x2 __asm__("x2") = c;
register long x3 __asm__("x3") = d;
register long x4 __asm__("x4") = e;
register long x5 __asm__("x5") = f;
__asm__ __volatile__("svc 0"
: "=r"(x0)
: "r"(x8), "0"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4),
"r"(x5)
: "memory", "cc");
return x0;
}
int clock_gettime(int clk_id, void* timespec) {
return _a64_syscall2(0x71, clk_id, (long)timespec);
}
int close(int fd) { return _a64_syscall1(57, fd); }
void exit(int status) {
_a64_syscall1(93, status);
while (1)
;
}
int getpid() { return _a64_syscall0(172); }
int kill(int pid, int sig) { return _a64_syscall2(129, pid, sig); }
int wait4(int pid, void* stat, int options, void* usage) {
return _a64_syscall4(260, pid, (long)stat, options, (long)usage);
}
int clone(long flags, void* stack, void* parent_tid, void* child_tid,
void* tls) {
return _a64_syscall5(220, flags, (long)stack, (long)parent_tid,
(long)child_tid, (long)tls);
}
int nanosleep(void* req, void* rem) {
return _a64_syscall2(101, (long)req, (long)rem);
}
long lseek(int fd, unsigned long offset, int whence) {
return _a64_syscall3(62, fd, offset, whence);
}
int openat(const char* path, int oflag, int mode) {
return _a64_syscall4(5, -100 /* AT_FDCWD */, (long)path, oflag, mode);
}
long read(int fd, void* buf, unsigned long n) {
return _a64_syscall3(63, fd, (long)buf, n);
}
long write(int fd, const void* buf, unsigned long n) {
return _a64_syscall3(64, fd, (long)buf, n);
}
void* xbrk(void* addr) { return (void*)_a64_syscall1(214, (long)addr); }
void _a64_main(long* args) { exit(main(args[0], (char**)&args[1])); }
/* linux has uses a non standard ABI when it starts a program */
__asm__(
".text\n"
".global _start\n"
".type _start, %function\n"
"_start:\n"
" mov x29, #0\n"
" mov x30, #0\n"
" mov x0, sp\n"
" and sp, x0, #-16\n"
" b _a64_main\n");
/* ============================================================ */
#elif __arm__
/* ============================================================ */
long _a32_syscall0(long n) {
register long r7 __asm__("r7") = n;
register long r0 __asm__("r0");
__asm__ __volatile__("svc 0" : "=r"(r0) : "r"(r7) : "memory");
return r0;
}
long _a32_syscall1(long n, long a) {
register long r7 __asm__("r7") = n;
register long r0 __asm__("r0") = a;
__asm__ __volatile__("svc 0" : "=r"(r0) : "r"(r7), "0"(r0) : "memory");
return r0;
}
long _a32_syscall2(long n, long a, long b) {
register long r7 __asm__("r7") = n;
register long r0 __asm__("r0") = a;
register long r1 __asm__("r1") = b;
__asm__ __volatile__("svc 0"
: "=r"(r0)
: "r"(r7), "0"(r0), "r"(r1)
: "memory");
return r0;
}
long _a32_syscall3(long n, long a, long b, long c) {
register long r7 __asm__("r7") = n;
register long r0 __asm__("r0") = a;
register long r1 __asm__("r1") = b;
register long r2 __asm__("r2") = c;
__asm__ __volatile__("svc 0"
: "=r"(r0)
: "r"(r7), "0"(r0), "r"(r1), "r"(r2)
: "memory");
return r0;
}
long _a32_syscall4(long n, long a, long b, long c, long d) {
register long r7 __asm__("r7") = n;
register long r0 __asm__("r0") = a;
register long r1 __asm__("r1") = b;
register long r2 __asm__("r2") = c;
register long r3 __asm__("r3") = d;
__asm__ __volatile__("svc 0"
: "=r"(r0)
: "r"(r7), "0"(r0), "r"(r1), "r"(r2), "r"(r3)
: "memory");
return r0;
}
long _a32_syscall5(long n, long a, long b, long c, long d, long e) {
register long r7 __asm__("r7") = n;
register long r0 __asm__("r0") = a;
register long r1 __asm__("r1") = b;
register long r2 __asm__("r2") = c;
register long r3 __asm__("r3") = d;
register long r4 __asm__("r4") = e;
__asm__ __volatile__("svc 0"
: "=r"(r0)
: "r"(r7), "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4)
: "memory");
return r0;
}
long _a32_syscall6(long n, long a, long b, long c, long d, long e, long f) {
register long r7 __asm__("r7") = n;
register long r0 __asm__("r0") = a;
register long r1 __asm__("r1") = b;
register long r2 __asm__("r2") = c;
register long r3 __asm__("r3") = d;
register long r4 __asm__("r4") = e;
register long r5 __asm__("r5") = f;
__asm__ __volatile__("svc 0"
: "=r"(r0)
: "r"(r7), "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4),
"r"(r5)
: "memory");
return r0;
}
int clock_gettime(int clk_id, void* timespec) {
return _a32_syscall2(0xe4, clk_id, (long)timespec);
}
int close(int fd) { return _a32_syscall1(6, fd); }
void exit(int status) {
_a32_syscall1(1, status);
while (1)
;
}
int getpid() { return _a32_syscall0(20); }
int kill(int pid, int sig) { return _a32_syscall2(37, pid, sig); }
int wait4(int pid, void* stat, int options, void* usage) {
return _a32_syscall4(120, pid, (long)stat, options, (long)usage);
}
int clone(long flags, void* stack, void* parent_tid, void* child_tid,
void* tls) {
return _a32_syscall5(114, flags, (long)stack, (long)parent_tid,
(long)child_tid, (long)tls);
}
int nanosleep(void* req, void* rem) {
return _a32_syscall2(162, (long)req, (long)rem);
}
long lseek(int fd, unsigned long offset, int whence) {
return _a32_syscall3(19, fd, offset, whence);
}
int open(const char* path, int oflag, int mode) {
return _a32_syscall3(5, (long)path, oflag, mode);
}
long read(int fd, void* buf, unsigned long n) {
return _a32_syscall3(3, fd, (long)buf, n);
}
long write(int fd, const void* buf, unsigned long n) {
return _a32_syscall3(4, fd, (long)buf, n);
}
void* xbrk(void* addr) { return (void*)_a32_syscall1(45, (long)addr); }
void _a32_main(long* args) { exit(main(args[0], (char**)&args[1])); }
/* linux has uses a non standard ABI when it starts a program */
__asm__(
".text\n"
".global _start\n"
".type _start, %function\n"
"_start:\n"
" mov fp, #0 \n"
" mov lr, #0 \n"
" mov a1, sp \n"
" and sp, a1, #-16 \n"
" bl _a32_main\n");
#else
#error "unsupported architecture"
#endif
#if __test_main__
int main(int argc, char** argv) {
write(1, "hello world\n", 12);
for (int i = 0; i < argc; ++i) {
char* s = argv[i];
int len = 0;
while (s[len] != 0) ++len;
write(1, s, len);
write(1, "\n", 1);
}
return 0;
}
#endif