Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some riscv32-linux porting work #20389

Merged
merged 23 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
11badbf
musl: Fix needsCrtiCrtn() to return false for riscv32 too.
alexrp Jun 22, 2024
6ec7757
link.Elf: Define __global_pointer$ for riscv32 too.
alexrp Jun 22, 2024
c31409b
std.Target.Abi: Handle a few more GNU ABIs in isGnu().
alexrp Jun 22, 2024
e74c368
std.zig.target: Set the minimum glibc for riscv32 to 2.33.
alexrp Jun 22, 2024
8b176ab
start: Implement _start() for riscv32.
alexrp Jun 22, 2024
b958225
c: Implement clone() for riscv32-linux.
alexrp Jun 22, 2024
b83d102
std.Thread: Implement LinuxThreadImpl.ThreadCompletion.freeAndExit() …
alexrp Jun 22, 2024
a59ab5f
std.Thread: Add some syscall comments to LinuxThreadImpl.ThreadComple…
alexrp Jul 20, 2024
e0b9ebf
gen_stubs: Add riscv32 handling in a few more places.
alexrp Jun 22, 2024
0460248
generate_linux_syscalls: Handle riscv_hwprobe.
alexrp Jun 22, 2024
290609e
generate_linux_syscalls: Add riscv32 support.
alexrp Jun 22, 2024
f494a47
generate_linux_syscalls: Add some missing include paths for riscv.
alexrp Jun 22, 2024
7e74276
generate_linux_syscalls: Rework generation strategy for newer kernel …
alexrp Jun 22, 2024
4e7c3cc
std.os.linux.syscalls: Regenerate based on Linux v6.7.
alexrp Jun 22, 2024
2d1ee67
std.os.linux: Some adjustments after syscall generation strategy chan…
alexrp Jun 22, 2024
7532a8a
std.os.linux: Add riscv32 support.
alexrp Jun 22, 2024
cafce8c
std.os.linux.test: Partially skip statx() test on riscv32.
alexrp Jun 25, 2024
890433e
std.os.linux: Define timespec as kernel_timespec (64-bit) for riscv32.
alexrp Jun 26, 2024
43410cd
std.os.linux: Remove the sparc64 workaround in fadvise().
alexrp Jun 23, 2024
aeb3abc
std.os.linux.start_pie: Handle riscv32 in getDynamicSymbol().
alexrp Jun 22, 2024
6eb9cb6
std.os.linux.tls: Handle riscv32 in setThreadPointer().
alexrp Jun 22, 2024
4e5068c
std: Stop supporting Linux/glibc versions older than declared in std.…
alexrp Jul 21, 2024
1394554
std.fs: Rework to always use statx() instead of fstat()/fstatat() on …
alexrp Jul 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (
alexrp marked this conversation as resolved.
Show resolved Hide resolved
\\ # 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