Merge pull request #1429 from shawnl/arm64

initial arm64 support
This commit is contained in:
Andrew Kelley 2018-10-06 00:11:39 -04:00 committed by GitHub
commit d40c4e7c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 713 additions and 86 deletions

View File

@ -578,6 +578,7 @@ set(ZIG_STD_FILES
"os/linux/index.zig"
"os/linux/vdso.zig"
"os/linux/x86_64.zig"
"os/linux/arm64.zig"
"os/path.zig"
"os/time.zig"
"os/windows/advapi32.zig"

View File

@ -280,7 +280,7 @@ pub const LibCInstallation = struct {
switch (builtin.arch) {
builtin.Arch.i386 => try stream.write("x86"),
builtin.Arch.x86_64 => try stream.write("x64"),
builtin.Arch.aarch64 => try stream.write("arm"),
builtin.Arch.aarch64v8 => try stream.write("arm"),
else => return error.UnsupportedArchitecture,
}
const ucrt_lib_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "ucrt.lib");
@ -355,7 +355,7 @@ pub const LibCInstallation = struct {
switch (builtin.arch) {
builtin.Arch.i386 => try stream.write("x86\\"),
builtin.Arch.x86_64 => try stream.write("x64\\"),
builtin.Arch.aarch64 => try stream.write("arm\\"),
builtin.Arch.aarch64v8 => try stream.write("arm\\"),
else => return error.UnsupportedArchitecture,
}
const kernel32_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "kernel32.lib");

View File

@ -330,7 +330,7 @@ fn constructLinkerArgsCoff(ctx: *Context) !void {
switch (ctx.comp.target.getArch()) {
builtin.Arch.i386 => try ctx.args.append(c"-MACHINE:X86"),
builtin.Arch.x86_64 => try ctx.args.append(c"-MACHINE:X64"),
builtin.Arch.aarch64 => try ctx.args.append(c"-MACHINE:ARM"),
builtin.Arch.aarch64v8 => try ctx.args.append(c"-MACHINE:ARM"),
else => return error.UnsupportedLinkArchitecture,
}
@ -556,7 +556,7 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {
}
},
DarwinPlatform.Kind.IPhoneOS => {
if (ctx.comp.target.getArch() == builtin.Arch.aarch64) {
if (ctx.comp.target.getArch() == builtin.Arch.aarch64v8) {
// iOS does not need any crt1 files for arm64
} else if (platform.versionLessThan(3, 1)) {
try ctx.args.append(c"-lcrt1.o");

View File

@ -250,8 +250,20 @@ pub const Target = union(enum) {
builtin.Arch.renderscript32,
=> return 32,
builtin.Arch.aarch64,
builtin.Arch.aarch64_be,
builtin.Arch.aarch64v8_3a,
builtin.Arch.aarch64v8_2a,
builtin.Arch.aarch64v8_1a,
builtin.Arch.aarch64v8,
builtin.Arch.aarch64v8r,
builtin.Arch.aarch64v8m_baseline,
builtin.Arch.aarch64v8m_mainline,
builtin.Arch.aarch64_bev8_3a,
builtin.Arch.aarch64_bev8_2a,
builtin.Arch.aarch64_bev8_1a,
builtin.Arch.aarch64_bev8,
builtin.Arch.aarch64_bev8r,
builtin.Arch.aarch64_bev8m_baseline,
builtin.Arch.aarch64_bev8m_mainline,
builtin.Arch.mips64,
builtin.Arch.mips64el,
builtin.Arch.powerpc64,
@ -316,8 +328,23 @@ pub const Target = union(enum) {
builtin.Arch.sparcel,
=> return "/lib/ld-linux.so.2",
builtin.Arch.aarch64 => return "/lib/ld-linux-aarch64.so.1",
builtin.Arch.aarch64_be => return "/lib/ld-linux-aarch64_be.so.1",
builtin.Arch.aarch64v8_3a,
builtin.Arch.aarch64v8_2a,
builtin.Arch.aarch64v8_1a,
builtin.Arch.aarch64v8,
builtin.Arch.aarch64v8r,
builtin.Arch.aarch64v8m_baseline,
builtin.Arch.aarch64v8m_mainline,
=> return "/lib/ld-linux-aarch64.so.1",
builtin.Arch.aarch64_bev8_3a,
builtin.Arch.aarch64_bev8_2a,
builtin.Arch.aarch64_bev8_1a,
builtin.Arch.aarch64_bev8,
builtin.Arch.aarch64_bev8r,
builtin.Arch.aarch64_bev8m_baseline,
builtin.Arch.aarch64_bev8m_mainline,
=> return "/lib/ld-linux-aarch64_be.so.1",
builtin.Arch.armv8_3a,
builtin.Arch.armv8_2a,
@ -530,7 +557,14 @@ pub const Target = union(enum) {
pub fn getDarwinArchString(self: Target) []const u8 {
const arch = self.getArch();
switch (arch) {
builtin.Arch.aarch64 => return "arm64",
builtin.Arch.aarch64v8_3a,
builtin.Arch.aarch64v8_2a,
builtin.Arch.aarch64v8_1a,
builtin.Arch.aarch64v8,
builtin.Arch.aarch64v8r,
builtin.Arch.aarch64v8m_baseline,
builtin.Arch.aarch64v8m_mainline,
=> return "arm64",
builtin.Arch.thumb,
builtin.Arch.armv8_3a,
builtin.Arch.armv8_2a,

View File

@ -90,7 +90,7 @@ static void populate_termination(Termination *term, int status) {
static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args, Termination *term) {
pid_t pid = fork();
if (pid == -1)
zig_panic("fork failed");
zig_panic("fork failed: %s", strerror(errno));
if (pid == 0) {
// child
const char **argv = allocate<const char *>(args.length + 2);
@ -806,7 +806,7 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,
pid_t pid = fork();
if (pid == -1)
zig_panic("fork failed");
zig_panic("fork failed: %s", strerror(errno));
if (pid == 0) {
// child
if (dup2(stdin_pipe[0], STDIN_FILENO) == -1)

View File

@ -55,8 +55,22 @@ static const ArchType arch_list[] = {
{ZigLLVM_armeb, ZigLLVM_ARMSubArch_v5te},
{ZigLLVM_armeb, ZigLLVM_ARMSubArch_v4t},
{ZigLLVM_aarch64, ZigLLVM_NoSubArch},
{ZigLLVM_aarch64_be, ZigLLVM_NoSubArch},
{ZigLLVM_aarch64, ZigLLVM_ARMSubArch_v8_3a},
{ZigLLVM_aarch64, ZigLLVM_ARMSubArch_v8_2a},
{ZigLLVM_aarch64, ZigLLVM_ARMSubArch_v8_1a},
{ZigLLVM_aarch64, ZigLLVM_ARMSubArch_v8},
{ZigLLVM_aarch64, ZigLLVM_ARMSubArch_v8r},
{ZigLLVM_aarch64, ZigLLVM_ARMSubArch_v8m_baseline},
{ZigLLVM_aarch64, ZigLLVM_ARMSubArch_v8m_mainline},
{ZigLLVM_aarch64_be, ZigLLVM_ARMSubArch_v8_3a},
{ZigLLVM_aarch64_be, ZigLLVM_ARMSubArch_v8_2a},
{ZigLLVM_aarch64_be, ZigLLVM_ARMSubArch_v8_1a},
{ZigLLVM_aarch64_be, ZigLLVM_ARMSubArch_v8},
{ZigLLVM_aarch64_be, ZigLLVM_ARMSubArch_v8r},
{ZigLLVM_aarch64_be, ZigLLVM_ARMSubArch_v8m_baseline},
{ZigLLVM_aarch64_be, ZigLLVM_ARMSubArch_v8m_mainline},
{ZigLLVM_arc, ZigLLVM_NoSubArch},
{ZigLLVM_avr, ZigLLVM_NoSubArch},
{ZigLLVM_bpfel, ZigLLVM_NoSubArch},
@ -943,8 +957,9 @@ const char *arch_stack_pointer_register_name(const ArchType *arch) {
return "sp";
case ZigLLVM_x86_64:
return "rsp";
case ZigLLVM_aarch64:
return "sp";
case ZigLLVM_arm:
case ZigLLVM_thumb:
case ZigLLVM_aarch64_be:

View File

@ -859,6 +859,11 @@ pub const Elf_MIPS_ABIFlags_v0 = extern struct {
flags2: Elf32_Word,
};
pub const Auxv = switch (@sizeOf(usize)) {
4 => Elf32_auxv_t,
8 => Elf64_auxv_t,
else => @compileError("expected pointer size of 32 or 64"),
};
pub const Ehdr = switch (@sizeOf(usize)) {
4 => Elf32_Ehdr,
8 => Elf64_Ehdr,

View File

@ -1044,7 +1044,9 @@ test "fmt.format" {
const result = try bufPrint(buf1[0..], "f64: {}\n", math.nan_f64);
assert(mem.eql(u8, result, "f64: nan\n"));
}
{
if (builtin.arch != builtin.Arch.armv8) {
// negative nan is not defined by IEE 754,
// and ARM thus normalizes it to positive nan
var buf1: [32]u8 = undefined;
const result = try bufPrint(buf1[0..], "f64: {}\n", -math.nan_f64);
assert(mem.eql(u8, result, "f64: -nan\n"));

View File

@ -8,7 +8,7 @@ pub const pi = 3.14159265358979323846264338327950288419716939937510;
// float.h details
pub const f64_true_min = 4.94065645841246544177e-324;
pub const f64_min = 2.22507385850720138309e-308;
pub const f64_min = 2.2250738585072014e-308;
pub const f64_max = 1.79769313486231570815e+308;
pub const f64_epsilon = 2.22044604925031308085e-16;
pub const f64_toint = 1.0 / f64_epsilon;
@ -650,3 +650,9 @@ pub fn lossyCast(comptime T: type, value: var) T {
else => @compileError("bad type"),
}
}
test "math.f64_min" {
const f64_min_u64 = 0x0010000000000000;
const fmin: f64 = f64_min;
assert(@bitCast(u64, fmin) == f64_min_u64);
}

View File

@ -655,16 +655,21 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
}
}
pub var linux_aux_raw = []usize{0} ** 38;
pub var linux_elf_aux_maybe: ?[*]std.elf.Auxv = null;
pub var posix_environ_raw: [][*]u8 = undefined;
/// See std.elf for the constants.
pub fn linuxGetAuxVal(index: usize) usize {
if (builtin.link_libc) {
return usize(std.c.getauxval(index));
} else {
return linux_aux_raw[index];
} else if (linux_elf_aux_maybe) |auxv| {
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;
}
pub fn getBaseAddress() usize {
@ -1676,7 +1681,7 @@ pub const Dir = struct {
}
while (true) {
const result = posix.getdents(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len);
const result = posix.getdents64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len);
const err = posix.getErrno(result);
if (err > 0) {
switch (err) {
@ -1694,7 +1699,7 @@ pub const Dir = struct {
break;
}
}
const linux_entry = @ptrCast(*align(1) posix.dirent, &self.handle.buf[self.handle.index]);
const linux_entry = @ptrCast(*align(1) posix.dirent64, &self.handle.buf[self.handle.index]);
const next_index = self.handle.index + linux_entry.d_reclen;
self.handle.index = next_index;
@ -1705,8 +1710,7 @@ pub const Dir = struct {
continue :start_over;
}
const type_char = self.handle.buf[next_index - 1];
const entry_kind = switch (type_char) {
const entry_kind = switch (linux_entry.d_type) {
posix.DT_BLK => Entry.Kind.BlockDevice,
posix.DT_CHR => Entry.Kind.CharacterDevice,
posix.DT_DIR => Entry.Kind.Directory,

465
std/os/linux/arm64.zig Normal file
View File

@ -0,0 +1,465 @@
const std = @import("../../index.zig");
const linux = std.os.linux;
const socklen_t = linux.socklen_t;
const iovec = linux.iovec;
pub const SYS_io_setup = 0;
pub const SYS_io_destroy = 1;
pub const SYS_io_submit = 2;
pub const SYS_io_cancel = 3;
pub const SYS_io_getevents = 4;
pub const SYS_setxattr = 5;
pub const SYS_lsetxattr = 6;
pub const SYS_fsetxattr = 7;
pub const SYS_getxattr = 8;
pub const SYS_lgetxattr = 9;
pub const SYS_fgetxattr = 10;
pub const SYS_listxattr = 11;
pub const SYS_llistxattr = 12;
pub const SYS_flistxattr = 13;
pub const SYS_removexattr = 14;
pub const SYS_lremovexattr = 15;
pub const SYS_fremovexattr = 16;
pub const SYS_getcwd = 17;
pub const SYS_lookup_dcookie = 18;
pub const SYS_eventfd2 = 19;
pub const SYS_epoll_create1 = 20;
pub const SYS_epoll_ctl = 21;
pub const SYS_epoll_pwait = 22;
pub const SYS_dup = 23;
pub const SYS_dup3 = 24;
pub const SYS_fcntl = 25;
pub const SYS_inotify_init1 = 26;
pub const SYS_inotify_add_watch = 27;
pub const SYS_inotify_rm_watch = 28;
pub const SYS_ioctl = 29;
pub const SYS_ioprio_set = 30;
pub const SYS_ioprio_get = 31;
pub const SYS_flock = 32;
pub const SYS_mknodat = 33;
pub const SYS_mkdirat = 34;
pub const SYS_unlinkat = 35;
pub const SYS_symlinkat = 36;
pub const SYS_linkat = 37;
pub const SYS_renameat = 38;
pub const SYS_umount2 = 39;
pub const SYS_mount = 40;
pub const SYS_pivot_root = 41;
pub const SYS_nfsservctl = 42;
pub const SYS_statfs = 43;
pub const SYS_fstatfs = 44;
pub const SYS_truncate = 45;
pub const SYS_ftruncate = 46;
pub const SYS_fallocate = 47;
pub const SYS_faccessat = 48;
pub const SYS_chdir = 49;
pub const SYS_fchdir = 50;
pub const SYS_chroot = 51;
pub const SYS_fchmod = 52;
pub const SYS_fchmodat = 53;
pub const SYS_fchownat = 54;
pub const SYS_fchown = 55;
pub const SYS_openat = 56;
pub const SYS_close = 57;
pub const SYS_vhangup = 58;
pub const SYS_pipe2 = 59;
pub const SYS_quotactl = 60;
pub const SYS_getdents64 = 61;
pub const SYS_lseek = 62;
pub const SYS_read = 63;
pub const SYS_write = 64;
pub const SYS_readv = 65;
pub const SYS_writev = 66;
pub const SYS_pread64 = 67;
pub const SYS_pwrite64 = 68;
pub const SYS_preadv = 69;
pub const SYS_pwritev = 70;
pub const SYS_pselect6 = 72;
pub const SYS_ppoll = 73;
pub const SYS_signalfd4 = 74;
pub const SYS_vmsplice = 75;
pub const SYS_splice = 76;
pub const SYS_tee = 77;
pub const SYS_readlinkat = 78;
pub const SYS_fstatat = 79;
pub const SYS_fstat = 80;
pub const SYS_sync = 81;
pub const SYS_fsync = 82;
pub const SYS_fdatasync = 83;
pub const SYS_sync_file_range2 = 84;
pub const SYS_sync_file_range = 84;
pub const SYS_timerfd_create = 85;
pub const SYS_timerfd_settime = 86;
pub const SYS_timerfd_gettime = 87;
pub const SYS_utimensat = 88;
pub const SYS_acct = 89;
pub const SYS_capget = 90;
pub const SYS_capset = 91;
pub const SYS_personality = 92;
pub const SYS_exit = 93;
pub const SYS_exit_group = 94;
pub const SYS_waitid = 95;
pub const SYS_set_tid_address = 96;
pub const SYS_unshare = 97;
pub const SYS_futex = 98;
pub const SYS_set_robust_list = 99;
pub const SYS_get_robust_list = 100;
pub const SYS_nanosleep = 101;
pub const SYS_getitimer = 102;
pub const SYS_setitimer = 103;
pub const SYS_kexec_load = 104;
pub const SYS_init_module = 105;
pub const SYS_delete_module = 106;
pub const SYS_timer_create = 107;
pub const SYS_timer_gettime = 108;
pub const SYS_timer_getoverrun = 109;
pub const SYS_timer_settime = 110;
pub const SYS_timer_delete = 111;
pub const SYS_clock_settime = 112;
pub const SYS_clock_gettime = 113;
pub const SYS_clock_getres = 114;
pub const SYS_clock_nanosleep = 115;
pub const SYS_syslog = 116;
pub const SYS_ptrace = 117;
pub const SYS_sched_setparam = 118;
pub const SYS_sched_setscheduler = 119;
pub const SYS_sched_getscheduler = 120;
pub const SYS_sched_getparam = 121;
pub const SYS_sched_setaffinity = 122;
pub const SYS_sched_getaffinity = 123;
pub const SYS_sched_yield = 124;
pub const SYS_sched_get_priority_max = 125;
pub const SYS_sched_get_priority_min = 126;
pub const SYS_sched_rr_get_interval = 127;
pub const SYS_restart_syscall = 128;
pub const SYS_kill = 129;
pub const SYS_tkill = 130;
pub const SYS_tgkill = 131;
pub const SYS_sigaltstack = 132;
pub const SYS_rt_sigsuspend = 133;
pub const SYS_rt_sigaction = 134;
pub const SYS_rt_sigprocmask = 135;
pub const SYS_rt_sigpending = 136;
pub const SYS_rt_sigtimedwait = 137;
pub const SYS_rt_sigqueueinfo = 138;
pub const SYS_rt_sigreturn = 139;
pub const SYS_setpriority = 140;
pub const SYS_getpriority = 141;
pub const SYS_reboot = 142;
pub const SYS_setregid = 143;
pub const SYS_setgid = 144;
pub const SYS_setreuid = 145;
pub const SYS_setuid = 146;
pub const SYS_setresuid = 147;
pub const SYS_getresuid = 148;
pub const SYS_setresgid = 149;
pub const SYS_getresgid = 150;
pub const SYS_setfsuid = 151;
pub const SYS_setfsgid = 152;
pub const SYS_times = 153;
pub const SYS_setpgid = 154;
pub const SYS_getpgid = 155;
pub const SYS_getsid = 156;
pub const SYS_setsid = 157;
pub const SYS_getgroups = 158;
pub const SYS_setgroups = 159;
pub const SYS_uname = 160;
pub const SYS_sethostname = 161;
pub const SYS_setdomainname = 162;
pub const SYS_getrlimit = 163;
pub const SYS_setrlimit = 164;
pub const SYS_getrusage = 165;
pub const SYS_umask = 166;
pub const SYS_prctl = 167;
pub const SYS_getcpu = 168;
pub const SYS_gettimeofday = 169;
pub const SYS_settimeofday = 170;
pub const SYS_adjtimex = 171;
pub const SYS_getpid = 172;
pub const SYS_getppid = 173;
pub const SYS_getuid = 174;
pub const SYS_geteuid = 175;
pub const SYS_getgid = 176;
pub const SYS_getegid = 177;
pub const SYS_gettid = 178;
pub const SYS_sysinfo = 179;
pub const SYS_mq_open = 180;
pub const SYS_mq_unlink = 181;
pub const SYS_mq_timedsend = 182;
pub const SYS_mq_timedreceive = 183;
pub const SYS_mq_notify = 184;
pub const SYS_mq_getsetattr = 185;
pub const SYS_msgget = 186;
pub const SYS_msgctl = 187;
pub const SYS_msgrcv = 188;
pub const SYS_msgsnd = 189;
pub const SYS_semget = 190;
pub const SYS_semctl = 191;
pub const SYS_semtimedop = 192;
pub const SYS_semop = 193;
pub const SYS_shmget = 194;
pub const SYS_shmctl = 195;
pub const SYS_shmat = 196;
pub const SYS_shmdt = 197;
pub const SYS_socket = 198;
pub const SYS_socketpair = 199;
pub const SYS_bind = 200;
pub const SYS_listen = 201;
pub const SYS_accept = 202;
pub const SYS_connect = 203;
pub const SYS_getsockname = 204;
pub const SYS_getpeername = 205;
pub const SYS_sendto = 206;
pub const SYS_recvfrom = 207;
pub const SYS_setsockopt = 208;
pub const SYS_getsockopt = 209;
pub const SYS_shutdown = 210;
pub const SYS_sendmsg = 211;
pub const SYS_recvmsg = 212;
pub const SYS_readahead = 213;
pub const SYS_brk = 214;
pub const SYS_munmap = 215;
pub const SYS_mremap = 216;
pub const SYS_add_key = 217;
pub const SYS_request_key = 218;
pub const SYS_keyctl = 219;
pub const SYS_clone = 220;
pub const SYS_execve = 221;
pub const SYS_mmap = 222;
pub const SYS_fadvise64 = 223;
pub const SYS_swapon = 224;
pub const SYS_swapoff = 225;
pub const SYS_mprotect = 226;
pub const SYS_msync = 227;
pub const SYS_mlock = 228;
pub const SYS_munlock = 229;
pub const SYS_mlockall = 230;
pub const SYS_munlockall = 231;
pub const SYS_mincore = 232;
pub const SYS_madvise = 233;
pub const SYS_remap_file_pages = 234;
pub const SYS_mbind = 235;
pub const SYS_get_mempolicy = 236;
pub const SYS_set_mempolicy = 237;
pub const SYS_migrate_pages = 238;
pub const SYS_move_pages = 239;
pub const SYS_rt_tgsigqueueinfo = 240;
pub const SYS_perf_event_open = 241;
pub const SYS_accept4 = 242;
pub const SYS_recvmmsg = 243;
pub const SYS_arch_specific_syscall = 244;
pub const SYS_wait4 = 260;
pub const SYS_prlimit64 = 261;
pub const SYS_fanotify_init = 262;
pub const SYS_fanotify_mark = 263;
pub const SYS_clock_adjtime = 266;
pub const SYS_syncfs = 267;
pub const SYS_setns = 268;
pub const SYS_sendmmsg = 269;
pub const SYS_process_vm_readv = 270;
pub const SYS_process_vm_writev = 271;
pub const SYS_kcmp = 272;
pub const SYS_finit_module = 273;
pub const SYS_sched_setattr = 274;
pub const SYS_sched_getattr = 275;
pub const SYS_renameat2 = 276;
pub const SYS_seccomp = 277;
pub const SYS_getrandom = 278;
pub const SYS_memfd_create = 279;
pub const SYS_bpf = 280;
pub const SYS_execveat = 281;
pub const SYS_userfaultfd = 282;
pub const SYS_membarrier = 283;
pub const SYS_mlock2 = 284;
pub const SYS_copy_file_range = 285;
pub const SYS_preadv2 = 286;
pub const SYS_pwritev2 = 287;
pub const SYS_pkey_mprotect = 288;
pub const SYS_pkey_alloc = 289;
pub const SYS_pkey_free = 290;
pub const SYS_statx = 291;
pub const SYS_io_pgetevents = 292;
pub const SYS_syscalls = 293;
pub const O_CREAT = 0o100;
pub const O_EXCL = 0o200;
pub const O_NOCTTY = 0o400;
pub const O_TRUNC = 0o1000;
pub const O_APPEND = 0o2000;
pub const O_NONBLOCK = 0o4000;
pub const O_DSYNC = 0o10000;
pub const O_SYNC = 0o4010000;
pub const O_RSYNC = 0o4010000;
pub const O_DIRECTORY = 0o200000;
pub const O_NOFOLLOW = 0o400000;
pub const O_CLOEXEC = 0o2000000;
pub const O_ASYNC = 0o20000;
pub const O_DIRECT = 0o40000;
pub const O_LARGEFILE = 0;
pub const O_NOATIME = 0o1000000;
pub const O_PATH = 0o10000000;
pub const O_TMPFILE = 0o20200000;
pub const O_NDELAY = O_NONBLOCK;
pub const F_DUPFD = 0;
pub const F_GETFD = 1;
pub const F_SETFD = 2;
pub const F_GETFL = 3;
pub const F_SETFL = 4;
pub const F_SETOWN = 8;
pub const F_GETOWN = 9;
pub const F_SETSIG = 10;
pub const F_GETSIG = 11;
pub const F_GETLK = 5;
pub const F_SETLK = 6;
pub const F_SETLKW = 7;
pub const F_SETOWN_EX = 15;
pub const F_GETOWN_EX = 16;
pub const F_GETOWNER_UIDS = 17;
pub const AT_FDCWD = -100;
pub const AT_SYMLINK_NOFOLLOW = 0x100;
pub const AT_REMOVEDIR = 0x200;
pub const AT_SYMLINK_FOLLOW = 0x400;
pub const AT_NO_AUTOMOUNT = 0x800;
pub const AT_EMPTY_PATH = 0x1000;
pub const VDSO_USEFUL = true;
pub const VDSO_CGT_SYM = "__kernel_clock_gettime";
pub const VDSO_CGT_VER = "LINUX_2.6.39";
pub fn syscall0(number: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize)
: [number] "{x8}" (number)
);
}
pub fn syscall1(number: usize, arg1: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize)
: [number] "{x8}" (number),
[arg1] "{x0}" (arg1)
);
}
pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize)
: [number] "{x8}" (number),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2)
);
}
pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize)
: [number] "{x8}" (number),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
[arg3] "{x2}" (arg3)
);
}
pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize)
: [number] "{x8}" (number),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
[arg3] "{x2}" (arg3),
[arg4] "{x3}" (arg4)
);
}
pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize)
: [number] "{x8}" (number),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
[arg3] "{x2}" (arg3),
[arg4] "{x3}" (arg4),
[arg5] "{x4}" (arg5)
);
}
pub fn syscall6(
number: usize,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
arg5: usize,
arg6: usize,
) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize)
: [number] "{x8}" (number),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
[arg3] "{x2}" (arg3),
[arg4] "{x3}" (arg4),
[arg5] "{x4}" (arg5),
[arg6] "{x5}" (arg6)
);
}
/// This matches the libc clone function.
pub extern fn clone(func: extern fn (arg: usize) u8, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub const msghdr = extern struct {
msg_name: *u8,
msg_namelen: socklen_t,
msg_iov: *iovec,
msg_iovlen: i32,
__pad1: i32,
msg_control: *u8,
msg_controllen: socklen_t,
__pad2: socklen_t,
msg_flags: i32,
};
/// Renamed to Stat to not conflict with the stat function.
pub const Stat = extern struct {
dev: u64,
ino: u64,
nlink: usize,
mode: u32,
uid: u32,
gid: u32,
__pad0: u32,
rdev: u64,
size: i64,
blksize: isize,
blocks: i64,
atim: timespec,
mtim: timespec,
ctim: timespec,
__unused: [3]isize,
};
pub const timespec = extern struct {
tv_sec: isize,
tv_nsec: isize,
};
pub const timeval = extern struct {
tv_sec: isize,
tv_usec: isize,
};
pub const timezone = extern struct {
tz_minuteswest: i32,
tz_dsttime: i32,
};
pub const Elf_Symndx = u32;

View File

@ -5,6 +5,7 @@ const vdso = @import("vdso.zig");
pub use switch (builtin.arch) {
builtin.Arch.x86_64 => @import("x86_64.zig"),
builtin.Arch.i386 => @import("i386.zig"),
builtin.Arch.aarch64v8 => @import("arm64.zig"),
else => @compileError("unsupported arch"),
};
pub use @import("errno.zig");
@ -697,7 +698,11 @@ pub fn getErrno(r: usize) usize {
}
pub fn dup2(old: i32, new: i32) usize {
return syscall2(SYS_dup2, @intCast(usize, old), @intCast(usize, new));
return dup3(old, new, 0);
}
pub fn dup3(old: i32, new: i32, flags: u32) usize {
return syscall3(SYS_dup3, @intCast(usize, old), @intCast(usize, new), flags);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -716,7 +721,7 @@ pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*
}
pub fn fork() usize {
return syscall0(SYS_fork);
return clone2(SIGCHLD, 0);
}
/// This must be inline, and inline call the syscall function, because if the
@ -740,8 +745,8 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
return syscall2(SYS_getcwd, @ptrToInt(buf), size);
}
pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
return syscall3(SYS_getdents, @intCast(usize, fd), @ptrToInt(dirp), count);
pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize {
return syscall3(SYS_getdents64, @intCast(usize, fd), @ptrToInt(dirp), count);
}
pub fn inotify_init1(flags: u32) usize {
@ -763,16 +768,26 @@ pub fn isatty(fd: i32) bool {
// TODO https://github.com/ziglang/zig/issues/265
pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
return readlinkat(AT_FDCWD, path, buf_ptr, buf_len);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
return syscall4(SYS_readlinkat, @intCast(usize, dirfd), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn mkdir(path: [*]const u8, mode: u32) usize {
return syscall2(SYS_mkdir, @ptrToInt(path), mode);
return mkdirat(AT_FDCWD, path, mode);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: usize, data: usize) usize {
pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize {
return syscall3(SYS_mkdirat, @intCast(usize, dirfd), @ptrToInt(path), mode);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize {
return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data);
}
@ -816,28 +831,38 @@ pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) us
// TODO https://github.com/ziglang/zig/issues/265
pub fn rmdir(path: [*]const u8) usize {
return syscall1(SYS_rmdir, @ptrToInt(path));
return unlinkat(AT_FDCWD, path, AT_REMOVEDIR);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new));
return symlinkat(existing, AT_FDCWD, new);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize {
return syscall3(SYS_symlinkat, @ptrToInt(existing), @intCast(usize, newfd), @ptrToInt(newpath));
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn access(path: [*]const u8, mode: u32) usize {
return syscall2(SYS_access, @ptrToInt(path), mode);
return faccessat(AT_FDCWD, path, mode);
}
pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize {
return syscall3(SYS_faccessat, @intCast(usize, dirfd), @ptrToInt(path), mode);
}
pub fn pipe(fd: *[2]i32) usize {
return pipe2(fd, 0);
}
pub fn pipe2(fd: *[2]i32, flags: usize) usize {
pub fn pipe2(fd: *[2]i32, flags: u32) usize {
return syscall2(SYS_pipe2, @ptrToInt(fd), flags);
}
@ -851,12 +876,17 @@ pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
// TODO https://github.com/ziglang/zig/issues/265
pub fn rename(old: [*]const u8, new: [*]const u8) usize {
return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new));
return renameat2(AT_FDCWD, old, AT_FDCWD, new, 0);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize {
return syscall5(SYS_renameat2, @intCast(usize, oldfd), @ptrToInt(oldpath), @intCast(usize, newfd), @ptrToInt(newpath), flags);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn open(path: [*]const u8, flags: u32, perm: usize) usize {
return syscall3(SYS_open, @ptrToInt(path), flags, perm);
return openat(AT_FDCWD, path, flags, perm);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -865,7 +895,7 @@ pub fn create(path: [*]const u8, perm: usize) usize {
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {
pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize {
return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);
}
@ -875,7 +905,7 @@ pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid:
}
/// See also `clone` (from the arch-specific include)
pub fn clone2(flags: usize, child_stack_ptr: usize) usize {
pub fn clone2(flags: u32, child_stack_ptr: usize) usize {
return syscall2(SYS_clone, flags, child_stack_ptr);
}
@ -898,7 +928,7 @@ pub fn exit_group(status: i32) noreturn {
}
pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
return syscall3(SYS_getrandom, @ptrToInt(buf), count, @intCast(usize, flags));
return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags);
}
pub fn kill(pid: i32, sig: i32) usize {
@ -907,7 +937,12 @@ pub fn kill(pid: i32, sig: i32) usize {
// TODO https://github.com/ziglang/zig/issues/265
pub fn unlink(path: [*]const u8) usize {
return syscall1(SYS_unlink, @ptrToInt(path));
return unlinkat(AT_FDCWD, path, 0);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize {
return syscall3(SYS_unlinkat, @intCast(usize, dirfd), @ptrToInt(path), flags);
}
pub fn waitpid(pid: i32, status: *i32, options: i32) usize {
@ -1222,12 +1257,17 @@ pub fn fstat(fd: i32, stat_buf: *Stat) usize {
// TODO https://github.com/ziglang/zig/issues/265
pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize {
return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf));
return fstatat(AT_FDCWD, pathname, statbuf, AT_NO_AUTOMOUNT);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize {
return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf));
return fstatat(AF_FDCWD, pathname, statbuf, AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT);
}
// TODO https://github.com/ziglang/zig/issues/265
pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize {
return syscall4(SYS_fstatat, @intCast(usize, dirfd), @ptrToInt(path), @ptrToInt(stat_buf), flags);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -1318,7 +1358,18 @@ pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
}
pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {
return syscall4(SYS_epoll_wait, @intCast(usize, epoll_fd), @ptrToInt(events), @intCast(usize, maxevents), @intCast(usize, timeout));
return epoll_pwait(epoll_fd, events, maxevents, timeout, null);
}
pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize {
return syscall6(SYS_epoll_pwait,
@intCast(usize, epoll_fd),
@ptrToInt(events),
@intCast(usize, maxevents),
@intCast(usize, timeout),
@ptrToInt(sigmask),
@sizeOf(sigset_t)
);
}
pub fn eventfd(count: u32, flags: u32) usize {
@ -1326,7 +1377,7 @@ pub fn eventfd(count: u32, flags: u32) usize {
}
pub fn timerfd_create(clockid: i32, flags: u32) usize {
return syscall2(SYS_timerfd_create, @intCast(usize, clockid), @intCast(usize, flags));
return syscall2(SYS_timerfd_create, @intCast(usize, clockid), flags);
}
pub const itimerspec = extern struct {
@ -1339,7 +1390,7 @@ pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
}
pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
return syscall4(SYS_timerfd_settime, @intCast(usize, fd), @intCast(usize, flags), @ptrToInt(new_value), @ptrToInt(old_value));
return syscall4(SYS_timerfd_settime, @intCast(usize, fd), flags, @ptrToInt(new_value), @ptrToInt(old_value));
}
pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;
@ -1449,7 +1500,7 @@ pub const cap_user_data_t = extern struct {
};
pub fn unshare(flags: usize) usize {
return syscall1(SYS_unshare, @intCast(usize, flags));
return syscall1(SYS_unshare, flags);
}
pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {
@ -1468,6 +1519,14 @@ pub const inotify_event = extern struct {
//name: [?]u8,
};
pub const dirent64 = extern struct {
d_ino: u64,
d_off: u64,
d_reclen: u16,
d_type: u8,
d_name: u8, // field address is the address of first byte of name https://github.com/ziglang/zig/issues/173
};
test "import" {
if (builtin.os == builtin.Os.linux) {
_ = @import("test.zig");

View File

@ -5,7 +5,7 @@ const cstr = std.cstr;
const mem = std.mem;
pub fn lookup(vername: []const u8, name: []const u8) usize {
const vdso_addr = std.os.linux_aux_raw[std.elf.AT_SYSINFO_EHDR];
const vdso_addr = std.os.linuxGetAuxVal(std.elf.AT_SYSINFO_EHDR);
if (vdso_addr == 0) return 0;
const eh = @intToPtr(*elf.Ehdr, vdso_addr);

View File

@ -266,6 +266,8 @@ pub const SYS_mknodat = 259;
pub const SYS_fchownat = 260;
pub const SYS_futimesat = 261;
pub const SYS_newfstatat = 262;
// https://github.com/ziglang/zig/issues/1439
pub const SYS_fstatat = 262;
pub const SYS_unlinkat = 263;
pub const SYS_renameat = 264;
pub const SYS_linkat = 265;
@ -371,6 +373,13 @@ pub const F_GETOWN_EX = 16;
pub const F_GETOWNER_UIDS = 17;
pub const AT_FDCWD = -100;
pub const AT_SYMLINK_NOFOLLOW = 0x100;
pub const AT_REMOVEDIR = 0x200;
pub const AT_SYMLINK_FOLLOW = 0x400;
pub const AT_NO_AUTOMOUNT = 0x800;
pub const AT_EMPTY_PATH = 0x1000;
pub const VDSO_USEFUL = true;
pub const VDSO_CGT_SYM = "__vdso_clock_gettime";
pub const VDSO_CGT_VER = "LINUX_2.6";
@ -521,11 +530,4 @@ pub const timezone = extern struct {
tz_dsttime: i32,
};
pub const dirent = extern struct {
d_ino: usize,
d_off: usize,
d_reclen: u16,
d_name: u8, // field address is the address of first byte of name
};
pub const Elf_Symndx = u32;

View File

@ -30,6 +30,11 @@ nakedcc fn _start() noreturn {
: [argc] "=r" (-> [*]usize)
);
},
builtin.Arch.aarch64v8 => {
argc_ptr = asm ("mov %[argc], sp"
: [argc] "=r" (-> [*]usize)
);
},
else => @compileError("unsupported arch"),
}
// If LLVM inlines stack variables into _start, they will overwrite
@ -54,11 +59,8 @@ fn posixCallMainAndExit() noreturn {
const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count];
if (builtin.os == builtin.Os.linux) {
const auxv = @ptrCast([*]usize, envp.ptr + envp_count + 1);
var i: usize = 0;
while (auxv[i] != 0) : (i += 2) {
if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i + 1];
}
std.debug.assert(std.os.linux_aux_raw[std.elf.AT_PAGESZ] == std.os.page_size);
std.os.linux_elf_aux_maybe = @ptrCast([*]std.elf.Auxv, auxv);
std.debug.assert(std.os.linuxGetAuxVal(std.elf.AT_PAGESZ) == std.os.page_size);
}
std.os.posix.exit(callMainWithArgs(argc, argv, envp));

View File

@ -60,7 +60,7 @@ comptime {
{
@export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong);
}
if (builtin.os == builtin.Os.linux and builtin.arch == builtin.Arch.x86_64) {
if (builtin.os == builtin.Os.linux) {
@export("clone", clone, builtin.GlobalLinkage.Strong);
}
}
@ -72,32 +72,64 @@ extern fn __stack_chk_fail() noreturn {
// it causes a segfault in release mode. this is a workaround of calling it
// across .o file boundaries. fix comptime @ptrCast of nakedcc functions.
nakedcc fn clone() void {
asm volatile (
\\ xor %%eax,%%eax
\\ mov $56,%%al
\\ mov %%rdi,%%r11
\\ mov %%rdx,%%rdi
\\ mov %%r8,%%rdx
\\ mov %%r9,%%r8
\\ mov 8(%%rsp),%%r10
\\ mov %%r11,%%r9
\\ and $-16,%%rsi
\\ sub $8,%%rsi
\\ mov %%rcx,(%%rsi)
\\ syscall
\\ test %%eax,%%eax
\\ jnz 1f
\\ xor %%ebp,%%ebp
\\ pop %%rdi
\\ call *%%r9
\\ mov %%eax,%%edi
\\ xor %%eax,%%eax
\\ mov $60,%%al
\\ syscall
\\ hlt
\\1: ret
\\
);
if (builtin.arch == builtin.Arch.x86_64) {
asm volatile (
\\ xor %%eax,%%eax
\\ mov $56,%%al // SYS_clone
\\ mov %%rdi,%%r11
\\ mov %%rdx,%%rdi
\\ mov %%r8,%%rdx
\\ mov %%r9,%%r8
\\ mov 8(%%rsp),%%r10
\\ mov %%r11,%%r9
\\ and $-16,%%rsi
\\ sub $8,%%rsi
\\ mov %%rcx,(%%rsi)
\\ syscall
\\ test %%eax,%%eax
\\ jnz 1f
\\ xor %%ebp,%%ebp
\\ pop %%rdi
\\ call *%%r9
\\ mov %%eax,%%edi
\\ xor %%eax,%%eax
\\ mov $60,%%al // SYS_exit
\\ syscall
\\ hlt
\\1: ret
\\
);
} else if (builtin.arch == builtin.Arch.aarch64v8) {
// __clone(func, stack, flags, arg, ptid, tls, ctid)
// x0, x1, w2, x3, x4, x5, x6
// syscall(SYS_clone, flags, stack, ptid, tls, ctid)
// x8, x0, x1, x2, x3, x4
asm volatile (
\\ // align stack and save func,arg
\\ and x1,x1,#-16
\\ stp x0,x3,[x1,#-16]!
\\
\\ // syscall
\\ uxtw x0,w2
\\ mov x2,x4
\\ mov x3,x5
\\ mov x4,x6
\\ mov x8,#220 // SYS_clone
\\ svc #0
\\
\\ cbz x0,1f
\\ // parent
\\ ret
\\ // child
\\1: ldp x1,x0,[sp],#16
\\ blr x1
\\ mov x8,#93 // SYS_exit
\\ svc #0
);
} else {
@compileError("Implement clone() for this arch.");
}
}
const math = @import("../math/index.zig");