-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscall.rs
175 lines (150 loc) · 4.38 KB
/
syscall.rs
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
use super::{Stat, TimeVal};
pub const SYSCALL_OPENAT: usize = 56;
pub const SYSCALL_CLOSE: usize = 57;
pub const SYSCALL_READ: usize = 63;
pub const SYSCALL_WRITE: usize = 64;
pub const SYSCALL_UNLINKAT: usize = 35;
pub const SYSCALL_LINKAT: usize = 37;
pub const SYSCALL_FSTAT: usize = 80;
pub const SYSCALL_EXIT: usize = 93;
pub const SYSCALL_YIELD: usize = 124;
pub const SYSCALL_GETTIMEOFDAY: usize = 169;
pub const SYSCALL_GETPID: usize = 172;
pub const SYSCALL_FORK: usize = 220;
pub const SYSCALL_EXEC: usize = 221;
pub const SYSCALL_WAITPID: usize = 260;
pub const SYSCALL_SET_PRIORITY: usize = 140;
pub const SYSCALL_MUNMAP: usize = 215;
pub const SYSCALL_MMAP: usize = 222;
pub const SYSCALL_SPAWN: usize = 400;
pub const SYSCALL_MAIL_READ: usize = 401;
pub const SYSCALL_MAIL_WRITE: usize = 402;
pub const SYSCALL_DUP: usize = 24;
pub const SYSCALL_PIPE: usize = 59;
pub fn syscall(id: usize, args: [usize; 3]) -> isize {
let mut ret: isize;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (args[0]), "{x11}" (args[1]), "{x12}" (args[2]), "{x17}" (id)
: "memory"
: "volatile"
);
}
ret
}
pub fn syscall6(id: usize, args: [usize; 6]) -> isize {
let mut ret: isize;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (args[0]), "{x11}" (args[1]), "{x12}" (args[2]), "{x13}" (args[3]),
"{x14}" (args[4]), "{x15}" (args[5]), "{x17}" (id)
: "memory"
: "volatile"
);
}
ret
}
pub fn sys_openat(dirfd: usize, path: &str, flags: u32, mode: u32) -> isize {
syscall6(
SYSCALL_OPENAT,
[
dirfd,
path.as_ptr() as usize,
flags as usize,
mode as usize,
0,
0,
],
)
}
pub fn sys_close(fd: usize) -> isize {
syscall(SYSCALL_CLOSE, [fd, 0, 0])
}
pub fn sys_read(fd: usize, buffer: &mut [u8]) -> isize {
syscall(
SYSCALL_READ,
[fd, buffer.as_mut_ptr() as usize, buffer.len()],
)
}
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()])
}
pub fn sys_linkat(
old_dirfd: usize,
old_path: &str,
new_dirfd: usize,
new_path: &str,
flags: usize,
) -> isize {
syscall6(
SYSCALL_LINKAT,
[
old_dirfd,
old_path.as_ptr() as usize,
new_dirfd,
new_path.as_ptr() as usize,
flags,
0,
],
)
}
pub fn sys_unlinkat(dirfd: usize, path: &str, flags: usize) -> isize {
syscall(SYSCALL_UNLINKAT, [dirfd, path.as_ptr() as usize, flags])
}
pub fn sys_fstat(fd: usize, st: &Stat) -> isize {
syscall(SYSCALL_FSTAT, [fd, st as *const _ as usize, 0])
}
pub fn sys_mail_read(buffer: &mut [u8]) -> isize {
syscall(
SYSCALL_MAIL_READ,
[buffer.as_ptr() as usize, buffer.len(), 0],
)
}
pub fn sys_mail_write(pid: usize, buffer: &[u8]) -> isize {
syscall(
SYSCALL_MAIL_WRITE,
[pid, buffer.as_ptr() as usize, buffer.len()],
)
}
pub fn sys_exit(exit_code: i32) -> ! {
syscall(SYSCALL_EXIT, [exit_code as usize, 0, 0]);
panic!("sys_exit never returns!");
}
pub fn sys_yield() -> isize {
syscall(SYSCALL_YIELD, [0, 0, 0])
}
pub fn sys_get_time(time: &TimeVal, tz: usize) -> isize {
syscall(SYSCALL_GETTIMEOFDAY, [time as *const _ as usize, tz, 0])
}
pub fn sys_getpid() -> isize {
syscall(SYSCALL_GETPID, [0, 0, 0])
}
pub fn sys_fork() -> isize {
syscall(SYSCALL_FORK, [0, 0, 0])
}
pub fn sys_exec(path: &str) -> isize {
syscall(SYSCALL_EXEC, [path.as_ptr() as usize, 0, 0])
}
pub fn sys_waitpid(pid: isize, xstatus: *mut i32) -> isize {
syscall(SYSCALL_WAITPID, [pid as usize, xstatus as usize, 0])
}
pub fn sys_set_priority(prio: isize) -> isize {
syscall(SYSCALL_SET_PRIORITY, [prio as usize, 0, 0])
}
pub fn sys_mmap(start: usize, len: usize, prot: usize) -> isize {
syscall(SYSCALL_MMAP, [start, len, prot])
}
pub fn sys_munmap(start: usize, len: usize) -> isize {
syscall(SYSCALL_MUNMAP, [start, len, 0])
}
pub fn sys_spawn(path: &str) -> isize {
syscall(SYSCALL_SPAWN, [path.as_ptr() as usize, 0, 0])
}
pub fn sys_dup(fd: usize) -> isize {
syscall(SYSCALL_DUP, [fd, 0, 0])
}
pub fn sys_pipe(pipe: &mut [usize]) -> isize {
syscall(SYSCALL_PIPE, [pipe.as_mut_ptr() as usize, 0, 0])
}