authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-03-01 14:11:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-03-01 14:11:38-07:00
log9c3d7b628c0b32d7276e4dc33e2e6f306af87472
tree355f70fbea55a26a035ac91ffaf9f58435642dd5
parent1d08ab087e60299a061c39fdb23903a43f12692c

rename syscall.zig to linux.zig


6 files changed, 390 insertions(+), 390 deletions(-)

CMakeLists.txt+1-1
......@@ -139,7 +139,7 @@ set(ZIG_STD_SRC
139139 "${CMAKE_SOURCE_DIR}/std/test_runner_nolibc.zig"
140140 "${CMAKE_SOURCE_DIR}/std/io.zig"
141141 "${CMAKE_SOURCE_DIR}/std/os.zig"
142 "${CMAKE_SOURCE_DIR}/std/syscall.zig"
142 "${CMAKE_SOURCE_DIR}/std/linux.zig"
143143 "${CMAKE_SOURCE_DIR}/std/errno.zig"
144144 "${CMAKE_SOURCE_DIR}/std/rand.zig"
145145 "${CMAKE_SOURCE_DIR}/std/math.zig"
std/bootstrap.zig+3-3
......@@ -1,7 +1,7 @@
11// This file is in a package which has the root source file exposed as "@root".
22
33const root = @import("@root");
4const syscall = @import("syscall.zig");
4const linux = @import("linux.zig");
55
66const want_start_symbol = switch(@compile_var("os")) {
77 linux => true,
......@@ -47,8 +47,8 @@ fn call_main() -> %void {
4747}
4848
4949fn call_main_and_exit() -> unreachable {
50 call_main() %% syscall.exit(1);
51 syscall.exit(0);
50 call_main() %% linux.exit(1);
51 linux.exit(0);
5252}
5353
5454#condition(want_main_symbol)
std/io.zig+13-13
......@@ -1,4 +1,4 @@
1const syscall = @import("syscall.zig");
1const linux = @import("linux.zig");
22const errno = @import("errno.zig");
33const math = @import("math.zig");
44
......@@ -105,7 +105,7 @@ pub struct OutStream {
105105 }
106106
107107 pub fn flush(os: &OutStream) -> %void {
108 const amt_written = syscall.write(os.fd, &os.buffer[0], os.index);
108 const amt_written = linux.write(os.fd, &os.buffer[0], os.index);
109109 os.index = 0;
110110 if (amt_written < 0) {
111111 return switch (-amt_written) {
......@@ -123,12 +123,12 @@ pub struct OutStream {
123123 }
124124
125125 pub fn close(os: &OutStream) -> %void {
126 const closed = close(os.fd);
126 const closed = linux.close(os.fd);
127127 if (closed < 0) {
128128 return switch (-closed) {
129 EIO => error.Io,
130 EBADF => error.BadFd,
131 EINTR => error.SigInterrupt,
129 errno.EIO => error.Io,
130 errno.EBADF => error.BadFd,
131 errno.EINTR => error.SigInterrupt,
132132 else => error.Unexpected,
133133 }
134134 }
......@@ -139,7 +139,7 @@ pub struct InStream {
139139 fd: isize,
140140
141141 pub fn read(is: &InStream, buf: []u8) -> %isize {
142 const amt_read = syscall.read(is.fd, &buf[0], buf.len);
142 const amt_read = linux.read(is.fd, &buf[0], buf.len);
143143 if (amt_read < 0) {
144144 return switch (-amt_read) {
145145 errno.EINVAL => unreachable{},
......@@ -154,12 +154,12 @@ pub struct InStream {
154154 }
155155
156156 pub fn close(is: &InStream) -> %void {
157 const closed = close(is.fd);
157 const closed = linux.close(is.fd);
158158 if (closed < 0) {
159159 return switch (-closed) {
160 EIO => error.Io,
161 EBADF => error.BadFd,
162 EINTR => error.SigInterrupt,
160 errno.EIO => error.Io,
161 errno.EBADF => error.BadFd,
162 errno.EINTR => error.SigInterrupt,
163163 else => error.Unexpected,
164164 }
165165 }
......@@ -168,8 +168,8 @@ pub struct InStream {
168168
169169#attribute("cold")
170170pub fn abort() -> unreachable {
171 syscall.raise(syscall.SIGABRT);
172 syscall.raise(syscall.SIGKILL);
171 linux.raise(linux.SIGABRT);
172 linux.raise(linux.SIGKILL);
173173 while (true) {}
174174}
175175
std/linux.zig created+371
......@@ -0,0 +1,371 @@
1const SYS_read = switch (@compile_var("arch")) {
2 x86_64 => 0,
3 i386 => 3,
4 else => unreachable{},
5};
6const SYS_write = switch (@compile_var("arch")) {
7 x86_64 => 1,
8 i386 => 4,
9 else => unreachable{},
10};
11const SYS_open = switch (@compile_var("arch")) {
12 x86_64 => 2,
13 i386 => 5,
14 else => unreachable{},
15};
16const SYS_close = switch (@compile_var("arch")) {
17 x86_64 => 3,
18 i386 => 6,
19 else => unreachable{},
20};
21const SYS_creat = switch (@compile_var("arch")) {
22 x86_64 => 85,
23 i386 => 8,
24 else => unreachable{},
25};
26const SYS_lseek = switch (@compile_var("arch")) {
27 x86_64 => 8,
28 i386 => 19,
29 else => unreachable{},
30};
31const SYS_mmap = switch (@compile_var("arch")) {
32 x86_64 => 9,
33 i386 => 90,
34 else => unreachable{},
35};
36const SYS_munmap = switch (@compile_var("arch")) {
37 x86_64 => 11,
38 i386 => 91,
39 else => unreachable{},
40};
41const SYS_rt_sigprocmask = switch (@compile_var("arch")) {
42 x86_64 => 14,
43 i386 => 175,
44 else => unreachable{},
45};
46const SYS_exit = switch (@compile_var("arch")) {
47 x86_64 => 60,
48 i386 => 1,
49 else => unreachable{},
50};
51const SYS_kill = switch (@compile_var("arch")) {
52 x86_64 => 62,
53 i386 => 37,
54 else => unreachable{},
55};
56const SYS_getgid = switch (@compile_var("arch")) {
57 x86_64 => 104,
58 i386 => 47,
59 else => unreachable{},
60};
61const SYS_gettid = switch (@compile_var("arch")) {
62 x86_64 => 186,
63 i386 => 224,
64 else => unreachable{},
65};
66const SYS_tkill = switch (@compile_var("arch")) {
67 x86_64 => 200,
68 i386 => 238,
69 else => unreachable{},
70};
71const SYS_tgkill = switch (@compile_var("arch")) {
72 x86_64 => 234,
73 i386 => 270,
74 else => unreachable{},
75};
76const SYS_openat = switch (@compile_var("arch")) {
77 x86_64 => 257,
78 i386 => 295,
79 else => unreachable{},
80};
81const SYS_getrandom = switch (@compile_var("arch")) {
82 x86_64 => 318,
83 i386 => 355,
84 else => unreachable{},
85};
86
87pub const MMAP_PROT_NONE = 0;
88pub const MMAP_PROT_READ = 1;
89pub const MMAP_PROT_WRITE = 2;
90pub const MMAP_PROT_EXEC = 4;
91
92pub const MMAP_MAP_FILE = 0;
93pub const MMAP_MAP_SHARED = 1;
94pub const MMAP_MAP_PRIVATE = 2;
95pub const MMAP_MAP_FIXED = 16;
96pub const MMAP_MAP_ANON = 32;
97
98pub const O_RDONLY = 0x0;
99pub const O_WRONLY = 0x1;
100pub const O_RDWR = 0x2;
101pub const O_CREAT = 0x40;
102pub const O_EXCL = 0x80;
103pub const O_TRUNC = 0x200;
104pub const O_APPEND = 0x400;
105pub const O_SYNC = 0x101000;
106
107pub const SIGHUP = 1;
108pub const SIGINT = 2;
109pub const SIGQUIT = 3;
110pub const SIGILL = 4;
111pub const SIGTRAP = 5;
112pub const SIGABRT = 6;
113pub const SIGIOT = SIGABRT;
114pub const SIGBUS = 7;
115pub const SIGFPE = 8;
116pub const SIGKILL = 9;
117pub const SIGUSR1 = 10;
118pub const SIGSEGV = 11;
119pub const SIGUSR2 = 12;
120pub const SIGPIPE = 13;
121pub const SIGALRM = 14;
122pub const SIGTERM = 15;
123pub const SIGSTKFLT = 16;
124pub const SIGCHLD = 17;
125pub const SIGCONT = 18;
126pub const SIGSTOP = 19;
127pub const SIGTSTP = 20;
128pub const SIGTTIN = 21;
129pub const SIGTTOU = 22;
130pub const SIGURG = 23;
131pub const SIGXCPU = 24;
132pub const SIGXFSZ = 25;
133pub const SIGVTALRM = 26;
134pub const SIGPROF = 27;
135pub const SIGWINCH = 28;
136pub const SIGIO = 29;
137pub const SIGPOLL = 29;
138pub const SIGPWR = 30;
139pub const SIGSYS = 31;
140pub const SIGUNUSED = SIGSYS;
141
142const SIG_BLOCK = 0;
143const SIG_UNBLOCK = 1;
144const SIG_SETMASK = 2;
145
146const syscall0 = switch (@compile_var("arch")) {
147 x86_64 => x86_64_syscall0,
148 i386 => i386_syscall0,
149 else => unreachable{},
150};
151const syscall1 = switch (@compile_var("arch")) {
152 x86_64 => x86_64_syscall1,
153 i386 => i386_syscall1,
154 else => unreachable{},
155};
156const syscall2 = switch (@compile_var("arch")) {
157 x86_64 => x86_64_syscall2,
158 i386 => i386_syscall2,
159 else => unreachable{},
160};
161const syscall3 = switch (@compile_var("arch")) {
162 x86_64 => x86_64_syscall3,
163 i386 => i386_syscall3,
164 else => unreachable{},
165};
166const syscall4 = switch (@compile_var("arch")) {
167 x86_64 => x86_64_syscall4,
168 i386 => i386_syscall4,
169 else => unreachable{},
170};
171const syscall6 = switch (@compile_var("arch")) {
172 x86_64 => x86_64_syscall6,
173 i386 => i386_syscall6,
174 else => unreachable{},
175};
176
177fn x86_64_syscall0(number: isize) -> isize {
178 asm volatile ("syscall"
179 : [ret] "={rax}" (-> isize)
180 : [number] "{rax}" (number)
181 : "rcx", "r11")
182}
183
184fn x86_64_syscall1(number: isize, arg1: isize) -> isize {
185 asm volatile ("syscall"
186 : [ret] "={rax}" (-> isize)
187 : [number] "{rax}" (number),
188 [arg1] "{rdi}" (arg1)
189 : "rcx", "r11")
190}
191
192fn x86_64_syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
193 asm volatile ("syscall"
194 : [ret] "={rax}" (-> isize)
195 : [number] "{rax}" (number),
196 [arg1] "{rdi}" (arg1),
197 [arg2] "{rsi}" (arg2)
198 : "rcx", "r11")
199}
200
201fn x86_64_syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
202 asm volatile ("syscall"
203 : [ret] "={rax}" (-> isize)
204 : [number] "{rax}" (number),
205 [arg1] "{rdi}" (arg1),
206 [arg2] "{rsi}" (arg2),
207 [arg3] "{rdx}" (arg3)
208 : "rcx", "r11")
209}
210
211fn x86_64_syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {
212 asm volatile ("syscall"
213 : [ret] "={rax}" (-> isize)
214 : [number] "{rax}" (number),
215 [arg1] "{rdi}" (arg1),
216 [arg2] "{rsi}" (arg2),
217 [arg3] "{rdx}" (arg3),
218 [arg4] "{r10}" (arg4)
219 : "rcx", "r11")
220}
221
222fn x86_64_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, arg5: isize, arg6: isize) -> isize {
223 asm volatile ("syscall"
224 : [ret] "={rax}" (-> isize)
225 : [number] "{rax}" (number),
226 [arg1] "{rdi}" (arg1),
227 [arg2] "{rsi}" (arg2),
228 [arg3] "{rdx}" (arg3),
229 [arg4] "{r10}" (arg4),
230 [arg5] "{r8}" (arg5),
231 [arg6] "{r9}" (arg6)
232 : "rcx", "r11")
233}
234
235fn i386_syscall0(number: isize) -> isize {
236 asm volatile ("int $0x80"
237 : [ret] "={eax}" (-> isize)
238 : [number] "{eax}" (number))
239}
240
241fn i386_syscall1(number: isize, arg1: isize) -> isize {
242 asm volatile ("int $0x80"
243 : [ret] "={eax}" (-> isize)
244 : [number] "{eax}" (number),
245 [arg1] "{ebx}" (arg1))
246}
247
248fn i386_syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
249 asm volatile ("int $0x80"
250 : [ret] "={eax}" (-> isize)
251 : [number] "{eax}" (number),
252 [arg1] "{ebx}" (arg1),
253 [arg2] "{ecx}" (arg2))
254}
255
256fn i386_syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
257 asm volatile ("int $0x80"
258 : [ret] "={eax}" (-> isize)
259 : [number] "{eax}" (number),
260 [arg1] "{ebx}" (arg1),
261 [arg2] "{ecx}" (arg2),
262 [arg3] "{edx}" (arg3))
263}
264
265fn i386_syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {
266 asm volatile ("int $0x80"
267 : [ret] "={eax}" (-> isize)
268 : [number] "{eax}" (number),
269 [arg1] "{ebx}" (arg1),
270 [arg2] "{ecx}" (arg2),
271 [arg3] "{edx}" (arg3),
272 [arg4] "{esi}" (arg4))
273}
274
275fn i386_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, arg5: isize, arg6: isize) -> isize {
276 asm volatile ("int $0x80"
277 : [ret] "={eax}" (-> isize)
278 : [number] "{eax}" (number),
279 [arg1] "{ebx}" (arg1),
280 [arg2] "{ecx}" (arg2),
281 [arg3] "{edx}" (arg3),
282 [arg4] "{esi}" (arg4),
283 [arg5] "{edi}" (arg5),
284 [arg6] "{ebp}" (arg6))
285}
286
287pub fn mmap(address: ?&u8, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {
288 // TODO ability to cast maybe pointer to isize
289 const addr = if (const unwrapped ?= address) isize(unwrapped) else 0;
290 syscall6(SYS_mmap, addr, length, prot, flags, fd, offset)
291}
292
293pub fn munmap(address: &u8, length: isize) -> isize {
294 syscall2(SYS_munmap, isize(address), length)
295}
296
297pub fn read(fd: isize, buf: &u8, count: isize) -> isize {
298 syscall3(SYS_read, isize(fd), isize(buf), count)
299}
300
301pub fn write(fd: isize, buf: &const u8, count: isize) -> isize {
302 syscall3(SYS_write, isize(fd), isize(buf), count)
303}
304
305pub fn open(path: []u8, flags: isize, perm: isize) -> isize {
306 var buf: [path.len + 1]u8 = undefined;
307 @memcpy(&buf[0], &path[0], path.len);
308 buf[path.len] = 0;
309 syscall3(SYS_open, isize(&buf[0]), flags, perm)
310}
311
312pub fn create(path: []u8, perm: isize) -> isize {
313 var buf: [path.len + 1]u8 = undefined;
314 @memcpy(&buf[0], &path[0], path.len);
315 buf[path.len] = 0;
316 syscall2(SYS_creat, isize(&buf[0]), perm)
317}
318
319pub fn openat(dirfd: isize, path: []u8, flags: isize, mode: isize) -> isize {
320 var buf: [path.len + 1]u8 = undefined;
321 @memcpy(&buf[0], &path[0], path.len);
322 buf[path.len] = 0;
323 syscall4(SYS_openat, dirfd, isize(&buf[0]), flags, mode)
324}
325
326pub fn close(fd: isize) -> isize {
327 syscall1(SYS_close, fd)
328}
329
330pub fn lseek(fd: isize, offset: isize, ref_pos: isize) -> isize {
331 syscall3(SYS_lseek, fd, offset, ref_pos)
332}
333
334pub fn exit(status: i32) -> unreachable {
335 syscall1(SYS_exit, isize(status));
336 unreachable{}
337}
338
339pub fn getrandom(buf: &u8, count: isize, flags: u32) -> isize {
340 syscall3(SYS_getrandom, isize(buf), count, isize(flags))
341}
342
343pub fn kill(pid: i32, sig: i32) -> i32 {
344 i32(syscall2(SYS_kill, pid, sig))
345}
346
347const NSIG = 65;
348const sigset_t = [128]u8;
349const all_mask = []u8 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
350const app_mask = []u8 { 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, };
351
352pub fn raise(sig: i32) -> i32 {
353 var set: sigset_t = undefined;
354 block_app_signals(&set);
355 const tid = i32(syscall0(SYS_gettid));
356 const ret = i32(syscall2(SYS_tkill, tid, sig));
357 restore_signals(&set);
358 return ret;
359}
360
361fn block_all_signals(set: &sigset_t) {
362 syscall4(SYS_rt_sigprocmask, SIG_BLOCK, isize(&all_mask), isize(set), NSIG/8);
363}
364
365fn block_app_signals(set: &sigset_t) {
366 syscall4(SYS_rt_sigprocmask, SIG_BLOCK, isize(&app_mask), isize(set), NSIG/8);
367}
368
369fn restore_signals(set: &sigset_t) {
370 syscall4(SYS_rt_sigprocmask, SIG_SETMASK, isize(set), 0, NSIG/8);
371}
std/os.zig+2-2
......@@ -1,4 +1,4 @@
1const syscall = @import("syscall.zig");
1const linux = @import("linux.zig");
22const errno = @import("errno.zig");
33
44pub error SigInterrupt;
......@@ -7,7 +7,7 @@ pub error Unexpected;
77pub fn get_random_bytes(buf: []u8) -> %void {
88 switch (@compile_var("os")) {
99 linux => {
10 const amt_got = syscall.getrandom(buf.ptr, buf.len, 0);
10 const amt_got = linux.getrandom(buf.ptr, buf.len, 0);
1111 if (amt_got < 0) {
1212 return switch (-amt_got) {
1313 errno.EINVAL => unreachable{},
std/syscall.zig deleted-371
......@@ -1,371 +0,0 @@
1const SYS_read = switch (@compile_var("arch")) {
2 x86_64 => 0,
3 i386 => 3,
4 else => unreachable{},
5};
6const SYS_write = switch (@compile_var("arch")) {
7 x86_64 => 1,
8 i386 => 4,
9 else => unreachable{},
10};
11const SYS_open = switch (@compile_var("arch")) {
12 x86_64 => 2,
13 i386 => 5,
14 else => unreachable{},
15};
16const SYS_close = switch (@compile_var("arch")) {
17 x86_64 => 3,
18 i386 => 6,
19 else => unreachable{},
20};
21const SYS_creat = switch (@compile_var("arch")) {
22 x86_64 => 85,
23 i386 => 8,
24 else => unreachable{},
25};
26const SYS_lseek = switch (@compile_var("arch")) {
27 x86_64 => 8,
28 i386 => 19,
29 else => unreachable{},
30};
31const SYS_mmap = switch (@compile_var("arch")) {
32 x86_64 => 9,
33 i386 => 90,
34 else => unreachable{},
35};
36const SYS_munmap = switch (@compile_var("arch")) {
37 x86_64 => 11,
38 i386 => 91,
39 else => unreachable{},
40};
41const SYS_rt_sigprocmask = switch (@compile_var("arch")) {
42 x86_64 => 14,
43 i386 => 175,
44 else => unreachable{},
45};
46const SYS_exit = switch (@compile_var("arch")) {
47 x86_64 => 60,
48 i386 => 1,
49 else => unreachable{},
50};
51const SYS_kill = switch (@compile_var("arch")) {
52 x86_64 => 62,
53 i386 => 37,
54 else => unreachable{},
55};
56const SYS_getgid = switch (@compile_var("arch")) {
57 x86_64 => 104,
58 i386 => 47,
59 else => unreachable{},
60};
61const SYS_gettid = switch (@compile_var("arch")) {
62 x86_64 => 186,
63 i386 => 224,
64 else => unreachable{},
65};
66const SYS_tkill = switch (@compile_var("arch")) {
67 x86_64 => 200,
68 i386 => 238,
69 else => unreachable{},
70};
71const SYS_tgkill = switch (@compile_var("arch")) {
72 x86_64 => 234,
73 i386 => 270,
74 else => unreachable{},
75};
76const SYS_openat = switch (@compile_var("arch")) {
77 x86_64 => 257,
78 i386 => 295,
79 else => unreachable{},
80};
81const SYS_getrandom = switch (@compile_var("arch")) {
82 x86_64 => 318,
83 i386 => 355,
84 else => unreachable{},
85};
86
87pub const MMAP_PROT_NONE = 0;
88pub const MMAP_PROT_READ = 1;
89pub const MMAP_PROT_WRITE = 2;
90pub const MMAP_PROT_EXEC = 4;
91
92pub const MMAP_MAP_FILE = 0;
93pub const MMAP_MAP_SHARED = 1;
94pub const MMAP_MAP_PRIVATE = 2;
95pub const MMAP_MAP_FIXED = 16;
96pub const MMAP_MAP_ANON = 32;
97
98pub const O_RDONLY = 0x0;
99pub const O_WRONLY = 0x1;
100pub const O_RDWR = 0x2;
101pub const O_CREAT = 0x40;
102pub const O_EXCL = 0x80;
103pub const O_TRUNC = 0x200;
104pub const O_APPEND = 0x400;
105pub const O_SYNC = 0x101000;
106
107pub const SIGHUP = 1;
108pub const SIGINT = 2;
109pub const SIGQUIT = 3;
110pub const SIGILL = 4;
111pub const SIGTRAP = 5;
112pub const SIGABRT = 6;
113pub const SIGIOT = SIGABRT;
114pub const SIGBUS = 7;
115pub const SIGFPE = 8;
116pub const SIGKILL = 9;
117pub const SIGUSR1 = 10;
118pub const SIGSEGV = 11;
119pub const SIGUSR2 = 12;
120pub const SIGPIPE = 13;
121pub const SIGALRM = 14;
122pub const SIGTERM = 15;
123pub const SIGSTKFLT = 16;
124pub const SIGCHLD = 17;
125pub const SIGCONT = 18;
126pub const SIGSTOP = 19;
127pub const SIGTSTP = 20;
128pub const SIGTTIN = 21;
129pub const SIGTTOU = 22;
130pub const SIGURG = 23;
131pub const SIGXCPU = 24;
132pub const SIGXFSZ = 25;
133pub const SIGVTALRM = 26;
134pub const SIGPROF = 27;
135pub const SIGWINCH = 28;
136pub const SIGIO = 29;
137pub const SIGPOLL = 29;
138pub const SIGPWR = 30;
139pub const SIGSYS = 31;
140pub const SIGUNUSED = SIGSYS;
141
142const SIG_BLOCK = 0;
143const SIG_UNBLOCK = 1;
144const SIG_SETMASK = 2;
145
146const syscall0 = switch (@compile_var("arch")) {
147 x86_64 => x86_64_syscall0,
148 i386 => i386_syscall0,
149 else => unreachable{},
150};
151const syscall1 = switch (@compile_var("arch")) {
152 x86_64 => x86_64_syscall1,
153 i386 => i386_syscall1,
154 else => unreachable{},
155};
156const syscall2 = switch (@compile_var("arch")) {
157 x86_64 => x86_64_syscall2,
158 i386 => i386_syscall2,
159 else => unreachable{},
160};
161const syscall3 = switch (@compile_var("arch")) {
162 x86_64 => x86_64_syscall3,
163 i386 => i386_syscall3,
164 else => unreachable{},
165};
166const syscall4 = switch (@compile_var("arch")) {
167 x86_64 => x86_64_syscall4,
168 i386 => i386_syscall4,
169 else => unreachable{},
170};
171const syscall6 = switch (@compile_var("arch")) {
172 x86_64 => x86_64_syscall6,
173 i386 => i386_syscall6,
174 else => unreachable{},
175};
176
177fn x86_64_syscall0(number: isize) -> isize {
178 asm volatile ("syscall"
179 : [ret] "={rax}" (-> isize)
180 : [number] "{rax}" (number)
181 : "rcx", "r11")
182}
183
184fn x86_64_syscall1(number: isize, arg1: isize) -> isize {
185 asm volatile ("syscall"
186 : [ret] "={rax}" (-> isize)
187 : [number] "{rax}" (number),
188 [arg1] "{rdi}" (arg1)
189 : "rcx", "r11")
190}
191
192fn x86_64_syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
193 asm volatile ("syscall"
194 : [ret] "={rax}" (-> isize)
195 : [number] "{rax}" (number),
196 [arg1] "{rdi}" (arg1),
197 [arg2] "{rsi}" (arg2)
198 : "rcx", "r11")
199}
200
201fn x86_64_syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
202 asm volatile ("syscall"
203 : [ret] "={rax}" (-> isize)
204 : [number] "{rax}" (number),
205 [arg1] "{rdi}" (arg1),
206 [arg2] "{rsi}" (arg2),
207 [arg3] "{rdx}" (arg3)
208 : "rcx", "r11")
209}
210
211fn x86_64_syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {
212 asm volatile ("syscall"
213 : [ret] "={rax}" (-> isize)
214 : [number] "{rax}" (number),
215 [arg1] "{rdi}" (arg1),
216 [arg2] "{rsi}" (arg2),
217 [arg3] "{rdx}" (arg3),
218 [arg4] "{r10}" (arg4)
219 : "rcx", "r11")
220}
221
222fn x86_64_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, arg5: isize, arg6: isize) -> isize {
223 asm volatile ("syscall"
224 : [ret] "={rax}" (-> isize)
225 : [number] "{rax}" (number),
226 [arg1] "{rdi}" (arg1),
227 [arg2] "{rsi}" (arg2),
228 [arg3] "{rdx}" (arg3),
229 [arg4] "{r10}" (arg4),
230 [arg5] "{r8}" (arg5),
231 [arg6] "{r9}" (arg6)
232 : "rcx", "r11")
233}
234
235fn i386_syscall0(number: isize) -> isize {
236 asm volatile ("int $0x80"
237 : [ret] "={eax}" (-> isize)
238 : [number] "{eax}" (number))
239}
240
241fn i386_syscall1(number: isize, arg1: isize) -> isize {
242 asm volatile ("int $0x80"
243 : [ret] "={eax}" (-> isize)
244 : [number] "{eax}" (number),
245 [arg1] "{ebx}" (arg1))
246}
247
248fn i386_syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
249 asm volatile ("int $0x80"
250 : [ret] "={eax}" (-> isize)
251 : [number] "{eax}" (number),
252 [arg1] "{ebx}" (arg1),
253 [arg2] "{ecx}" (arg2))
254}
255
256fn i386_syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
257 asm volatile ("int $0x80"
258 : [ret] "={eax}" (-> isize)
259 : [number] "{eax}" (number),
260 [arg1] "{ebx}" (arg1),
261 [arg2] "{ecx}" (arg2),
262 [arg3] "{edx}" (arg3))
263}
264
265fn i386_syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {
266 asm volatile ("int $0x80"
267 : [ret] "={eax}" (-> isize)
268 : [number] "{eax}" (number),
269 [arg1] "{ebx}" (arg1),
270 [arg2] "{ecx}" (arg2),
271 [arg3] "{edx}" (arg3),
272 [arg4] "{esi}" (arg4))
273}
274
275fn i386_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, arg5: isize, arg6: isize) -> isize {
276 asm volatile ("int $0x80"
277 : [ret] "={eax}" (-> isize)
278 : [number] "{eax}" (number),
279 [arg1] "{ebx}" (arg1),
280 [arg2] "{ecx}" (arg2),
281 [arg3] "{edx}" (arg3),
282 [arg4] "{esi}" (arg4),
283 [arg5] "{edi}" (arg5),
284 [arg6] "{ebp}" (arg6))
285}
286
287pub fn mmap(address: ?&u8, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {
288 // TODO ability to cast maybe pointer to isize
289 const addr = if (const unwrapped ?= address) isize(unwrapped) else 0;
290 syscall6(SYS_mmap, addr, length, prot, flags, fd, offset)
291}
292
293pub fn munmap(address: &u8, length: isize) -> isize {
294 syscall2(SYS_munmap, isize(address), length)
295}
296
297pub fn read(fd: isize, buf: &u8, count: isize) -> isize {
298 syscall3(SYS_read, isize(fd), isize(buf), count)
299}
300
301pub fn write(fd: isize, buf: &const u8, count: isize) -> isize {
302 syscall3(SYS_write, isize(fd), isize(buf), count)
303}
304
305pub fn open(path: []u8, flags: isize, perm: isize) -> isize {
306 var buf: [path.len + 1]u8 = undefined;
307 @memcpy(&buf[0], &path[0], path.len);
308 buf[path.len] = 0;
309 syscall3(SYS_open, isize(&buf[0]), flags, perm)
310}
311
312pub fn create(path: []u8, perm: isize) -> isize {
313 var buf: [path.len + 1]u8 = undefined;
314 @memcpy(&buf[0], &path[0], path.len);
315 buf[path.len] = 0;
316 syscall2(SYS_creat, isize(&buf[0]), perm)
317}
318
319pub fn openat(dirfd: isize, path: []u8, flags: isize, mode: isize) -> isize {
320 var buf: [path.len + 1]u8 = undefined;
321 @memcpy(&buf[0], &path[0], path.len);
322 buf[path.len] = 0;
323 syscall4(SYS_openat, dirfd, isize(&buf[0]), flags, mode)
324}
325
326pub fn close(fd: isize) -> isize {
327 syscall1(SYS_close, fd)
328}
329
330pub fn lseek(fd: isize, offset: isize, ref_pos: isize) -> isize {
331 syscall3(SYS_lseek, fd, offset, ref_pos)
332}
333
334pub fn exit(status: i32) -> unreachable {
335 syscall1(SYS_exit, isize(status));
336 unreachable{}
337}
338
339pub fn getrandom(buf: &u8, count: isize, flags: u32) -> isize {
340 syscall3(SYS_getrandom, isize(buf), count, isize(flags))
341}
342
343pub fn kill(pid: i32, sig: i32) -> i32 {
344 i32(syscall2(SYS_kill, pid, sig))
345}
346
347const NSIG = 65;
348const sigset_t = [128]u8;
349const all_mask = []u8 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
350const app_mask = []u8 { 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, };
351
352pub fn raise(sig: i32) -> i32 {
353 var set: sigset_t = undefined;
354 block_app_signals(&set);
355 const tid = i32(syscall0(SYS_gettid));
356 const ret = i32(syscall2(SYS_tkill, tid, sig));
357 restore_signals(&set);
358 return ret;
359}
360
361fn block_all_signals(set: &sigset_t) {
362 syscall4(SYS_rt_sigprocmask, SIG_BLOCK, isize(&all_mask), isize(set), NSIG/8);
363}
364
365fn block_app_signals(set: &sigset_t) {
366 syscall4(SYS_rt_sigprocmask, SIG_BLOCK, isize(&app_mask), isize(set), NSIG/8);
367}
368
369fn restore_signals(set: &sigset_t) {
370 syscall4(SYS_rt_sigprocmask, SIG_SETMASK, isize(set), 0, NSIG/8);
371}