From 52330d35289ae40c29f4b1cdde032210037083c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sat, 22 Jun 2024 16:11:52 +0200 Subject: [PATCH] std.os.linux: Add riscv32 support. --- lib/std/os/linux.zig | 31 ++++- lib/std/os/linux/riscv32.zig | 227 +++++++++++++++++++++++++++++++++++ lib/std/os/linux/riscv64.zig | 10 +- 3 files changed, 257 insertions(+), 11 deletions(-) create mode 100644 lib/std/os/linux/riscv32.zig diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 2104e8f02089..c9b8a7a4f755 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -38,6 +38,7 @@ const arch_bits = switch (native_arch) { .x86_64 => @import("linux/x86_64.zig"), .aarch64, .aarch64_be => @import("linux/arm64.zig"), .arm, .thumb => @import("linux/arm-eabi.zig"), + .riscv32 => @import("linux/riscv32.zig"), .riscv64 => @import("linux/riscv64.zig"), .sparc64 => @import("linux/sparc64.zig"), .mips, .mipsel => @import("linux/mips.zig"), @@ -101,6 +102,7 @@ pub const SYS = switch (@import("builtin").cpu.arch) { .x86_64 => syscalls.X64, .aarch64, .aarch64_be => syscalls.Arm64, .arm, .thumb => syscalls.Arm, + .riscv32 => syscalls.RiscV32, .riscv64 => syscalls.RiscV64, .sparc64 => syscalls.Sparc64, .mips, .mipsel => syscalls.Mips, @@ -160,7 +162,7 @@ pub const MAP = switch (native_arch) { UNINITIALIZED: bool = false, _: u5 = 0, }, - .riscv64 => packed struct(u32) { + .riscv32, .riscv64 => packed struct(u32) { TYPE: MAP_TYPE, FIXED: bool = false, ANONYMOUS: bool = false, @@ -265,7 +267,7 @@ pub const O = switch (native_arch) { TMPFILE: bool = false, _: u9 = 0, }, - .x86, .riscv64 => packed struct(u32) { + .x86, .riscv32, .riscv64 => packed struct(u32) { ACCMODE: ACCMODE = .RDONLY, _2: u4 = 0, CREAT: bool = false, @@ -1787,7 +1789,11 @@ pub fn accept4(fd: i32, noalias addr: ?*sockaddr, noalias len: ?*socklen_t, flag } pub fn fstat(fd: i32, stat_buf: *Stat) usize { - if (@hasField(SYS, "fstat64")) { + if (native_arch == .riscv32) { + // riscv32 has made the interesting decision to not implement some of + // the older stat syscalls, including this one. + @compileError("No fstat syscall on this architecture."); + } else if (@hasField(SYS, "fstat64")) { return syscall2(.fstat64, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(stat_buf)); } else { return syscall2(.fstat, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(stat_buf)); @@ -1795,7 +1801,11 @@ pub fn fstat(fd: i32, stat_buf: *Stat) usize { } pub fn stat(pathname: [*:0]const u8, statbuf: *Stat) usize { - if (@hasField(SYS, "stat64")) { + if (native_arch == .riscv32) { + // riscv32 has made the interesting decision to not implement some of + // the older stat syscalls, including this one. + @compileError("No stat syscall on this architecture."); + } else if (@hasField(SYS, "stat64")) { return syscall2(.stat64, @intFromPtr(pathname), @intFromPtr(statbuf)); } else { return syscall2(.stat, @intFromPtr(pathname), @intFromPtr(statbuf)); @@ -1803,7 +1813,11 @@ pub fn stat(pathname: [*:0]const u8, statbuf: *Stat) usize { } pub fn lstat(pathname: [*:0]const u8, statbuf: *Stat) usize { - if (@hasField(SYS, "lstat64")) { + if (native_arch == .riscv32) { + // riscv32 has made the interesting decision to not implement some of + // the older stat syscalls, including this one. + @compileError("No lstat syscall on this architecture."); + } else if (@hasField(SYS, "lstat64")) { return syscall2(.lstat64, @intFromPtr(pathname), @intFromPtr(statbuf)); } else { return syscall2(.lstat, @intFromPtr(pathname), @intFromPtr(statbuf)); @@ -1811,7 +1825,11 @@ pub fn lstat(pathname: [*:0]const u8, statbuf: *Stat) usize { } pub fn fstatat(dirfd: i32, path: [*:0]const u8, stat_buf: *Stat, flags: u32) usize { - if (@hasField(SYS, "fstatat64")) { + if (native_arch == .riscv32) { + // riscv32 has made the interesting decision to not implement some of + // the older stat syscalls, including this one. + @compileError("No fstatat syscall on this architecture."); + } else if (@hasField(SYS, "fstatat64")) { return syscall4(.fstatat64, @as(usize, @bitCast(@as(isize, dirfd))), @intFromPtr(path), @intFromPtr(stat_buf), flags); } else { return syscall4(.fstatat, @as(usize, @bitCast(@as(isize, dirfd))), @intFromPtr(path), @intFromPtr(stat_buf), flags); @@ -7135,6 +7153,7 @@ pub const AUDIT = struct { .x86_64 => .X86_64, .aarch64 => .AARCH64, .arm, .thumb => .ARM, + .riscv32 => .RISCV32, .riscv64 => .RISCV64, .sparc64 => .SPARC64, .mips => .MIPS, diff --git a/lib/std/os/linux/riscv32.zig b/lib/std/os/linux/riscv32.zig new file mode 100644 index 000000000000..59e15eaef7a6 --- /dev/null +++ b/lib/std/os/linux/riscv32.zig @@ -0,0 +1,227 @@ +const std = @import("../../std.zig"); +const iovec = std.posix.iovec; +const iovec_const = std.posix.iovec_const; +const linux = std.os.linux; +const SYS = linux.SYS; +const uid_t = std.os.linux.uid_t; +const gid_t = std.os.linux.gid_t; +const pid_t = std.os.linux.pid_t; +const sockaddr = linux.sockaddr; +const socklen_t = linux.socklen_t; +const timespec = std.os.linux.timespec; + +pub fn syscall0(number: SYS) usize { + return asm volatile ("ecall" + : [ret] "={x10}" (-> usize), + : [number] "{x17}" (@intFromEnum(number)), + : "memory" + ); +} + +pub fn syscall1(number: SYS, arg1: usize) usize { + return asm volatile ("ecall" + : [ret] "={x10}" (-> usize), + : [number] "{x17}" (@intFromEnum(number)), + [arg1] "{x10}" (arg1), + : "memory" + ); +} + +pub fn syscall2(number: SYS, arg1: usize, arg2: usize) usize { + return asm volatile ("ecall" + : [ret] "={x10}" (-> usize), + : [number] "{x17}" (@intFromEnum(number)), + [arg1] "{x10}" (arg1), + [arg2] "{x11}" (arg2), + : "memory" + ); +} + +pub fn syscall3(number: SYS, arg1: usize, arg2: usize, arg3: usize) usize { + return asm volatile ("ecall" + : [ret] "={x10}" (-> usize), + : [number] "{x17}" (@intFromEnum(number)), + [arg1] "{x10}" (arg1), + [arg2] "{x11}" (arg2), + [arg3] "{x12}" (arg3), + : "memory" + ); +} + +pub fn syscall4(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { + return asm volatile ("ecall" + : [ret] "={x10}" (-> usize), + : [number] "{x17}" (@intFromEnum(number)), + [arg1] "{x10}" (arg1), + [arg2] "{x11}" (arg2), + [arg3] "{x12}" (arg3), + [arg4] "{x13}" (arg4), + : "memory" + ); +} + +pub fn syscall5(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize { + return asm volatile ("ecall" + : [ret] "={x10}" (-> usize), + : [number] "{x17}" (@intFromEnum(number)), + [arg1] "{x10}" (arg1), + [arg2] "{x11}" (arg2), + [arg3] "{x12}" (arg3), + [arg4] "{x13}" (arg4), + [arg5] "{x14}" (arg5), + : "memory" + ); +} + +pub fn syscall6( + number: SYS, + arg1: usize, + arg2: usize, + arg3: usize, + arg4: usize, + arg5: usize, + arg6: usize, +) usize { + return asm volatile ("ecall" + : [ret] "={x10}" (-> usize), + : [number] "{x17}" (@intFromEnum(number)), + [arg1] "{x10}" (arg1), + [arg2] "{x11}" (arg2), + [arg3] "{x12}" (arg3), + [arg4] "{x13}" (arg4), + [arg5] "{x14}" (arg5), + [arg6] "{x15}" (arg6), + : "memory" + ); +} + +const CloneFn = *const fn (arg: usize) callconv(.C) u8; + +pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize; + +pub const restore = restore_rt; + +pub fn restore_rt() callconv(.Naked) noreturn { + asm volatile ( + \\ ecall + : + : [number] "{x17}" (@intFromEnum(SYS.rt_sigreturn)), + : "memory" + ); +} + +pub const MMAP2_UNIT = 4096; + +pub const F = struct { + pub const DUPFD = 0; + pub const GETFD = 1; + pub const SETFD = 2; + pub const GETFL = 3; + pub const SETFL = 4; + pub const GETLK = 5; + pub const SETLK = 6; + pub const SETLKW = 7; + pub const SETOWN = 8; + pub const GETOWN = 9; + pub const SETSIG = 10; + pub const GETSIG = 11; + + pub const RDLCK = 0; + pub const WRLCK = 1; + pub const UNLCK = 2; + + pub const SETOWN_EX = 15; + pub const GETOWN_EX = 16; + + pub const GETOWNER_UIDS = 17; +}; + +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const UN = 8; + pub const NB = 4; +}; + +pub const blksize_t = i32; +pub const nlink_t = u32; +pub const time_t = i64; +pub const mode_t = u32; +pub const off_t = i64; +pub const ino_t = u64; +pub const dev_t = u64; +pub const blkcnt_t = i64; + +pub const timeval = extern struct { + tv_sec: time_t, + tv_usec: i64, +}; + +pub const Flock = extern struct { + type: i16, + whence: i16, + start: off_t, + len: off_t, + pid: pid_t, + __unused: [4]u8, +}; + +pub const msghdr = extern struct { + name: ?*sockaddr, + namelen: socklen_t, + iov: [*]iovec, + iovlen: i32, + __pad1: i32 = 0, + control: ?*anyopaque, + controllen: socklen_t, + __pad2: socklen_t = 0, + flags: i32, +}; + +pub const msghdr_const = extern struct { + name: ?*const sockaddr, + namelen: socklen_t, + iov: [*]const iovec_const, + iovlen: i32, + __pad1: i32 = 0, + control: ?*const anyopaque, + controllen: socklen_t, + __pad2: socklen_t = 0, + flags: i32, +}; + +// The `stat` definition used by the Linux kernel. +pub const Stat = extern struct { + dev: dev_t, + ino: ino_t, + mode: mode_t, + nlink: nlink_t, + uid: uid_t, + gid: gid_t, + rdev: dev_t, + __pad: usize, + size: off_t, + blksize: blksize_t, + __pad2: i32, + blocks: blkcnt_t, + atim: timespec, + mtim: timespec, + ctim: timespec, + __unused: [2]u32, + + pub fn atime(self: @This()) timespec { + return self.atim; + } + + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + + pub fn ctime(self: @This()) timespec { + return self.ctim; + } +}; + +pub const Elf_Symndx = u32; + +pub const VDSO = struct {}; diff --git a/lib/std/os/linux/riscv64.zig b/lib/std/os/linux/riscv64.zig index 88b5c70fbc32..b6470744ed9b 100644 --- a/lib/std/os/linux/riscv64.zig +++ b/lib/std/os/linux/riscv64.zig @@ -143,12 +143,12 @@ pub const LOCK = struct { pub const blksize_t = i32; pub const nlink_t = u32; -pub const time_t = isize; +pub const time_t = i64; pub const mode_t = u32; -pub const off_t = isize; -pub const ino_t = usize; -pub const dev_t = usize; -pub const blkcnt_t = isize; +pub const off_t = i64; +pub const ino_t = u64; +pub const dev_t = u64; +pub const blkcnt_t = i64; pub const timeval = extern struct { tv_sec: time_t,