From 32f4606cece09b6bcf4f553c06c3aaab34f784c6 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 28 Aug 2019 16:35:23 +0200 Subject: [PATCH 01/16] add arm32 linux bits definitions --- std/os/bits/linux.zig | 1 + std/os/bits/linux/arm-eabi.zig | 510 +++++++++++++++++++++++++++++++++ 2 files changed, 511 insertions(+) create mode 100644 std/os/bits/linux/arm-eabi.zig diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index c7d8cc2ad2..0617378da9 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -7,6 +7,7 @@ pub usingnamespace @import("linux/errno.zig"); pub usingnamespace switch (builtin.arch) { .x86_64 => @import("linux/x86_64.zig"), .aarch64 => @import("linux/arm64.zig"), + .arm => @import("linux/arm-eabi.zig"), else => struct {}, }; diff --git a/std/os/bits/linux/arm-eabi.zig b/std/os/bits/linux/arm-eabi.zig new file mode 100644 index 0000000000..ea4c452eb6 --- /dev/null +++ b/std/os/bits/linux/arm-eabi.zig @@ -0,0 +1,510 @@ +// arm-eabi-specific declarations that are intended to be imported into the POSIX namespace. + +const std = @import("../../std.zig"); +const linux = std.os.linux; +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; +const iovec_const = linux.iovec_const; + +pub const SYS_restart_syscall = 0; +pub const SYS_exit = 1; +pub const SYS_fork = 2; +pub const SYS_read = 3; +pub const SYS_write = 4; +pub const SYS_open = 5; +pub const SYS_close = 6; +pub const SYS_creat = 8; +pub const SYS_link = 9; +pub const SYS_unlink = 10; +pub const SYS_execve = 11; +pub const SYS_chdir = 12; +pub const SYS_mknod = 14; +pub const SYS_chmod = 15; +pub const SYS_lchown = 16; +pub const SYS_lseek = 19; +pub const SYS_getpid = 20; +pub const SYS_mount = 21; +pub const SYS_setuid = 23; +pub const SYS_getuid = 24; +pub const SYS_ptrace = 26; +pub const SYS_pause = 29; +pub const SYS_access = 33; +pub const SYS_nice = 34; +pub const SYS_sync = 36; +pub const SYS_kill = 37; +pub const SYS_rename = 38; +pub const SYS_mkdir = 39; +pub const SYS_rmdir = 40; +pub const SYS_dup = 41; +pub const SYS_pipe = 42; +pub const SYS_times = 43; +pub const SYS_brk = 45; +pub const SYS_setgid = 46; +pub const SYS_getgid = 47; +pub const SYS_geteuid = 49; +pub const SYS_getegid = 50; +pub const SYS_acct = 51; +pub const SYS_umount2 = 52; +pub const SYS_ioctl = 54; +pub const SYS_fcntl = 55; +pub const SYS_setpgid = 57; +pub const SYS_umask = 60; +pub const SYS_chroot = 61; +pub const SYS_ustat = 62; +pub const SYS_dup2 = 63; +pub const SYS_getppid = 64; +pub const SYS_getpgrp = 65; +pub const SYS_setsid = 66; +pub const SYS_sigaction = 67; +pub const SYS_setreuid = 70; +pub const SYS_setregid = 71; +pub const SYS_sigsuspend = 72; +pub const SYS_sigpending = 73; +pub const SYS_sethostname = 74; +pub const SYS_setrlimit = 75; +pub const SYS_getrusage = 77; +pub const SYS_gettimeofday = 78; +pub const SYS_settimeofday = 79; +pub const SYS_getgroups = 80; +pub const SYS_setgroups = 81; +pub const SYS_symlink = 83; +pub const SYS_readlink = 85; +pub const SYS_uselib = 86; +pub const SYS_swapon = 87; +pub const SYS_reboot = 88; +pub const SYS_munmap = 91; +pub const SYS_truncate = 92; +pub const SYS_ftruncate = 93; +pub const SYS_fchmod = 94; +pub const SYS_fchown = 95; +pub const SYS_getpriority = 96; +pub const SYS_setpriority = 97; +pub const SYS_statfs = 99; +pub const SYS_fstatfs = 100; +pub const SYS_syslog = 103; +pub const SYS_setitimer = 104; +pub const SYS_getitimer = 105; +pub const SYS_stat = 106; +pub const SYS_lstat = 107; +pub const SYS_fstat = 108; +pub const SYS_vhangup = 111; +pub const SYS_wait4 = 114; +pub const SYS_swapoff = 115; +pub const SYS_sysinfo = 116; +pub const SYS_fsync = 118; +pub const SYS_sigreturn = 119; +pub const SYS_clone = 120; +pub const SYS_setdomainname = 121; +pub const SYS_uname = 122; +pub const SYS_adjtimex = 124; +pub const SYS_mprotect = 125; +pub const SYS_sigprocmask = 126; +pub const SYS_init_module = 128; +pub const SYS_delete_module = 129; +pub const SYS_quotactl = 131; +pub const SYS_getpgid = 132; +pub const SYS_fchdir = 133; +pub const SYS_bdflush = 134; +pub const SYS_sysfs = 135; +pub const SYS_personality = 136; +pub const SYS_setfsuid = 138; +pub const SYS_setfsgid = 139; +pub const SYS__llseek = 140; +pub const SYS_getdents = 141; +pub const SYS__newselect = 142; +pub const SYS_flock = 143; +pub const SYS_msync = 144; +pub const SYS_readv = 145; +pub const SYS_writev = 146; +pub const SYS_getsid = 147; +pub const SYS_fdatasync = 148; +pub const SYS__sysctl = 149; +pub const SYS_mlock = 150; +pub const SYS_munlock = 151; +pub const SYS_mlockall = 152; +pub const SYS_munlockall = 153; +pub const SYS_sched_setparam = 154; +pub const SYS_sched_getparam = 155; +pub const SYS_sched_setscheduler = 156; +pub const SYS_sched_getscheduler = 157; +pub const SYS_sched_yield = 158; +pub const SYS_sched_get_priority_max = 159; +pub const SYS_sched_get_priority_min = 160; +pub const SYS_sched_rr_get_interval = 161; +pub const SYS_nanosleep = 162; +pub const SYS_mremap = 163; +pub const SYS_setresuid = 164; +pub const SYS_getresuid = 165; +pub const SYS_poll = 168; +pub const SYS_nfsservctl = 169; +pub const SYS_setresgid = 170; +pub const SYS_getresgid = 171; +pub const SYS_prctl = 172; +pub const SYS_rt_sigreturn = 173; +pub const SYS_rt_sigaction = 174; +pub const SYS_rt_sigprocmask = 175; +pub const SYS_rt_sigpending = 176; +pub const SYS_rt_sigtimedwait = 177; +pub const SYS_rt_sigqueueinfo = 178; +pub const SYS_rt_sigsuspend = 179; +pub const SYS_pread64 = 180; +pub const SYS_pwrite64 = 181; +pub const SYS_chown = 182; +pub const SYS_getcwd = 183; +pub const SYS_capget = 184; +pub const SYS_capset = 185; +pub const SYS_sigaltstack = 186; +pub const SYS_sendfile = 187; +pub const SYS_vfork = 190; +pub const SYS_ugetrlimit = 191; +pub const SYS_mmap2 = 192; +pub const SYS_truncate64 = 193; +pub const SYS_ftruncate64 = 194; +pub const SYS_stat64 = 195; +pub const SYS_lstat64 = 196; +pub const SYS_fstat64 = 197; +pub const SYS_lchown32 = 198; +pub const SYS_getuid32 = 199; +pub const SYS_getgid32 = 200; +pub const SYS_geteuid32 = 201; +pub const SYS_getegid32 = 202; +pub const SYS_setreuid32 = 203; +pub const SYS_setregid32 = 204; +pub const SYS_getgroups32 = 205; +pub const SYS_setgroups32 = 206; +pub const SYS_fchown32 = 207; +pub const SYS_setresuid32 = 208; +pub const SYS_getresuid32 = 209; +pub const SYS_setresgid32 = 210; +pub const SYS_getresgid32 = 211; +pub const SYS_chown32 = 212; +pub const SYS_setuid32 = 213; +pub const SYS_setgid32 = 214; +pub const SYS_setfsuid32 = 215; +pub const SYS_setfsgid32 = 216; +pub const SYS_getdents64 = 217; +pub const SYS_pivot_root = 218; +pub const SYS_mincore = 219; +pub const SYS_madvise = 220; +pub const SYS_fcntl64 = 221; +pub const SYS_gettid = 224; +pub const SYS_readahead = 225; +pub const SYS_setxattr = 226; +pub const SYS_lsetxattr = 227; +pub const SYS_fsetxattr = 228; +pub const SYS_getxattr = 229; +pub const SYS_lgetxattr = 230; +pub const SYS_fgetxattr = 231; +pub const SYS_listxattr = 232; +pub const SYS_llistxattr = 233; +pub const SYS_flistxattr = 234; +pub const SYS_removexattr = 235; +pub const SYS_lremovexattr = 236; +pub const SYS_fremovexattr = 237; +pub const SYS_tkill = 238; +pub const SYS_sendfile64 = 239; +pub const SYS_futex = 240; +pub const SYS_sched_setaffinity = 241; +pub const SYS_sched_getaffinity = 242; +pub const SYS_io_setup = 243; +pub const SYS_io_destroy = 244; +pub const SYS_io_getevents = 245; +pub const SYS_io_submit = 246; +pub const SYS_io_cancel = 247; +pub const SYS_exit_group = 248; +pub const SYS_lookup_dcookie = 249; +pub const SYS_epoll_create = 250; +pub const SYS_epoll_ctl = 251; +pub const SYS_epoll_wait = 252; +pub const SYS_remap_file_pages = 253; +pub const SYS_set_tid_address = 256; +pub const SYS_timer_create = 257; +pub const SYS_timer_settime = 258; +pub const SYS_timer_gettime = 259; +pub const SYS_timer_getoverrun = 260; +pub const SYS_timer_delete = 261; +pub const SYS_clock_settime = 262; +pub const SYS_clock_gettime = 263; +pub const SYS_clock_getres = 264; +pub const SYS_clock_nanosleep = 265; +pub const SYS_statfs64 = 266; +pub const SYS_fstatfs64 = 267; +pub const SYS_tgkill = 268; +pub const SYS_utimes = 269; +pub const SYS_arm_fadvise64_64 = 270; +pub const SYS_pciconfig_iobase = 271; +pub const SYS_pciconfig_read = 272; +pub const SYS_pciconfig_write = 273; +pub const SYS_mq_open = 274; +pub const SYS_mq_unlink = 275; +pub const SYS_mq_timedsend = 276; +pub const SYS_mq_timedreceive = 277; +pub const SYS_mq_notify = 278; +pub const SYS_mq_getsetattr = 279; +pub const SYS_waitid = 280; +pub const SYS_socket = 281; +pub const SYS_bind = 282; +pub const SYS_connect = 283; +pub const SYS_listen = 284; +pub const SYS_accept = 285; +pub const SYS_getsockname = 286; +pub const SYS_getpeername = 287; +pub const SYS_socketpair = 288; +pub const SYS_send = 289; +pub const SYS_sendto = 290; +pub const SYS_recv = 291; +pub const SYS_recvfrom = 292; +pub const SYS_shutdown = 293; +pub const SYS_setsockopt = 294; +pub const SYS_getsockopt = 295; +pub const SYS_sendmsg = 296; +pub const SYS_recvmsg = 297; +pub const SYS_semop = 298; +pub const SYS_semget = 299; +pub const SYS_semctl = 300; +pub const SYS_msgsnd = 301; +pub const SYS_msgrcv = 302; +pub const SYS_msgget = 303; +pub const SYS_msgctl = 304; +pub const SYS_shmat = 305; +pub const SYS_shmdt = 306; +pub const SYS_shmget = 307; +pub const SYS_shmctl = 308; +pub const SYS_add_key = 309; +pub const SYS_request_key = 310; +pub const SYS_keyctl = 311; +pub const SYS_semtimedop = 312; +pub const SYS_vserver = 313; +pub const SYS_ioprio_set = 314; +pub const SYS_ioprio_get = 315; +pub const SYS_inotify_init = 316; +pub const SYS_inotify_add_watch = 317; +pub const SYS_inotify_rm_watch = 318; +pub const SYS_mbind = 319; +pub const SYS_get_mempolicy = 320; +pub const SYS_set_mempolicy = 321; +pub const SYS_openat = 322; +pub const SYS_mkdirat = 323; +pub const SYS_mknodat = 324; +pub const SYS_fchownat = 325; +pub const SYS_futimesat = 326; +pub const SYS_fstatat64 = 327; +pub const SYS_unlinkat = 328; +pub const SYS_renameat = 329; +pub const SYS_linkat = 330; +pub const SYS_symlinkat = 331; +pub const SYS_readlinkat = 332; +pub const SYS_fchmodat = 333; +pub const SYS_faccessat = 334; +pub const SYS_pselect6 = 335; +pub const SYS_ppoll = 336; +pub const SYS_unshare = 337; +pub const SYS_set_robust_list = 338; +pub const SYS_get_robust_list = 339; +pub const SYS_splice = 340; +pub const SYS_arm_sync_file_range = 341; +pub const SYS_tee = 342; +pub const SYS_vmsplice = 343; +pub const SYS_move_pages = 344; +pub const SYS_getcpu = 345; +pub const SYS_epoll_pwait = 346; +pub const SYS_kexec_load = 347; +pub const SYS_utimensat = 348; +pub const SYS_signalfd = 349; +pub const SYS_timerfd_create = 350; +pub const SYS_eventfd = 351; +pub const SYS_fallocate = 352; +pub const SYS_timerfd_settime = 353; +pub const SYS_timerfd_gettime = 354; +pub const SYS_signalfd4 = 355; +pub const SYS_eventfd2 = 356; +pub const SYS_epoll_create1 = 357; +pub const SYS_dup3 = 358; +pub const SYS_pipe2 = 359; +pub const SYS_inotify_init1 = 360; +pub const SYS_preadv = 361; +pub const SYS_pwritev = 362; +pub const SYS_rt_tgsigqueueinfo = 363; +pub const SYS_perf_event_open = 364; +pub const SYS_recvmmsg = 365; +pub const SYS_accept4 = 366; +pub const SYS_fanotify_init = 367; +pub const SYS_fanotify_mark = 368; +pub const SYS_prlimit64 = 369; +pub const SYS_name_to_handle_at = 370; +pub const SYS_open_by_handle_at = 371; +pub const SYS_clock_adjtime = 372; +pub const SYS_syncfs = 373; +pub const SYS_sendmmsg = 374; +pub const SYS_setns = 375; +pub const SYS_process_vm_readv = 376; +pub const SYS_process_vm_writev = 377; +pub const SYS_kcmp = 378; +pub const SYS_finit_module = 379; +pub const SYS_sched_setattr = 380; +pub const SYS_sched_getattr = 381; +pub const SYS_renameat2 = 382; +pub const SYS_seccomp = 383; +pub const SYS_getrandom = 384; +pub const SYS_memfd_create = 385; +pub const SYS_bpf = 386; +pub const SYS_execveat = 387; +pub const SYS_userfaultfd = 388; +pub const SYS_membarrier = 389; +pub const SYS_mlock2 = 390; +pub const SYS_copy_file_range = 391; +pub const SYS_preadv2 = 392; +pub const SYS_pwritev2 = 393; +pub const SYS_pkey_mprotect = 394; +pub const SYS_pkey_alloc = 395; +pub const SYS_pkey_free = 396; +pub const SYS_statx = 397; +pub const SYS_rseq = 398; +pub const SYS_io_pgetevents = 399; + +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 = 0o40000; +pub const O_NOFOLLOW = 0o100000; +pub const O_CLOEXEC = 0o2000000; + +pub const O_ASYNC = 0o20000; +pub const O_DIRECT = 0o200000; +pub const O_LARGEFILE = 0o400000; +pub const O_NOATIME = 0o1000000; +pub const O_PATH = 0o10000000; +pub const O_TMPFILE = 0o20040000; +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 = 12; +pub const F_SETLK = 13; +pub const F_SETLKW = 14; + +pub const F_SETOWN_EX = 15; +pub const F_GETOWN_EX = 16; + +pub const F_GETOWNER_UIDS = 17; + +/// stack-like segment +pub const MAP_GROWSDOWN = 0x0100; + +/// ETXTBSY +pub const MAP_DENYWRITE = 0x0800; + +/// mark it as an executable +pub const MAP_EXECUTABLE = 0x1000; + +/// pages are locked +pub const MAP_LOCKED = 0x2000; + +/// don't check for reservations +pub const MAP_NORESERVE = 0x4000; + +/// populate (prefault) pagetables +pub const MAP_POPULATE = 0x8000; + +/// do not block on IO +pub const MAP_NONBLOCK = 0x10000; + +/// give out an address that is best suited for process/thread stacks +pub const MAP_STACK = 0x20000; + +/// create a huge page mapping +pub const MAP_HUGETLB = 0x40000; + +/// perform synchronous page faults for the mapping +pub const MAP_SYNC = 0x80000; + +pub const VDSO_USEFUL = true; +pub const VDSO_CGT_SYM = "__vdso_clock_gettime"; +pub const VDSO_CGT_VER = "LINUX_2.6"; + +pub const msghdr = extern struct { + msg_name: ?*sockaddr, + msg_namelen: socklen_t, + msg_iov: [*]iovec, + msg_iovlen: i32, + msg_control: ?*c_void, + msg_controllen: socklen_t, + msg_flags: i32, +}; + +pub const msghdr_const = extern struct { + msg_name: ?*const sockaddr, + msg_namelen: socklen_t, + msg_iov: [*]iovec_const, + msg_iovlen: i32, + msg_control: ?*c_void, + msg_controllen: socklen_t, + msg_flags: i32, +}; + +/// Renamed to Stat to not conflict with the stat function. +/// atime, mtime, and ctime have functions to return `timespec`, +/// because although this is a POSIX API, the layout and names of +/// the structs are inconsistent across operating systems, and +/// in C, macros are used to hide the differences. Here we use +/// methods to accomplish this. +pub const Stat = extern struct { + dev: u64, + __dev_patting: u32, + __ino_truncated: u32, + mode: u32, + nlink: u32, + uid: u32, + gid: u32, + rdev: u64, + __rdev_padding: u32, + size: i64, + blksize: i32, + blocks: i64, + atim: timespec, + mtim: timespec, + ctim: timespec, + ino: u64, + + pub fn atime(self: Stat) timespec { + return self.atim; + } + + pub fn mtime(self: Stat) timespec { + return self.mtim; + } + + pub fn ctime(self: Stat) timespec { + return self.ctim; + } +}; + +pub const timespec = extern struct { + tv_sec: i32, + tv_nsec: i32, +}; + +pub const timeval = extern struct { + tv_sec: i32, + tv_usec: i32, +}; + +pub const timezone = extern struct { + tz_minuteswest: i32, + tz_dsttime: i32, +}; From 50c37c75d1208bb8a0bf5b2792e4a1d69d6f7110 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 28 Aug 2019 17:41:49 +0200 Subject: [PATCH 02/16] add arm32 syscall conventions --- std/os/linux.zig | 1 + std/os/linux/arm-eabi.zig | 80 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 std/os/linux/arm-eabi.zig diff --git a/std/os/linux.zig b/std/os/linux.zig index 053f30d265..ba679387dd 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -17,6 +17,7 @@ pub const is_the_target = builtin.os == .linux; pub usingnamespace switch (builtin.arch) { .x86_64 => @import("linux/x86_64.zig"), .aarch64 => @import("linux/arm64.zig"), + .arm => @import("linux/arm-eabi.zig"), else => struct {}, }; pub usingnamespace @import("bits.zig"); diff --git a/std/os/linux/arm-eabi.zig b/std/os/linux/arm-eabi.zig new file mode 100644 index 0000000000..3b8eb055d5 --- /dev/null +++ b/std/os/linux/arm-eabi.zig @@ -0,0 +1,80 @@ +pub fn syscall0(number: usize) usize { + return asm volatile ("svc #0" + : [ret] "={r0}" (-> usize) + : [number] "{r7}" (number) + ); +} + +pub fn syscall1(number: usize, arg1: usize) usize { + return asm volatile ("svc #0" + : [ret] "={r0}" (-> usize) + : [number] "{r7}" (number), + [arg1] "{r0}" (arg1) + ); +} + +pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize { + return asm volatile ("svc #0" + : [ret] "={r0}" (-> usize) + : [number] "{r7}" (number), + [arg1] "{r0}" (arg1), + [arg2] "{r1}" (arg2) + ); +} + +pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { + return asm volatile ("svc #0" + : [ret] "={r0}" (-> usize) + : [number] "{r7}" (number), + [arg1] "{r0}" (arg1), + [arg2] "{r1}" (arg2), + [arg3] "{r2}" (arg3) + ); +} + +pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { + return asm volatile ("svc #0" + : [ret] "={r0}" (-> usize) + : [number] "{r7}" (number), + [arg1] "{r0}" (arg1), + [arg2] "{r1}" (arg2), + [arg3] "{r2}" (arg3), + [arg4] "{r3}" (arg4) + ); +} + +pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize { + return asm volatile ("svc #0" + : [ret] "={r0}" (-> usize) + : [number] "{r7}" (number), + [arg1] "{r0}" (arg1), + [arg2] "{r1}" (arg2), + [arg3] "{r2}" (arg3), + [arg4] "{r3}" (arg4), + [arg5] "{r4}" (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] "={r0}" (-> usize) + : [number] "{r7}" (number), + [arg1] "{r0}" (arg1), + [arg2] "{r1}" (arg2), + [arg3] "{r2}" (arg3), + [arg4] "{r3}" (arg4), + [arg5] "{r4}" (arg5), + [arg6] "{r5}" (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; From 223b773e038b7af6ee1dec1c69836c135049d1ba Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 29 Aug 2019 00:54:57 +0200 Subject: [PATCH 03/16] Add more syscall constants --- std/os/bits/linux/arm-eabi.zig | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/std/os/bits/linux/arm-eabi.zig b/std/os/bits/linux/arm-eabi.zig index ea4c452eb6..956e40a2ce 100644 --- a/std/os/bits/linux/arm-eabi.zig +++ b/std/os/bits/linux/arm-eabi.zig @@ -231,6 +231,7 @@ pub const SYS_statfs64 = 266; pub const SYS_fstatfs64 = 267; pub const SYS_tgkill = 268; pub const SYS_utimes = 269; +pub const SYS_fadvise64_64 = 270; pub const SYS_arm_fadvise64_64 = 270; pub const SYS_pciconfig_iobase = 271; pub const SYS_pciconfig_read = 272; @@ -302,6 +303,7 @@ pub const SYS_unshare = 337; pub const SYS_set_robust_list = 338; pub const SYS_get_robust_list = 339; pub const SYS_splice = 340; +pub const SYS_sync_file_range2 = 341; pub const SYS_arm_sync_file_range = 341; pub const SYS_tee = 342; pub const SYS_vmsplice = 343; @@ -361,6 +363,39 @@ pub const SYS_pkey_free = 396; pub const SYS_statx = 397; pub const SYS_rseq = 398; pub const SYS_io_pgetevents = 399; +pub const SYS_migrate_pages = 400; +pub const SYS_kexec_file_load = 401; +pub const SYS_clock_gettime64 = 403; +pub const SYS_clock_settime64 = 404; +pub const SYS_clock_adjtime64 = 405; +pub const SYS_clock_getres_time64 = 406; +pub const SYS_clock_nanosleep_time64 = 407; +pub const SYS_timer_gettime64 = 408; +pub const SYS_timer_settime64 = 409; +pub const SYS_timerfd_gettime64 = 410; +pub const SYS_timerfd_settime64 = 411; +pub const SYS_utimensat_time64 = 412; +pub const SYS_pselect6_time64 = 413; +pub const SYS_ppoll_time64 = 414; +pub const SYS_io_pgetevents_time64 = 416; +pub const SYS_recvmmsg_time64 = 417; +pub const SYS_mq_timedsend_time64 = 418; +pub const SYS_mq_timedreceive_time64 = 419; +pub const SYS_semtimedop_time64 = 420; +pub const SYS_rt_sigtimedwait_time64 = 421; +pub const SYS_futex_time64 = 422; +pub const SYS_sched_rr_get_interval_time64 = 423; +pub const SYS_pidfd_send_signal = 424; +pub const SYS_io_uring_setup = 425; +pub const SYS_io_uring_enter = 426; +pub const SYS_io_uring_register = 427; + +pub const SYS_breakpoint = 0x0f0001; +pub const SYS_cacheflush = 0x0f0002; +pub const SYS_usr26 = 0x0f0003; +pub const SYS_usr32 = 0x0f0004; +pub const SYS_set_tls = 0x0f0005; +pub const SYS_get_tls = 0x0f0006; pub const O_CREAT = 0o100; pub const O_EXCL = 0o200; From 57de61084e1a805838243e5428e774a591197454 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 29 Aug 2019 00:55:22 +0200 Subject: [PATCH 04/16] Make mmap use SYS_mmap2 if it exists --- std/os/bits/linux/arm-eabi.zig | 2 ++ std/os/linux.zig | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/std/os/bits/linux/arm-eabi.zig b/std/os/bits/linux/arm-eabi.zig index 956e40a2ce..1b45588459 100644 --- a/std/os/bits/linux/arm-eabi.zig +++ b/std/os/bits/linux/arm-eabi.zig @@ -397,6 +397,8 @@ pub const SYS_usr32 = 0x0f0004; pub const SYS_set_tls = 0x0f0005; pub const SYS_get_tls = 0x0f0006; +pub const MMAP2_UNIT = 4096; + pub const O_CREAT = 0o100; pub const O_EXCL = 0o200; pub const O_NOCTTY = 0o400; diff --git a/std/os/linux.zig b/std/os/linux.zig index ba679387dd..7d019001ef 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -190,7 +190,11 @@ pub fn umount2(special: [*]const u8, flags: u32) usize { } pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { - return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); + if (@hasDecl(@This(), "SYS_mmap2")) { + return syscall6(SYS_mmap2, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, @divTrunc(offset, MMAP2_UNIT))); + } else { + return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); + } } pub fn mprotect(address: [*]const u8, length: usize, protection: usize) usize { From b3ac323a448e1e8a816eab64777b855396afe6c8 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 29 Aug 2019 01:14:43 +0200 Subject: [PATCH 05/16] TLS initialization, clone definition and _start functionality --- std/os/linux/tls.zig | 3 +++ std/special/c.zig | 29 +++++++++++++++++++++++++++++ std/special/start.zig | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index a10d68663b..63c44d6982 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -118,6 +118,9 @@ pub fn setThreadPointer(addr: usize) void { : [addr] "r" (addr) ); }, + .arm => |arm| { + _ = std.os.linux.syscall1(std.os.linux.SYS_set_tls, addr); + }, else => @compileError("Unsupported architecture"), } } diff --git a/std/special/c.zig b/std/special/c.zig index 7300b8bf74..5ac7457429 100644 --- a/std/special/c.zig +++ b/std/special/c.zig @@ -238,6 +238,35 @@ nakedcc fn clone() void { \\ mov x8,#93 // SYS_exit \\ svc #0 ); + } else if (builtin.arch == builtin.Arch.arm) { + asm volatile ( + \\ stmfd sp!,{r4,r5,r6,r7} + \\ mov r7,#120 + \\ mov r6,r3 + \\ mov r5,r0 + \\ mov r0,r2 + \\ and r1,r1,#-16 + \\ ldr r2,[sp,#16] + \\ ldr r3,[sp,#20] + \\ ldr r4,[sp,#24] + \\ svc 0 + \\ tst r0,r0 + \\ beq 1f + \\ ldmfd sp!,{r4,r5,r6,r7} + \\ bx lr + \\ + \\1: mov r0,r6 + \\ tst r5,#1 + \\ bne 1f + \\ mov lr,pc + \\ mov pc,r5 + \\2: mov r7,#1 + \\ svc 0 + \\ + \\1: mov lr,pc + \\ bx r5 + \\ b 2b + ); } else { @compileError("Implement clone() for this arch."); } diff --git a/std/special/start.zig b/std/special/start.zig index 3427ff422d..fde79a4baf 100644 --- a/std/special/start.zig +++ b/std/special/start.zig @@ -44,7 +44,7 @@ nakedcc fn _start() noreturn { : [argc] "={esp}" (-> [*]usize) ); }, - .aarch64, .aarch64_be => { + .aarch64, .aarch64_be, .arm => { argc_ptr = asm ("mov %[argc], sp" : [argc] "=r" (-> [*]usize) ); From 108a51b11032ba3ed7864a653f87283853e6e318 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 29 Aug 2019 10:34:05 +0200 Subject: [PATCH 06/16] fix issues with debug.zig - Use sys_*stat*64 instead of sys_*stat* where appropriate - Fix overflow when calculating atime, ctime and mtime on File.stat() - Fix compilation error casting getEndPos to usize. --- std/debug.zig | 2 +- std/fs/file.zig | 6 +++--- std/os/bits/linux/arm-eabi.zig | 4 ++-- std/os/linux.zig | 24 ++++++++++++++++++++---- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/std/debug.zig b/std/debug.zig index d1c17343ef..a6ef60b663 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1053,7 +1053,7 @@ fn openSelfDebugInfoPosix(allocator: *mem.Allocator) !DwarfInfo { S.self_exe_file = try fs.openSelfExe(); errdefer S.self_exe_file.close(); - const self_exe_mmap_len = mem.alignForward(try S.self_exe_file.getEndPos(), mem.page_size); + const self_exe_mmap_len = mem.alignForward(@intCast(usize, try S.self_exe_file.getEndPos()), mem.page_size); const self_exe_mmap = try os.mmap( null, self_exe_mmap_len, diff --git a/std/fs/file.zig b/std/fs/file.zig index 0014693d40..83cbe23780 100644 --- a/std/fs/file.zig +++ b/std/fs/file.zig @@ -261,9 +261,9 @@ pub const File = struct { return Stat{ .size = @bitCast(u64, st.size), .mode = st.mode, - .atime = atime.tv_sec * std.time.ns_per_s + atime.tv_nsec, - .mtime = mtime.tv_sec * std.time.ns_per_s + mtime.tv_nsec, - .ctime = ctime.tv_sec * std.time.ns_per_s + ctime.tv_nsec, + .atime = @intCast(i64, atime.tv_sec) * std.time.ns_per_s + atime.tv_nsec, + .mtime = @intCast(i64, mtime.tv_sec) * std.time.ns_per_s + mtime.tv_nsec, + .ctime = @intCast(i64, ctime.tv_sec) * std.time.ns_per_s + ctime.tv_nsec, }; } diff --git a/std/os/bits/linux/arm-eabi.zig b/std/os/bits/linux/arm-eabi.zig index 1b45588459..5048a2afc7 100644 --- a/std/os/bits/linux/arm-eabi.zig +++ b/std/os/bits/linux/arm-eabi.zig @@ -502,7 +502,7 @@ pub const msghdr_const = extern struct { /// methods to accomplish this. pub const Stat = extern struct { dev: u64, - __dev_patting: u32, + __dev_padding: u32, __ino_truncated: u32, mode: u32, nlink: u32, @@ -512,7 +512,7 @@ pub const Stat = extern struct { __rdev_padding: u32, size: i64, blksize: i32, - blocks: i64, + blocks: u64, atim: timespec, mtim: timespec, ctim: timespec, diff --git a/std/os/linux.zig b/std/os/linux.zig index 7d019001ef..3134ebf641 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -713,22 +713,38 @@ pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: } pub fn fstat(fd: i32, stat_buf: *Stat) usize { - return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); + if (@hasDecl(@This(), "SYS_fstat64")) { + return syscall2(SYS_fstat64, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); + } else { + return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); + } } // 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)); + if (@hasDecl(@This(), "SYS_stat64")) { + return syscall2(SYS_stat64, @ptrToInt(pathname), @ptrToInt(statbuf)); + } else { + return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); + } } // 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)); + if (@hasDecl(@This(), "SYS_lstat64")) { + return syscall2(SYS_lstat64, @ptrToInt(pathname), @ptrToInt(statbuf)); + } else { + return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); + } } // 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, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); + if (@hasDecl(@This(), "SYS_fstatat64")) { + return syscall4(SYS_fstatat64, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); + } else { + return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); + } } // TODO https://github.com/ziglang/zig/issues/265 From e4c262b804dded7e47aea0bb61f4578b94a7fa86 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 29 Aug 2019 10:52:50 +0200 Subject: [PATCH 07/16] Don't print line info if source is not available --- std/debug.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/debug.zig b/std/debug.zig index a6ef60b663..8918426e47 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -793,7 +793,7 @@ fn printLineInfo( try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); } } else |err| switch (err) { - error.EndOfFile => {}, + error.EndOfFile, error.FileNotFound => {}, else => return err, } } else { From 2323da3a680f48207d35db26f63ea0553e74d956 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 29 Aug 2019 18:12:58 +0200 Subject: [PATCH 08/16] add __aeabi_read_tp --- std/os/bits/linux/arm-eabi.zig | 24 ++++++++++++++++++++++++ std/os/linux/arm-eabi.zig | 8 ++++++++ std/os/linux/tls.zig | 12 ++++++++++++ std/special/c.zig | 2 ++ 4 files changed, 46 insertions(+) diff --git a/std/os/bits/linux/arm-eabi.zig b/std/os/bits/linux/arm-eabi.zig index 5048a2afc7..ca80b67fe1 100644 --- a/std/os/bits/linux/arm-eabi.zig +++ b/std/os/bits/linux/arm-eabi.zig @@ -474,6 +474,30 @@ pub const VDSO_USEFUL = true; pub const VDSO_CGT_SYM = "__vdso_clock_gettime"; pub const VDSO_CGT_VER = "LINUX_2.6"; +pub const HWCAP_SWP = 1 << 0; +pub const HWCAP_HALF = 1 << 1; +pub const HWCAP_THUMB = 1 << 2; +pub const HWCAP_26BIT = 1 << 3; +pub const HWCAP_FAST_MULT = 1 << 4; +pub const HWCAP_FPA = 1 << 5; +pub const HWCAP_VFP = 1 << 6; +pub const HWCAP_EDSP = 1 << 7; +pub const HWCAP_JAVA = 1 << 8; +pub const HWCAP_IWMMXT = 1 << 9; +pub const HWCAP_CRUNCH = 1 << 10; +pub const HWCAP_THUMBEE = 1 << 11; +pub const HWCAP_NEON = 1 << 12; +pub const HWCAP_VFPv3 = 1 << 13; +pub const HWCAP_VFPv3D16 = 1 << 14; +pub const HWCAP_TLS = 1 << 15; +pub const HWCAP_VFPv4 = 1 << 16; +pub const HWCAP_IDIVA = 1 << 17; +pub const HWCAP_IDIVT = 1 << 18; +pub const HWCAP_VFPD32 = 1 << 19; +pub const HWCAP_IDIV = HWCAP_IDIVA | HWCAP_IDIVT; +pub const HWCAP_LPAE = 1 << 20; +pub const HWCAP_EVTSTRM = 1 << 21; + pub const msghdr = extern struct { msg_name: ?*sockaddr, msg_namelen: socklen_t, diff --git a/std/os/linux/arm-eabi.zig b/std/os/linux/arm-eabi.zig index 3b8eb055d5..a8cb52d711 100644 --- a/std/os/linux/arm-eabi.zig +++ b/std/os/linux/arm-eabi.zig @@ -78,3 +78,11 @@ pub fn syscall6( /// 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; + +// LLVM calls this when the read-tp-hard feature is set to false. Currently, there is no way to pass +// that to llvm via zig. See https://github.com/ziglang/zig/issues/2883 +pub nakedcc fn getThreadPointer() usize { + return asm volatile("mrc p15, 0, %[ret], c13, c0, 3" + : [ret] "=r" (-> usize) + ); +} \ No newline at end of file diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index 63c44d6982..a14b26a5cf 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -133,6 +133,7 @@ pub fn initTLS() void { var at_phent: usize = undefined; var at_phnum: usize = undefined; var at_phdr: usize = undefined; + var at_hwcap: usize = undefined; var i: usize = 0; while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { @@ -140,10 +141,21 @@ pub fn initTLS() void { elf.AT_PHENT => at_phent = auxv[i].a_un.a_val, elf.AT_PHNUM => at_phnum = auxv[i].a_un.a_val, elf.AT_PHDR => at_phdr = auxv[i].a_un.a_val, + elf.AT_HWCAP => at_phdr = auxv[i].a_un.a_val, else => continue, } } + // If the cpu is arm-based, check if it supports the TLS register + if (builtin.arch == builtin.Arch.arm and builtin.os == .linux) { + if (at_hwcap & std.os.linux.HWCAP_TLS == 0) { + // If the CPU does not support TLS via a coprocessor register, + // it could be accessed via a kernel helper function + // see musl/src/thread/arm/ for details + @panic("cpu does not support TLS via coprocessor register"); + } + } + // Sanity check assert(at_phent == @sizeOf(elf.Phdr)); diff --git a/std/special/c.zig b/std/special/c.zig index 5ac7457429..669a771c2e 100644 --- a/std/special/c.zig +++ b/std/special/c.zig @@ -25,6 +25,8 @@ comptime { @export("strncmp", strncmp, .Strong); @export("strerror", strerror, .Strong); @export("strlen", strlen, .Strong); + } else if (builtin.arch == builtin.Arch.arm and builtin.os == .linux) { + @export("__aeabi_read_tp", std.os.linux.getThreadPointer, .Strong); } } From c1f8b201a1419e1f86c116c67de2f5a8c58b9a84 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Fri, 30 Aug 2019 12:58:44 +0200 Subject: [PATCH 09/16] Improve comments near un-implemented functionality --- std/os/linux/arm-eabi.zig | 3 ++- std/os/linux/tls.zig | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/std/os/linux/arm-eabi.zig b/std/os/linux/arm-eabi.zig index a8cb52d711..1167c10f0b 100644 --- a/std/os/linux/arm-eabi.zig +++ b/std/os/linux/arm-eabi.zig @@ -80,7 +80,8 @@ pub fn syscall6( pub extern fn clone(func: extern fn (arg: usize) u8, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize; // LLVM calls this when the read-tp-hard feature is set to false. Currently, there is no way to pass -// that to llvm via zig. See https://github.com/ziglang/zig/issues/2883 +// that to llvm via zig, see https://github.com/ziglang/zig/issues/2883. +// LLVM expects libc to provide this function as __aeabi_read_tp, so it is exported if needed from special/c.zig. pub nakedcc fn getThreadPointer() usize { return asm volatile("mrc p15, 0, %[ret], c13, c0, 3" : [ret] "=r" (-> usize) diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index a14b26a5cf..1b2687b82c 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -150,9 +150,9 @@ pub fn initTLS() void { if (builtin.arch == builtin.Arch.arm and builtin.os == .linux) { if (at_hwcap & std.os.linux.HWCAP_TLS == 0) { // If the CPU does not support TLS via a coprocessor register, - // it could be accessed via a kernel helper function - // see musl/src/thread/arm/ for details - @panic("cpu does not support TLS via coprocessor register"); + // a kernel helper function can be used instead on certain linux kernels. + // See linux/arch/arm/include/asm/tls.h and musl/src/thread/arm/__set_thread_area.c. + @panic("TODO: Implement ARM fallback TLS functionality"); } } From 4b8325f3815c7fa774bb06ef5d190f039723222b Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Fri, 30 Aug 2019 13:02:15 +0200 Subject: [PATCH 10/16] Remove unneeded os check --- std/os/linux/tls.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index 1b2687b82c..8920e28a5b 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -147,7 +147,7 @@ pub fn initTLS() void { } // If the cpu is arm-based, check if it supports the TLS register - if (builtin.arch == builtin.Arch.arm and builtin.os == .linux) { + if (builtin.arch == builtin.Arch.arm) { if (at_hwcap & std.os.linux.HWCAP_TLS == 0) { // If the CPU does not support TLS via a coprocessor register, // a kernel helper function can be used instead on certain linux kernels. From e39c93a2f346e4bfb2653ccf231d736dfb9bce6e Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sun, 1 Sep 2019 11:54:57 +0200 Subject: [PATCH 11/16] Replace legacy 16-bit syscalls with 32-bit versions when appropriate --- std/os/linux.zig | 84 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 14 deletions(-) diff --git a/std/os/linux.zig b/std/os/linux.zig index 3134ebf641..e24ba441cc 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -486,35 +486,67 @@ pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { } pub fn setuid(uid: u32) usize { - return syscall1(SYS_setuid, uid); + if (@hasDecl(@This(), "SYS_setuid32")) { + return syscall1(SYS_setuid32, uid); + } else { + return syscall1(SYS_setuid, uid); + } } pub fn setgid(gid: u32) usize { - return syscall1(SYS_setgid, gid); + if (@hasDecl(@This(), "SYS_setgid32")) { + return syscall1(SYS_setgid32, gid); + } else { + return syscall1(SYS_setgid, gid); + } } pub fn setreuid(ruid: u32, euid: u32) usize { - return syscall2(SYS_setreuid, ruid, euid); + if (@hasDecl(@This(), "SYS_setreuid32")) { + return syscall2(SYS_setreuid32, ruid, euid)); + } else { + return syscall2(SYS_setreuid, ruid, euid)); + } } pub fn setregid(rgid: u32, egid: u32) usize { - return syscall2(SYS_setregid, rgid, egid); + if (@hasDecl(@This(), "SYS_setregid32")) { + return syscall2(SYS_setregid32, rgid, egid); + } else { + return syscall2(SYS_setregid, rgid, egid); + } } pub fn getuid() u32 { - return u32(syscall0(SYS_getuid)); + if (@hasDecl(@This(), "SYS_getuid32")) { + return u32(syscall0(SYS_getuid32)); + } else { + return u32(syscall0(SYS_getuid)); + } } pub fn getgid() u32 { - return u32(syscall0(SYS_getgid)); + if (@hasDecl(@This(), "SYS_getgid32")) { + return u32(syscall0(SYS_getgid32)); + } else { + return u32(syscall0(SYS_getgid)); + } } pub fn geteuid() u32 { - return u32(syscall0(SYS_geteuid)); + if (@hasDecl(@This(), "SYS_geteuid32")) { + return u32(syscall0(SYS_geteuid32)); + } else { + return u32(syscall0(SYS_geteuid)); + } } pub fn getegid() u32 { - return u32(syscall0(SYS_getegid)); + if (@hasDecl(@This(), "SYS_getegid32")) { + return u32(syscall0(SYS_getegid32)); + } else { + return u32(syscall0(SYS_getegid)); + } } pub fn seteuid(euid: u32) usize { @@ -526,27 +558,51 @@ pub fn setegid(egid: u32) usize { } pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { - return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); + if (@hasDecl(@This(), "SYS_getresuid32")) { + return syscall3(SYS_getresuid32, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); + } else { + return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); + } } pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize { - return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); + if (@hasDecl(@This(), "SYS_getresgid32")) { + return syscall3(SYS_getresgid32, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); + } else { + return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); + } } pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize { - return syscall3(SYS_setresuid, ruid, euid, suid); + if (@hasDecl(@This(), "SYS_setresuid32")) { + return syscall3(SYS_setresuid32, ruid, euid, suid); + } else { + return syscall3(SYS_setresuid, ruid, euid, suid); + } } pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize { - return syscall3(SYS_setresgid, rgid, egid, sgid); + if (@hasDecl(@This(), "SYS_setresgid32")) { + return syscall3(SYS_setresgid32, rgid, egid, sgid); + } else { + return syscall3(SYS_setresgid, rgid, egid, sgid); + } } pub fn getgroups(size: usize, list: *u32) usize { - return syscall2(SYS_getgroups, size, @ptrToInt(list)); + if (@hasDecl(@This(), "SYS_getgroups32")) { + return syscall2(SYS_getgroups32, size, @ptrToInt(list)); + } else { + return syscall2(SYS_getgroups, size, @ptrToInt(list)); + } } pub fn setgroups(size: usize, list: *const u32) usize { - return syscall2(SYS_setgroups, size, @ptrToInt(list)); + if (@hasDecl(@This(), "SYS_setgroups32")) { + return syscall2(SYS_setgroups32, size, @ptrToInt(list)); + } else { + return syscall2(SYS_setgroups, size, @ptrToInt(list)); + } } pub fn getpid() i32 { From e9d795b025e55dcd9d8c4378141b996358634863 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sun, 1 Sep 2019 12:08:02 +0200 Subject: [PATCH 12/16] Fix up seteuid and setegid --- std/os/linux.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/std/os/linux.zig b/std/os/linux.zig index e24ba441cc..7bb59dde41 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -550,11 +550,11 @@ pub fn getegid() u32 { } pub fn seteuid(euid: u32) usize { - return syscall1(SYS_seteuid, euid); + return setreuid(std.math.maxInt(u32), euid); } pub fn setegid(egid: u32) usize { - return syscall1(SYS_setegid, egid); + return setregid(std.math.maxInt(u32), egid); } pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { From e7912dee9bd63b03415f441b4de9b3babc79c859 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sun, 1 Sep 2019 16:10:36 +0200 Subject: [PATCH 13/16] Fix up preadv, preadv2, pwritev and pwritev2 --- std/os/linux.zig | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/std/os/linux.zig b/std/os/linux.zig index 7bb59dde41..58c402f829 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -210,11 +210,26 @@ pub fn read(fd: i32, buf: [*]u8, count: usize) usize { } pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { - return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); + return syscall5( + SYS_preadv, + @bitCast(usize, isize(fd)), + @ptrToInt(iov), + count, + @truncate(usize, offset), + @truncate(usize, offset >> 32), + ); } pub fn preadv2(fd: i32, iov: [*]const iovec, count: usize, offset: u64, flags: kernel_rwf) usize { - return syscall5(SYS_preadv2, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset, flags); + return syscall6( + SYS_preadv2, + @bitCast(usize, isize(fd)), + @ptrToInt(iov), + count, + @truncate(usize, offset), + @truncate(usize, offset >> 32), + flags + ); } pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { @@ -226,11 +241,26 @@ pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { } pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { - return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); + return syscall5( + SYS_pwritev, + @bitCast(usize, isize(fd)), + @ptrToInt(iov), + count, + @truncate(usize, offset), + @truncate(usize, offset >> 32), + ); } pub fn pwritev2(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64, flags: kernel_rwf) usize { - return syscall5(SYS_pwritev2, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset, flags); + return syscall6( + SYS_pwritev2, + @bitCast(usize, isize(fd)), + @ptrToInt(iov), + count, + @truncate(usize, offset), + @truncate(usize, offset >> 32), + flags + ); } // TODO https://github.com/ziglang/zig/issues/265 @@ -503,9 +533,9 @@ pub fn setgid(gid: u32) usize { pub fn setreuid(ruid: u32, euid: u32) usize { if (@hasDecl(@This(), "SYS_setreuid32")) { - return syscall2(SYS_setreuid32, ruid, euid)); + return syscall2(SYS_setreuid32, ruid, euid); } else { - return syscall2(SYS_setreuid, ruid, euid)); + return syscall2(SYS_setreuid, ruid, euid); } } From 6a76298740344ba6d82c33b19914238a1233eaa0 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 4 Sep 2019 15:59:51 +0200 Subject: [PATCH 14/16] Add missing clobbers on arm-eabi and arm64 syscall conventions --- std/os/linux/arm-eabi.zig | 7 +++++++ std/os/linux/arm64.zig | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/std/os/linux/arm-eabi.zig b/std/os/linux/arm-eabi.zig index 1167c10f0b..a0a00dce8b 100644 --- a/std/os/linux/arm-eabi.zig +++ b/std/os/linux/arm-eabi.zig @@ -2,6 +2,7 @@ pub fn syscall0(number: usize) usize { return asm volatile ("svc #0" : [ret] "={r0}" (-> usize) : [number] "{r7}" (number) + : "memory" ); } @@ -10,6 +11,7 @@ pub fn syscall1(number: usize, arg1: usize) usize { : [ret] "={r0}" (-> usize) : [number] "{r7}" (number), [arg1] "{r0}" (arg1) + : "memory" ); } @@ -19,6 +21,7 @@ pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize { : [number] "{r7}" (number), [arg1] "{r0}" (arg1), [arg2] "{r1}" (arg2) + : "memory" ); } @@ -29,6 +32,7 @@ pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { [arg1] "{r0}" (arg1), [arg2] "{r1}" (arg2), [arg3] "{r2}" (arg3) + : "memory" ); } @@ -40,6 +44,7 @@ pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usiz [arg2] "{r1}" (arg2), [arg3] "{r2}" (arg3), [arg4] "{r3}" (arg4) + : "memory" ); } @@ -52,6 +57,7 @@ pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usiz [arg3] "{r2}" (arg3), [arg4] "{r3}" (arg4), [arg5] "{r4}" (arg5) + : "memory" ); } @@ -73,6 +79,7 @@ pub fn syscall6( [arg4] "{r3}" (arg4), [arg5] "{r4}" (arg5), [arg6] "{r5}" (arg6) + : "memory" ); } diff --git a/std/os/linux/arm64.zig b/std/os/linux/arm64.zig index 94cf8818de..28da9af1c6 100644 --- a/std/os/linux/arm64.zig +++ b/std/os/linux/arm64.zig @@ -2,6 +2,7 @@ pub fn syscall0(number: usize) usize { return asm volatile ("svc #0" : [ret] "={x0}" (-> usize) : [number] "{x8}" (number) + : "memory", "cc" ); } @@ -10,6 +11,7 @@ pub fn syscall1(number: usize, arg1: usize) usize { : [ret] "={x0}" (-> usize) : [number] "{x8}" (number), [arg1] "{x0}" (arg1) + : "memory", "cc" ); } @@ -19,6 +21,7 @@ pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize { : [number] "{x8}" (number), [arg1] "{x0}" (arg1), [arg2] "{x1}" (arg2) + : "memory", "cc" ); } @@ -29,6 +32,7 @@ pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { [arg1] "{x0}" (arg1), [arg2] "{x1}" (arg2), [arg3] "{x2}" (arg3) + : "memory", "cc" ); } @@ -40,6 +44,7 @@ pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usiz [arg2] "{x1}" (arg2), [arg3] "{x2}" (arg3), [arg4] "{x3}" (arg4) + : "memory", "cc" ); } @@ -52,6 +57,7 @@ pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usiz [arg3] "{x2}" (arg3), [arg4] "{x3}" (arg4), [arg5] "{x4}" (arg5) + : "memory", "cc" ); } @@ -73,6 +79,7 @@ pub fn syscall6( [arg4] "{x3}" (arg4), [arg5] "{x4}" (arg5), [arg6] "{x5}" (arg6) + : "memory", "cc" ); } From 77d04c03e3f20c4c60d98e73b876222006aa05fc Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 4 Sep 2019 16:23:25 +0200 Subject: [PATCH 15/16] Implement remaining requested changes - Replace @intCast with a checked version (std/debug.zig) - Replace @intCast with i64() when casting from a smaller type (std/fs/file.zig) - Replace `nakedcc` with appropriate calling convention for linking with c (std/os/linux/arm-eabi.zig) - Only check if hwcap contains TLS when the hwcap field actually exists (std/os/linux/tls.zig) --- std/debug.zig | 3 ++- std/fs/file.zig | 6 +++--- std/os/linux/arm-eabi.zig | 2 +- std/os/linux/tls.zig | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/std/debug.zig b/std/debug.zig index 8918426e47..68e6220a72 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1053,7 +1053,8 @@ fn openSelfDebugInfoPosix(allocator: *mem.Allocator) !DwarfInfo { S.self_exe_file = try fs.openSelfExe(); errdefer S.self_exe_file.close(); - const self_exe_mmap_len = mem.alignForward(@intCast(usize, try S.self_exe_file.getEndPos()), mem.page_size); + const self_exe_len = math.cast(usize, try S.self_exe_file.getEndPos()) catch return error.DebugInfoTooLarge; + const self_exe_mmap_len = mem.alignForward(self_exe_len, mem.page_size); const self_exe_mmap = try os.mmap( null, self_exe_mmap_len, diff --git a/std/fs/file.zig b/std/fs/file.zig index 83cbe23780..5ecad01026 100644 --- a/std/fs/file.zig +++ b/std/fs/file.zig @@ -261,9 +261,9 @@ pub const File = struct { return Stat{ .size = @bitCast(u64, st.size), .mode = st.mode, - .atime = @intCast(i64, atime.tv_sec) * std.time.ns_per_s + atime.tv_nsec, - .mtime = @intCast(i64, mtime.tv_sec) * std.time.ns_per_s + mtime.tv_nsec, - .ctime = @intCast(i64, ctime.tv_sec) * std.time.ns_per_s + ctime.tv_nsec, + .atime = i64(atime.tv_sec) * std.time.ns_per_s + atime.tv_nsec, + .mtime = i64(mtime.tv_sec) * std.time.ns_per_s + mtime.tv_nsec, + .ctime = i64(ctime.tv_sec) * std.time.ns_per_s + ctime.tv_nsec, }; } diff --git a/std/os/linux/arm-eabi.zig b/std/os/linux/arm-eabi.zig index a0a00dce8b..c8a5bbe378 100644 --- a/std/os/linux/arm-eabi.zig +++ b/std/os/linux/arm-eabi.zig @@ -89,7 +89,7 @@ pub extern fn clone(func: extern fn (arg: usize) u8, stack: usize, flags: u32, a // LLVM calls this when the read-tp-hard feature is set to false. Currently, there is no way to pass // that to llvm via zig, see https://github.com/ziglang/zig/issues/2883. // LLVM expects libc to provide this function as __aeabi_read_tp, so it is exported if needed from special/c.zig. -pub nakedcc fn getThreadPointer() usize { +pub extern fn getThreadPointer() usize { return asm volatile("mrc p15, 0, %[ret], c13, c0, 3" : [ret] "=r" (-> usize) ); diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index 8920e28a5b..2ed5f1d75b 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -133,7 +133,7 @@ pub fn initTLS() void { var at_phent: usize = undefined; var at_phnum: usize = undefined; var at_phdr: usize = undefined; - var at_hwcap: usize = undefined; + var at_hwcap: ?usize = null; var i: usize = 0; while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { @@ -147,8 +147,8 @@ pub fn initTLS() void { } // If the cpu is arm-based, check if it supports the TLS register - if (builtin.arch == builtin.Arch.arm) { - if (at_hwcap & std.os.linux.HWCAP_TLS == 0) { + if (at_hwcap) |hwcap| { + if (builtin.arch == builtin.Arch.arm and hwcap & std.os.linux.HWCAP_TLS == 0) { // If the CPU does not support TLS via a coprocessor register, // a kernel helper function can be used instead on certain linux kernels. // See linux/arch/arm/include/asm/tls.h and musl/src/thread/arm/__set_thread_area.c. From df06976e73e37a36009fb5f43c2310ad610a4a2e Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Wed, 4 Sep 2019 17:48:01 +0200 Subject: [PATCH 16/16] Only check for TLS support on arm if TLS segment exists --- std/os/linux/tls.zig | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index 2ed5f1d75b..3c55013f47 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -133,7 +133,7 @@ pub fn initTLS() void { var at_phent: usize = undefined; var at_phnum: usize = undefined; var at_phdr: usize = undefined; - var at_hwcap: ?usize = null; + var at_hwcap: usize = undefined; var i: usize = 0; while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { @@ -146,16 +146,6 @@ pub fn initTLS() void { } } - // If the cpu is arm-based, check if it supports the TLS register - if (at_hwcap) |hwcap| { - if (builtin.arch == builtin.Arch.arm and hwcap & std.os.linux.HWCAP_TLS == 0) { - // If the CPU does not support TLS via a coprocessor register, - // a kernel helper function can be used instead on certain linux kernels. - // See linux/arch/arm/include/asm/tls.h and musl/src/thread/arm/__set_thread_area.c. - @panic("TODO: Implement ARM fallback TLS functionality"); - } - } - // Sanity check assert(at_phent == @sizeOf(elf.Phdr)); @@ -171,6 +161,14 @@ pub fn initTLS() void { } if (tls_phdr) |phdr| { + // If the cpu is arm-based, check if it supports the TLS register + if (builtin.arch == builtin.Arch.arm and hwcap & std.os.linux.HWCAP_TLS == 0) { + // If the CPU does not support TLS via a coprocessor register, + // a kernel helper function can be used instead on certain linux kernels. + // See linux/arch/arm/include/asm/tls.h and musl/src/thread/arm/__set_thread_area.c. + @panic("TODO: Implement ARM fallback TLS functionality"); + } + // Offsets into the allocated TLS area var tcb_offset: usize = undefined; var dtv_offset: usize = undefined;