-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
linux.zig
5599 lines (4813 loc) · 158 KB
/
linux.zig
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! This file provides the system interface functions for Linux matching those
//! that are provided by libc, whether or not libc is linked. The following
//! abstractions are made:
//! * Work around kernel bugs and limitations. For example, see sendmmsg.
//! * Implement all the syscalls in the same way that libc functions will
//! provide `rename` when only the `renameat` syscall exists.
//! * Does not support POSIX thread cancellation.
const std = @import("../std.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const maxInt = std.math.maxInt;
const elf = std.elf;
const vdso = @import("linux/vdso.zig");
const dl = @import("../dynamic_library.zig");
const native_arch = builtin.cpu.arch;
const native_endian = native_arch.endian();
const is_mips = native_arch.isMIPS();
const is_ppc = native_arch.isPPC();
const is_ppc64 = native_arch.isPPC64();
const is_sparc = native_arch.isSPARC();
const iovec = std.os.iovec;
const iovec_const = std.os.iovec_const;
test {
if (builtin.os.tag == .linux) {
_ = @import("linux/test.zig");
}
}
const syscall_bits = switch (native_arch) {
.thumb => @import("linux/thumb.zig"),
else => arch_bits,
};
const arch_bits = switch (native_arch) {
.i386 => @import("linux/i386.zig"),
.x86_64 => @import("linux/x86_64.zig"),
.aarch64 => @import("linux/arm64.zig"),
.arm, .thumb => @import("linux/arm-eabi.zig"),
.riscv64 => @import("linux/riscv64.zig"),
.sparc64 => @import("linux/sparc64.zig"),
.mips, .mipsel => @import("linux/mips.zig"),
.powerpc => @import("linux/powerpc.zig"),
.powerpc64, .powerpc64le => @import("linux/powerpc64.zig"),
else => struct {},
};
pub const syscall0 = syscall_bits.syscall0;
pub const syscall1 = syscall_bits.syscall1;
pub const syscall2 = syscall_bits.syscall2;
pub const syscall3 = syscall_bits.syscall3;
pub const syscall4 = syscall_bits.syscall4;
pub const syscall5 = syscall_bits.syscall5;
pub const syscall6 = syscall_bits.syscall6;
pub const syscall7 = syscall_bits.syscall7;
pub const restore = syscall_bits.restore;
pub const restore_rt = syscall_bits.restore_rt;
pub const socketcall = syscall_bits.socketcall;
pub const syscall_pipe = syscall_bits.syscall_pipe;
pub const syscall_fork = syscall_bits.syscall_fork;
pub const ARCH = arch_bits.ARCH;
pub const Elf_Symndx = arch_bits.Elf_Symndx;
pub const F = arch_bits.F;
pub const Flock = arch_bits.Flock;
pub const HWCAP = arch_bits.HWCAP;
pub const LOCK = arch_bits.LOCK;
pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT;
pub const REG = arch_bits.REG;
pub const SC = arch_bits.SC;
pub const Stat = arch_bits.Stat;
pub const VDSO = arch_bits.VDSO;
pub const blkcnt_t = arch_bits.blkcnt_t;
pub const blksize_t = arch_bits.blksize_t;
pub const clone = arch_bits.clone;
pub const dev_t = arch_bits.dev_t;
pub const ino_t = arch_bits.ino_t;
pub const mcontext_t = arch_bits.mcontext_t;
pub const mode_t = arch_bits.mode_t;
pub const msghdr = arch_bits.msghdr;
pub const msghdr_const = arch_bits.msghdr_const;
pub const nlink_t = arch_bits.nlink_t;
pub const off_t = arch_bits.off_t;
pub const time_t = arch_bits.time_t;
pub const timeval = arch_bits.timeval;
pub const timezone = arch_bits.timezone;
pub const ucontext_t = arch_bits.ucontext_t;
pub const user_desc = arch_bits.user_desc;
pub const tls = @import("linux/tls.zig");
pub const pie = @import("linux/start_pie.zig");
pub const BPF = @import("linux/bpf.zig");
pub const IOCTL = @import("linux/ioctl.zig");
pub const SECCOMP = @import("linux/seccomp.zig");
pub const syscalls = @import("linux/syscalls.zig");
pub const SYS = switch (@import("builtin").cpu.arch) {
.i386 => syscalls.X86,
.x86_64 => syscalls.X64,
.aarch64 => syscalls.Arm64,
.arm, .thumb => syscalls.Arm,
.riscv64 => syscalls.RiscV64,
.sparc64 => syscalls.Sparc64,
.mips, .mipsel => syscalls.Mips,
.powerpc => syscalls.PowerPC,
.powerpc64, .powerpc64le => syscalls.PowerPC64,
else => @compileError("The Zig Standard Library is missing syscall definitions for the target CPU architecture"),
};
pub const MAP = struct {
pub usingnamespace arch_bits.MAP;
/// Share changes
pub const SHARED = 0x01;
/// Changes are private
pub const PRIVATE = 0x02;
/// share + validate extension flags
pub const SHARED_VALIDATE = 0x03;
/// Mask for type of mapping
pub const TYPE = 0x0f;
/// Interpret addr exactly
pub const FIXED = 0x10;
/// don't use a file
pub const ANONYMOUS = if (is_mips) 0x800 else 0x20;
// MAP_ 0x0100 - 0x4000 flags are per architecture
/// populate (prefault) pagetables
pub const POPULATE = if (is_mips) 0x10000 else 0x8000;
/// do not block on IO
pub const NONBLOCK = if (is_mips) 0x20000 else 0x10000;
/// give out an address that is best suited for process/thread stacks
pub const STACK = if (is_mips) 0x40000 else 0x20000;
/// create a huge page mapping
pub const HUGETLB = if (is_mips) 0x80000 else 0x40000;
/// perform synchronous page faults for the mapping
pub const SYNC = 0x80000;
/// MAP_FIXED which doesn't unmap underlying mapping
pub const FIXED_NOREPLACE = 0x100000;
/// For anonymous mmap, memory could be uninitialized
pub const UNINITIALIZED = 0x4000000;
};
pub const O = struct {
pub usingnamespace arch_bits.O;
pub const RDONLY = 0o0;
pub const WRONLY = 0o1;
pub const RDWR = 0o2;
};
pub usingnamespace @import("linux/io_uring.zig");
/// Set by startup code, used by `getauxval`.
pub var elf_aux_maybe: ?[*]std.elf.Auxv = null;
/// See `std.elf` for the constants.
pub fn getauxval(index: usize) usize {
const auxv = elf_aux_maybe orelse return 0;
var i: usize = 0;
while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
if (auxv[i].a_type == index)
return auxv[i].a_un.a_val;
}
return 0;
}
// Some architectures (and some syscalls) require 64bit parameters to be passed
// in a even-aligned register pair.
const require_aligned_register_pair =
builtin.cpu.arch.isPPC() or
builtin.cpu.arch.isMIPS() or
builtin.cpu.arch.isARM() or
builtin.cpu.arch.isThumb();
// Split a 64bit value into a {LSB,MSB} pair.
// The LE/BE variants specify the endianness to assume.
fn splitValueLE64(val: i64) [2]u32 {
const u = @bitCast(u64, val);
return [2]u32{
@truncate(u32, u),
@truncate(u32, u >> 32),
};
}
fn splitValueBE64(val: i64) [2]u32 {
const u = @bitCast(u64, val);
return [2]u32{
@truncate(u32, u >> 32),
@truncate(u32, u),
};
}
fn splitValue64(val: i64) [2]u32 {
const u = @bitCast(u64, val);
switch (native_endian) {
.Little => return [2]u32{
@truncate(u32, u),
@truncate(u32, u >> 32),
},
.Big => return [2]u32{
@truncate(u32, u >> 32),
@truncate(u32, u),
},
}
}
/// Get the errno from a syscall return value, or 0 for no error.
pub fn getErrno(r: usize) E {
const signed_r = @bitCast(isize, r);
const int = if (signed_r > -4096 and signed_r < 0) -signed_r else 0;
return @intToEnum(E, int);
}
pub fn dup(old: i32) usize {
return syscall1(.dup, @bitCast(usize, @as(isize, old)));
}
pub fn dup2(old: i32, new: i32) usize {
if (@hasField(SYS, "dup2")) {
return syscall2(.dup2, @bitCast(usize, @as(isize, old)), @bitCast(usize, @as(isize, new)));
} else {
if (old == new) {
if (std.debug.runtime_safety) {
const rc = syscall2(.fcntl, @bitCast(usize, @as(isize, old)), F.GETFD);
if (@bitCast(isize, rc) < 0) return rc;
}
return @intCast(usize, old);
} else {
return syscall3(.dup3, @bitCast(usize, @as(isize, old)), @bitCast(usize, @as(isize, new)), 0);
}
}
}
pub fn dup3(old: i32, new: i32, flags: u32) usize {
return syscall3(.dup3, @bitCast(usize, @as(isize, old)), @bitCast(usize, @as(isize, new)), flags);
}
pub fn chdir(path: [*:0]const u8) usize {
return syscall1(.chdir, @ptrToInt(path));
}
pub fn fchdir(fd: fd_t) usize {
return syscall1(.fchdir, @bitCast(usize, @as(isize, fd)));
}
pub fn chroot(path: [*:0]const u8) usize {
return syscall1(.chroot, @ptrToInt(path));
}
pub fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) usize {
return syscall3(.execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp));
}
pub fn fork() usize {
if (comptime native_arch.isSPARC()) {
return syscall_fork();
} else if (@hasField(SYS, "fork")) {
return syscall0(.fork);
} else {
return syscall2(.clone, SIG.CHLD, 0);
}
}
/// This must be inline, and inline call the syscall function, because if the
/// child does a return it will clobber the parent's stack.
/// It is advised to avoid this function and use clone instead, because
/// the compiler is not aware of how vfork affects control flow and you may
/// see different results in optimized builds.
pub inline fn vfork() usize {
return @call(.{ .modifier = .always_inline }, syscall0, .{.vfork});
}
pub fn futimens(fd: i32, times: *const [2]timespec) usize {
return utimensat(fd, null, times, 0);
}
pub fn utimensat(dirfd: i32, path: ?[*:0]const u8, times: *const [2]timespec, flags: u32) usize {
return syscall4(.utimensat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(times), flags);
}
pub fn fallocate(fd: i32, mode: i32, offset: i64, length: i64) usize {
if (usize_bits < 64) {
const offset_halves = splitValue64(offset);
const length_halves = splitValue64(length);
return syscall6(
.fallocate,
@bitCast(usize, @as(isize, fd)),
@bitCast(usize, @as(isize, mode)),
offset_halves[0],
offset_halves[1],
length_halves[0],
length_halves[1],
);
} else {
return syscall4(
.fallocate,
@bitCast(usize, @as(isize, fd)),
@bitCast(usize, @as(isize, mode)),
@bitCast(u64, offset),
@bitCast(u64, length),
);
}
}
pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*const timespec) usize {
return syscall4(.futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout));
}
pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize {
return syscall3(.futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val));
}
pub fn getcwd(buf: [*]u8, size: usize) usize {
return syscall2(.getcwd, @ptrToInt(buf), size);
}
pub fn getdents(fd: i32, dirp: [*]u8, len: usize) usize {
return syscall3(
.getdents,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(dirp),
std.math.min(len, maxInt(c_int)),
);
}
pub fn getdents64(fd: i32, dirp: [*]u8, len: usize) usize {
return syscall3(
.getdents64,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(dirp),
std.math.min(len, maxInt(c_int)),
);
}
pub fn inotify_init1(flags: u32) usize {
return syscall1(.inotify_init1, flags);
}
pub fn inotify_add_watch(fd: i32, pathname: [*:0]const u8, mask: u32) usize {
return syscall3(.inotify_add_watch, @bitCast(usize, @as(isize, fd)), @ptrToInt(pathname), mask);
}
pub fn inotify_rm_watch(fd: i32, wd: i32) usize {
return syscall2(.inotify_rm_watch, @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, wd)));
}
pub fn readlink(noalias path: [*:0]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
if (@hasField(SYS, "readlink")) {
return syscall3(.readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
} else {
return syscall4(.readlinkat, @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
}
}
pub fn readlinkat(dirfd: i32, noalias path: [*:0]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
return syscall4(.readlinkat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
}
pub fn mkdir(path: [*:0]const u8, mode: u32) usize {
if (@hasField(SYS, "mkdir")) {
return syscall2(.mkdir, @ptrToInt(path), mode);
} else {
return syscall3(.mkdirat, @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(path), mode);
}
}
pub fn mkdirat(dirfd: i32, path: [*:0]const u8, mode: u32) usize {
return syscall3(.mkdirat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), mode);
}
pub fn mknod(path: [*:0]const u8, mode: u32, dev: u32) usize {
if (@hasField(SYS, "mknod")) {
return syscall3(.mknod, @ptrToInt(path), mode, dev);
} else {
return mknodat(AT.FDCWD, path, mode, dev);
}
}
pub fn mknodat(dirfd: i32, path: [*:0]const u8, mode: u32, dev: u32) usize {
return syscall4(.mknodat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), mode, dev);
}
pub fn mount(special: [*:0]const u8, dir: [*:0]const u8, fstype: ?[*:0]const u8, flags: u32, data: usize) usize {
return syscall5(.mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data);
}
pub fn umount(special: [*:0]const u8) usize {
return syscall2(.umount2, @ptrToInt(special), 0);
}
pub fn umount2(special: [*:0]const u8, flags: u32) usize {
return syscall2(.umount2, @ptrToInt(special), flags);
}
pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: i64) usize {
if (@hasField(SYS, "mmap2")) {
// Make sure the offset is also specified in multiples of page size
if ((offset & (MMAP2_UNIT - 1)) != 0)
return @bitCast(usize, -@as(isize, @enumToInt(E.INVAL)));
return syscall6(
.mmap2,
@ptrToInt(address),
length,
prot,
flags,
@bitCast(usize, @as(isize, fd)),
@truncate(usize, @bitCast(u64, offset) / MMAP2_UNIT),
);
} else {
return syscall6(
.mmap,
@ptrToInt(address),
length,
prot,
flags,
@bitCast(usize, @as(isize, fd)),
@bitCast(u64, offset),
);
}
}
pub fn mprotect(address: [*]const u8, length: usize, protection: usize) usize {
return syscall3(.mprotect, @ptrToInt(address), length, protection);
}
pub const MSF = struct {
pub const ASYNC = 1;
pub const INVALIDATE = 2;
pub const SYNC = 4;
};
pub fn msync(address: [*]const u8, length: usize, flags: i32) usize {
return syscall3(.msync, @ptrToInt(address), length, @bitCast(u32, flags));
}
pub fn munmap(address: [*]const u8, length: usize) usize {
return syscall2(.munmap, @ptrToInt(address), length);
}
pub fn poll(fds: [*]pollfd, n: nfds_t, timeout: i32) usize {
if (@hasField(SYS, "poll")) {
return syscall3(.poll, @ptrToInt(fds), n, @bitCast(u32, timeout));
} else {
return syscall5(
.ppoll,
@ptrToInt(fds),
n,
@ptrToInt(if (timeout >= 0)
×pec{
.tv_sec = @divTrunc(timeout, 1000),
.tv_nsec = @rem(timeout, 1000) * 1000000,
}
else
null),
0,
NSIG / 8,
);
}
}
pub fn ppoll(fds: [*]pollfd, n: nfds_t, timeout: ?*timespec, sigmask: ?*const sigset_t) usize {
return syscall5(.ppoll, @ptrToInt(fds), n, @ptrToInt(timeout), @ptrToInt(sigmask), NSIG / 8);
}
pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
return syscall3(.read, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count);
}
pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: i64) usize {
const offset_u = @bitCast(u64, offset);
return syscall5(
.preadv,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(iov),
count,
// Kernel expects the offset is splitted into largest natural word-size.
// See following link for detail:
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=601cc11d054ae4b5e9b5babec3d8e4667a2cb9b5
@truncate(usize, offset_u),
if (usize_bits < 64) @truncate(usize, offset_u >> 32) else 0,
);
}
pub fn preadv2(fd: i32, iov: [*]const iovec, count: usize, offset: i64, flags: kernel_rwf) usize {
const offset_u = @bitCast(u64, offset);
return syscall6(
.preadv2,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(iov),
count,
// See comments in preadv
@truncate(usize, offset_u),
if (usize_bits < 64) @truncate(usize, offset_u >> 32) else 0,
flags,
);
}
pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize {
return syscall3(.readv, @bitCast(usize, @as(isize, fd)), @ptrToInt(iov), count);
}
pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize {
return syscall3(.writev, @bitCast(usize, @as(isize, fd)), @ptrToInt(iov), count);
}
pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: i64) usize {
const offset_u = @bitCast(u64, offset);
return syscall5(
.pwritev,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(iov),
count,
// See comments in preadv
@truncate(usize, offset_u),
if (usize_bits < 64) @truncate(usize, offset_u >> 32) else 0,
);
}
pub fn pwritev2(fd: i32, iov: [*]const iovec_const, count: usize, offset: i64, flags: kernel_rwf) usize {
const offset_u = @bitCast(u64, offset);
return syscall6(
.pwritev2,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(iov),
count,
// See comments in preadv
@truncate(usize, offset_u),
if (usize_bits < 64) @truncate(usize, offset_u >> 32) else 0,
flags,
);
}
pub fn rmdir(path: [*:0]const u8) usize {
if (@hasField(SYS, "rmdir")) {
return syscall1(.rmdir, @ptrToInt(path));
} else {
return syscall3(.unlinkat, @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(path), AT.REMOVEDIR);
}
}
pub fn symlink(existing: [*:0]const u8, new: [*:0]const u8) usize {
if (@hasField(SYS, "symlink")) {
return syscall2(.symlink, @ptrToInt(existing), @ptrToInt(new));
} else {
return syscall3(.symlinkat, @ptrToInt(existing), @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(new));
}
}
pub fn symlinkat(existing: [*:0]const u8, newfd: i32, newpath: [*:0]const u8) usize {
return syscall3(.symlinkat, @ptrToInt(existing), @bitCast(usize, @as(isize, newfd)), @ptrToInt(newpath));
}
pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: i64) usize {
if (@hasField(SYS, "pread64") and usize_bits < 64) {
const offset_halves = splitValue64(offset);
if (require_aligned_register_pair) {
return syscall6(
.pread64,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(buf),
count,
0,
offset_halves[0],
offset_halves[1],
);
} else {
return syscall5(
.pread64,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(buf),
count,
offset_halves[0],
offset_halves[1],
);
}
} else {
// Some architectures (eg. 64bit SPARC) pread is called pread64.
const syscall_number = if (!@hasField(SYS, "pread") and @hasField(SYS, "pread64"))
.pread64
else
.pread;
return syscall4(
syscall_number,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(buf),
count,
@bitCast(u64, offset),
);
}
}
pub fn access(path: [*:0]const u8, mode: u32) usize {
if (@hasField(SYS, "access")) {
return syscall2(.access, @ptrToInt(path), mode);
} else {
return syscall4(.faccessat, @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(path), mode, 0);
}
}
pub fn faccessat(dirfd: i32, path: [*:0]const u8, mode: u32, flags: u32) usize {
return syscall4(.faccessat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), mode, flags);
}
pub fn pipe(fd: *[2]i32) usize {
if (comptime (native_arch.isMIPS() or native_arch.isSPARC())) {
return syscall_pipe(fd);
} else if (@hasField(SYS, "pipe")) {
return syscall1(.pipe, @ptrToInt(fd));
} else {
return syscall2(.pipe2, @ptrToInt(fd), 0);
}
}
pub fn pipe2(fd: *[2]i32, flags: u32) usize {
return syscall2(.pipe2, @ptrToInt(fd), flags);
}
pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
return syscall3(.write, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count);
}
pub fn ftruncate(fd: i32, length: i64) usize {
if (@hasField(SYS, "ftruncate64") and usize_bits < 64) {
const length_halves = splitValue64(length);
if (require_aligned_register_pair) {
return syscall4(
.ftruncate64,
@bitCast(usize, @as(isize, fd)),
0,
length_halves[0],
length_halves[1],
);
} else {
return syscall3(
.ftruncate64,
@bitCast(usize, @as(isize, fd)),
length_halves[0],
length_halves[1],
);
}
} else {
return syscall2(
.ftruncate,
@bitCast(usize, @as(isize, fd)),
@bitCast(usize, length),
);
}
}
pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: i64) usize {
if (@hasField(SYS, "pwrite64") and usize_bits < 64) {
const offset_halves = splitValue64(offset);
if (require_aligned_register_pair) {
return syscall6(
.pwrite64,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(buf),
count,
0,
offset_halves[0],
offset_halves[1],
);
} else {
return syscall5(
.pwrite64,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(buf),
count,
offset_halves[0],
offset_halves[1],
);
}
} else {
// Some architectures (eg. 64bit SPARC) pwrite is called pwrite64.
const syscall_number = if (!@hasField(SYS, "pwrite") and @hasField(SYS, "pwrite64"))
.pwrite64
else
.pwrite;
return syscall4(
syscall_number,
@bitCast(usize, @as(isize, fd)),
@ptrToInt(buf),
count,
@bitCast(u64, offset),
);
}
}
pub fn rename(old: [*:0]const u8, new: [*:0]const u8) usize {
if (@hasField(SYS, "rename")) {
return syscall2(.rename, @ptrToInt(old), @ptrToInt(new));
} else if (@hasField(SYS, "renameat")) {
return syscall4(.renameat, @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(old), @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(new));
} else {
return syscall5(.renameat2, @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(old), @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(new), 0);
}
}
pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize {
if (@hasField(SYS, "renameat")) {
return syscall4(
.renameat,
@bitCast(usize, @as(isize, oldfd)),
@ptrToInt(oldpath),
@bitCast(usize, @as(isize, newfd)),
@ptrToInt(newpath),
);
} else {
return syscall5(
.renameat2,
@bitCast(usize, @as(isize, oldfd)),
@ptrToInt(oldpath),
@bitCast(usize, @as(isize, newfd)),
@ptrToInt(newpath),
0,
);
}
}
pub fn renameat2(oldfd: i32, oldpath: [*:0]const u8, newfd: i32, newpath: [*:0]const u8, flags: u32) usize {
return syscall5(
.renameat2,
@bitCast(usize, @as(isize, oldfd)),
@ptrToInt(oldpath),
@bitCast(usize, @as(isize, newfd)),
@ptrToInt(newpath),
flags,
);
}
pub fn open(path: [*:0]const u8, flags: u32, perm: mode_t) usize {
if (@hasField(SYS, "open")) {
return syscall3(.open, @ptrToInt(path), flags, perm);
} else {
return syscall4(
.openat,
@bitCast(usize, @as(isize, AT.FDCWD)),
@ptrToInt(path),
flags,
perm,
);
}
}
pub fn create(path: [*:0]const u8, perm: mode_t) usize {
return syscall2(.creat, @ptrToInt(path), perm);
}
pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, mode: mode_t) usize {
// dirfd could be negative, for example AT.FDCWD is -100
return syscall4(.openat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags, mode);
}
/// See also `clone` (from the arch-specific include)
pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize {
return syscall5(.clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls);
}
/// See also `clone` (from the arch-specific include)
pub fn clone2(flags: u32, child_stack_ptr: usize) usize {
return syscall2(.clone, flags, child_stack_ptr);
}
pub fn close(fd: i32) usize {
return syscall1(.close, @bitCast(usize, @as(isize, fd)));
}
pub fn fchmod(fd: i32, mode: mode_t) usize {
return syscall2(.fchmod, @bitCast(usize, @as(isize, fd)), mode);
}
pub fn fchown(fd: i32, owner: uid_t, group: gid_t) usize {
if (@hasField(SYS, "fchown32")) {
return syscall3(.fchown32, @bitCast(usize, @as(isize, fd)), owner, group);
} else {
return syscall3(.fchown, @bitCast(usize, @as(isize, fd)), owner, group);
}
}
/// Can only be called on 32 bit systems. For 64 bit see `lseek`.
pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize {
// NOTE: The offset parameter splitting is independent from the target
// endianness.
return syscall5(
._llseek,
@bitCast(usize, @as(isize, fd)),
@truncate(usize, offset >> 32),
@truncate(usize, offset),
@ptrToInt(result),
whence,
);
}
/// Can only be called on 64 bit systems. For 32 bit see `llseek`.
pub fn lseek(fd: i32, offset: i64, whence: usize) usize {
return syscall3(.lseek, @bitCast(usize, @as(isize, fd)), @bitCast(usize, offset), whence);
}
pub fn exit(status: i32) noreturn {
_ = syscall1(.exit, @bitCast(usize, @as(isize, status)));
unreachable;
}
pub fn exit_group(status: i32) noreturn {
_ = syscall1(.exit_group, @bitCast(usize, @as(isize, status)));
unreachable;
}
pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
return syscall3(.getrandom, @ptrToInt(buf), count, flags);
}
pub fn kill(pid: pid_t, sig: i32) usize {
return syscall2(.kill, @bitCast(usize, @as(isize, pid)), @bitCast(usize, @as(isize, sig)));
}
pub fn tkill(tid: pid_t, sig: i32) usize {
return syscall2(.tkill, @bitCast(usize, @as(isize, tid)), @bitCast(usize, @as(isize, sig)));
}
pub fn tgkill(tgid: pid_t, tid: pid_t, sig: i32) usize {
return syscall3(.tgkill, @bitCast(usize, @as(isize, tgid)), @bitCast(usize, @as(isize, tid)), @bitCast(usize, @as(isize, sig)));
}
pub fn link(oldpath: [*:0]const u8, newpath: [*:0]const u8, flags: i32) usize {
if (@hasField(SYS, "link")) {
return syscall3(
.link,
@ptrToInt(oldpath),
@ptrToInt(newpath),
@bitCast(usize, @as(isize, flags)),
);
} else {
return syscall5(
.linkat,
@bitCast(usize, @as(isize, AT.FDCWD)),
@ptrToInt(oldpath),
@bitCast(usize, @as(isize, AT.FDCWD)),
@ptrToInt(newpath),
@bitCast(usize, @as(isize, flags)),
);
}
}
pub fn linkat(oldfd: fd_t, oldpath: [*:0]const u8, newfd: fd_t, newpath: [*:0]const u8, flags: i32) usize {
return syscall5(
.linkat,
@bitCast(usize, @as(isize, oldfd)),
@ptrToInt(oldpath),
@bitCast(usize, @as(isize, newfd)),
@ptrToInt(newpath),
@bitCast(usize, @as(isize, flags)),
);
}
pub fn unlink(path: [*:0]const u8) usize {
if (@hasField(SYS, "unlink")) {
return syscall1(.unlink, @ptrToInt(path));
} else {
return syscall3(.unlinkat, @bitCast(usize, @as(isize, AT.FDCWD)), @ptrToInt(path), 0);
}
}
pub fn unlinkat(dirfd: i32, path: [*:0]const u8, flags: u32) usize {
return syscall3(.unlinkat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags);
}
pub fn waitpid(pid: pid_t, status: *u32, flags: u32) usize {
return syscall4(.wait4, @bitCast(usize, @as(isize, pid)), @ptrToInt(status), flags, 0);
}
pub fn waitid(id_type: P, id: i32, infop: *siginfo_t, flags: u32) usize {
return syscall5(.waitid, @enumToInt(id_type), @bitCast(usize, @as(isize, id)), @ptrToInt(infop), flags, 0);
}
pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) usize {
return syscall3(.fcntl, @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, cmd)), arg);
}
pub fn flock(fd: fd_t, operation: i32) usize {
return syscall2(.flock, @bitCast(usize, @as(isize, fd)), @bitCast(usize, @as(isize, operation)));
}
var vdso_clock_gettime = if (builtin.zig_backend == .stage1)
@ptrCast(?*const anyopaque, init_vdso_clock_gettime)
else
@ptrCast(?*const anyopaque, &init_vdso_clock_gettime);
// We must follow the C calling convention when we call into the VDSO
const vdso_clock_gettime_ty = if (builtin.zig_backend == .stage1)
fn (i32, *timespec) callconv(.C) usize
else
*const fn (i32, *timespec) callconv(.C) usize;
pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
if (@hasDecl(VDSO, "CGT_SYM")) {
const ptr = @atomicLoad(?*const anyopaque, &vdso_clock_gettime, .Unordered);
if (ptr) |fn_ptr| {
const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);
const rc = f(clk_id, tp);
switch (rc) {
0, @bitCast(usize, -@as(isize, @enumToInt(E.INVAL))) => return rc,
else => {},
}
}
}
return syscall2(.clock_gettime, @bitCast(usize, @as(isize, clk_id)), @ptrToInt(tp));
}
fn init_vdso_clock_gettime(clk: i32, ts: *timespec) callconv(.C) usize {
const ptr = @intToPtr(?*const anyopaque, vdso.lookup(VDSO.CGT_VER, VDSO.CGT_SYM));
// Note that we may not have a VDSO at all, update the stub address anyway
// so that clock_gettime will fall back on the good old (and slow) syscall
@atomicStore(?*const anyopaque, &vdso_clock_gettime, ptr, .Monotonic);
// Call into the VDSO if available
if (ptr) |fn_ptr| {
const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);
return f(clk, ts);
}
return @bitCast(usize, -@as(isize, @enumToInt(E.NOSYS)));
}
pub fn clock_getres(clk_id: i32, tp: *timespec) usize {
return syscall2(.clock_getres, @bitCast(usize, @as(isize, clk_id)), @ptrToInt(tp));
}
pub fn clock_settime(clk_id: i32, tp: *const timespec) usize {
return syscall2(.clock_settime, @bitCast(usize, @as(isize, clk_id)), @ptrToInt(tp));
}
pub fn gettimeofday(tv: *timeval, tz: *timezone) usize {
return syscall2(.gettimeofday, @ptrToInt(tv), @ptrToInt(tz));
}
pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize {
return syscall2(.settimeofday, @ptrToInt(tv), @ptrToInt(tz));
}
pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {
return syscall2(.nanosleep, @ptrToInt(req), @ptrToInt(rem));
}
pub fn setuid(uid: uid_t) usize {
if (@hasField(SYS, "setuid32")) {
return syscall1(.setuid32, uid);
} else {
return syscall1(.setuid, uid);
}
}
pub fn setgid(gid: gid_t) usize {
if (@hasField(SYS, "setgid32")) {
return syscall1(.setgid32, gid);
} else {
return syscall1(.setgid, gid);
}
}
pub fn setreuid(ruid: uid_t, euid: uid_t) usize {
if (@hasField(SYS, "setreuid32")) {
return syscall2(.setreuid32, ruid, euid);
} else {
return syscall2(.setreuid, ruid, euid);
}
}
pub fn setregid(rgid: gid_t, egid: gid_t) usize {
if (@hasField(SYS, "setregid32")) {
return syscall2(.setregid32, rgid, egid);
} else {
return syscall2(.setregid, rgid, egid);
}
}
pub fn getuid() uid_t {
if (@hasField(SYS, "getuid32")) {
return @intCast(uid_t, syscall0(.getuid32));
} else {
return @intCast(uid_t, syscall0(.getuid));
}
}
pub fn getgid() gid_t {
if (@hasField(SYS, "getgid32")) {
return @intCast(gid_t, syscall0(.getgid32));
} else {
return @intCast(gid_t, syscall0(.getgid));
}
}
pub fn geteuid() uid_t {
if (@hasField(SYS, "geteuid32")) {
return @intCast(uid_t, syscall0(.geteuid32));
} else {
return @intCast(uid_t, syscall0(.geteuid));
}
}
pub fn getegid() gid_t {
if (@hasField(SYS, "getegid32")) {
return @intCast(gid_t, syscall0(.getegid32));
} else {