-
Notifications
You must be signed in to change notification settings - Fork 28
/
usyscall.h
75 lines (70 loc) · 2.54 KB
/
usyscall.h
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
#pragma once
#define SYSCALL_X(x) \
x(read, int, 3, int, fd, void *, buf, unsigned, len) \
x(write, int, 3, int, fd, const void *, buf, unsigned, len) \
x(close, int, 1, int, fd) \
x(dup, int, 1, int, fd) \
x(pipe, int, 1, int *, pipe) \
x(fork, int, 0) \
x(exit, int, 1, int, code) \
x(exec, int, 2, const char *, name, char **, argv) \
#define SC_NR(name, ...) os_syscall_nr_ ## name,
enum syscalls_num {
SYSCALL_X(SC_NR)
};
#undef SC_NR
static inline long os_syscall(int syscall,
unsigned long arg1, unsigned long arg2,
unsigned long arg3, unsigned long arg4,
void *rest) {
long ret;
__asm__ __volatile__(
"int $0x81\n"
: "=a"(ret)
: "a"(syscall), // rax
"b"(arg1), // rbx
"c"(arg2), // rcx
"d"(arg3), // rdx
"S"(arg4), // rsi
"D"(rest) // rdi
:
);
return ret;
}
#define DEFINE0(ret, name) \
static inline ret os_ ## name (void) { \
return (ret) os_syscall(os_syscall_nr_ ## name, 0, 0, 0, 0, (void *) 0); \
}
#define DEFINE1(ret, name, type1, name1) \
static inline ret os_ ## name (type1 name1) { \
return (ret) os_syscall(os_syscall_nr_ ## name, (unsigned long) name1, 0, 0, 0, (void *) 0); \
}
#define DEFINE2(ret, name, type1, name1, type2, name2) \
static inline ret os_ ## name (type1 name1, type2 name2) { \
return (ret) os_syscall(os_syscall_nr_ ## name, (unsigned long) name1, (unsigned long) name2, 0, 0, (void *) 0); \
}
#define DEFINE3(ret, name, type1, name1, type2, name2, type3, name3) \
static inline ret os_ ## name (type1 name1, type2 name2, type3 name3) { \
return (ret) os_syscall(os_syscall_nr_ ## name, (unsigned long) name1, (unsigned long) name2, \
(unsigned long) name3, 0, (void *) 0); \
}
#define DEFINE4(ret, name, type1, name1, type2, name2, type3, name3, type4, name4) \
static inline ret os_ ## name (type1 name1, type2 name2, type3 name3, type4 name4) { \
return (ret) os_syscall(os_syscall_nr_ ## name, (unsigned long) name1, (unsigned long) name2, \
(unsigned long) name3, (unsigned long) name4, (void *) 0); \
}
#define DEFINE5(ret, name, type1, name1, type2, name2, type3, name3, type4, name4, type5, name5) \
static inline ret os_ ## name (type1 name1, type2 name2, type3 name3, type4 name4) { \
return (ret) os_syscall(os_syscall_nr_ ## name, (unsigned long) name1, (unsigned long) name2, \
(unsigned long) name3, (unsigned long) name4, (void *) name5); \
}
#define DEFINE(name, ret, n, ...) \
DEFINE ## n (ret, name, ## __VA_ARGS__)
SYSCALL_X(DEFINE)
#undef DEFINE0
#undef DEFINE1
#undef DEFINE2
#undef DEFINE3
#undef DEFINE4
#undef DEFINE5
#undef DEFINE