Skip to content

Commit

Permalink
Merge pull request #20389 from alexrp/riscv32
Browse files Browse the repository at this point in the history
Some `riscv32-linux` porting work
  • Loading branch information
andrewrk authored Jul 29, 2024
2 parents 73a4447 + 1394554 commit 38e0f04
Show file tree
Hide file tree
Showing 21 changed files with 913 additions and 222 deletions.
34 changes: 34 additions & 0 deletions lib/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,40 @@ fn clone() callconv(.Naked) void {
\\3: bx r5
);
},
.riscv32 => {
// __clone(func, stack, flags, arg, ptid, tls, ctid)
// a0, a1, a2, a3, a4, a5, a6

// syscall(SYS_clone, flags, stack, ptid, tls, ctid)
// a7 a0, a1, a2, a3, a4
asm volatile (
\\ # Save func and arg to stack
\\ addi a1, a1, -8
\\ sw a0, 0(a1)
\\ sw a3, 4(a1)
\\
\\ # Call SYS_clone
\\ mv a0, a2
\\ mv a2, a4
\\ mv a3, a5
\\ mv a4, a6
\\ li a7, 220 # SYS_clone
\\ ecall
\\
\\ beqz a0, 1f
\\ # Parent
\\ ret
\\
\\ # Child
\\1: lw a1, 0(sp)
\\ lw a0, 4(sp)
\\ jalr a1
\\
\\ # Exit
\\ li a7, 93 # SYS_exit
\\ ecall
);
},
.riscv64 => {
// __clone(func, stack, flags, arg, ptid, tls, ctid)
// a0, a1, a2, a3, a4, a5, a6
Expand Down
11 changes: 10 additions & 1 deletion lib/std/Target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,16 @@ pub const Abi = enum {

pub inline fn isGnu(abi: Abi) bool {
return switch (abi) {
.gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => true,
.gnu,
.gnuabin32,
.gnuabi64,
.gnueabi,
.gnueabihf,
.gnuf32,
.gnusf,
.gnux32,
.gnuilp32,
=> true,
else => false,
};
}
Expand Down
49 changes: 31 additions & 18 deletions lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1082,11 +1082,11 @@ const LinuxThreadImpl = struct {
fn freeAndExit(self: *ThreadCompletion) noreturn {
switch (target.cpu.arch) {
.x86 => asm volatile (
\\ movl $91, %%eax
\\ movl $91, %%eax # SYS_munmap
\\ movl %[ptr], %%ebx
\\ movl %[len], %%ecx
\\ int $128
\\ movl $1, %%eax
\\ movl $1, %%eax # SYS_exit
\\ movl $0, %%ebx
\\ int $128
:
Expand All @@ -1095,21 +1095,21 @@ const LinuxThreadImpl = struct {
: "memory"
),
.x86_64 => asm volatile (
\\ movq $11, %%rax
\\ movq $11, %%rax # SYS_munmap
\\ syscall
\\ movq $60, %%rax
\\ movq $60, %%rax # SYS_exit
\\ movq $1, %%rdi
\\ syscall
:
: [ptr] "{rdi}" (@intFromPtr(self.mapped.ptr)),
[len] "{rsi}" (self.mapped.len),
),
.arm, .armeb, .thumb, .thumbeb => asm volatile (
\\ mov r7, #91
\\ mov r7, #91 // SYS_munmap
\\ mov r0, %[ptr]
\\ mov r1, %[len]
\\ svc 0
\\ mov r7, #1
\\ mov r7, #1 // SYS_exit
\\ mov r0, #0
\\ svc 0
:
Expand All @@ -1118,11 +1118,11 @@ const LinuxThreadImpl = struct {
: "memory"
),
.aarch64, .aarch64_be => asm volatile (
\\ mov x8, #215
\\ mov x8, #215 // SYS_munmap
\\ mov x0, %[ptr]
\\ mov x1, %[len]
\\ svc 0
\\ mov x8, #93
\\ mov x8, #93 // SYS_exit
\\ mov x0, #0
\\ svc 0
:
Expand All @@ -1132,11 +1132,11 @@ const LinuxThreadImpl = struct {
),
.mips, .mipsel => asm volatile (
\\ move $sp, $25
\\ li $2, 4091
\\ li $2, 4091 # SYS_munmap
\\ move $4, %[ptr]
\\ move $5, %[len]
\\ syscall
\\ li $2, 4001
\\ li $2, 4001 # SYS_exit
\\ li $4, 0
\\ syscall
:
Expand All @@ -1145,11 +1145,11 @@ const LinuxThreadImpl = struct {
: "memory"
),
.mips64, .mips64el => asm volatile (
\\ li $2, 4091
\\ li $2, 4091 # SYS_munmap
\\ move $4, %[ptr]
\\ move $5, %[len]
\\ syscall
\\ li $2, 4001
\\ li $2, 4001 # SYS_exit
\\ li $4, 0
\\ syscall
:
Expand All @@ -1158,11 +1158,11 @@ const LinuxThreadImpl = struct {
: "memory"
),
.powerpc, .powerpcle, .powerpc64, .powerpc64le => asm volatile (
\\ li 0, 91
\\ li 0, 91 # SYS_munmap
\\ mr %[ptr], 3
\\ mr %[len], 4
\\ sc
\\ li 0, 1
\\ li 0, 1 # SYS_exit
\\ li 3, 0
\\ sc
\\ blr
Expand All @@ -1171,12 +1171,25 @@ const LinuxThreadImpl = struct {
[len] "r" (self.mapped.len),
: "memory"
),
.riscv32 => asm volatile (
\\ li a7, 215 # SYS_munmap
\\ mv a0, %[ptr]
\\ mv a1, %[len]
\\ ecall
\\ li a7, 93 # SYS_exit
\\ mv a0, zero
\\ ecall
:
: [ptr] "r" (@intFromPtr(self.mapped.ptr)),
[len] "r" (self.mapped.len),
: "memory"
),
.riscv64 => asm volatile (
\\ li a7, 215
\\ li a7, 215 # SYS_munmap
\\ mv a0, %[ptr]
\\ mv a1, %[len]
\\ ecall
\\ li a7, 93
\\ li a7, 93 # SYS_exit
\\ mv a0, zero
\\ ecall
:
Expand All @@ -1196,14 +1209,14 @@ const LinuxThreadImpl = struct {
\\ ba 1b
\\ restore
\\ 2:
\\ mov 73, %%g1
\\ mov 73, %%g1 # SYS_munmap
\\ mov %[ptr], %%o0
\\ mov %[len], %%o1
\\ # Flush register window contents to prevent background
\\ # memory access before unmapping the stack.
\\ flushw
\\ t 0x6d
\\ mov 1, %%g1
\\ mov 1, %%g1 # SYS_exit
\\ mov 1, %%o0
\\ t 0x6d
:
Expand Down
3 changes: 2 additions & 1 deletion lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,8 @@ pub const StackIterator = struct {
.SUCCESS => return bytes_read == buf.len,
.FAULT => return false,
.INVAL, .PERM, .SRCH => unreachable, // own pid is always valid
.NOMEM, .NOSYS => {},
.NOMEM => {},
.NOSYS => {}, // QEMU is known not to implement this syscall.
else => unreachable, // unexpected
}
var path_buf: [
Expand Down
29 changes: 27 additions & 2 deletions lib/std/fs/Dir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ pub const Iterator = switch (native_os) {
first_iter: bool,

const Self = @This();
const linux = std.os.linux;

pub const Error = IteratorError;

Expand Down Expand Up @@ -2690,8 +2689,33 @@ pub fn statFile(self: Dir, sub_path: []const u8) StatFileError!Stat {
const st = try std.os.fstatat_wasi(self.fd, sub_path, .{ .SYMLINK_FOLLOW = true });
return Stat.fromWasi(st);
}
if (native_os == .linux) {
const sub_path_c = try posix.toPosixPath(sub_path);
var stx = std.mem.zeroes(linux.Statx);

const rc = linux.statx(
self.fd,
&sub_path_c,
linux.AT.NO_AUTOMOUNT,
linux.STATX_TYPE | linux.STATX_MODE | linux.STATX_ATIME | linux.STATX_MTIME | linux.STATX_CTIME,
&stx,
);

return switch (linux.E.init(rc)) {
.SUCCESS => Stat.fromLinux(stx),
.ACCES => error.AccessDenied,
.BADF => unreachable,
.FAULT => unreachable,
.INVAL => unreachable,
.LOOP => error.SymLinkLoop,
.NAMETOOLONG => unreachable, // Handled by posix.toPosixPath() above.
.NOENT, .NOTDIR => error.FileNotFound,
.NOMEM => error.SystemResources,
else => |err| posix.unexpectedErrno(err),
};
}
const st = try posix.fstatat(self.fd, sub_path, 0);
return Stat.fromSystem(st);
return Stat.fromPosix(st);
}

pub const ChmodError = File.ChmodError;
Expand Down Expand Up @@ -2751,6 +2775,7 @@ const path = fs.path;
const fs = std.fs;
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const linux = std.os.linux;
const windows = std.os.windows;
const native_os = builtin.os.tag;
const have_flock = @TypeOf(posix.system.flock) != void;
Loading

0 comments on commit 38e0f04

Please sign in to comment.