Skip to content

Commit 38e0f04

Browse files
authored
Merge pull request #20389 from alexrp/riscv32
Some `riscv32-linux` porting work
2 parents 73a4447 + 1394554 commit 38e0f04

21 files changed

+913
-222
lines changed

lib/c.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,40 @@ fn clone() callconv(.Naked) void {
319319
\\3: bx r5
320320
);
321321
},
322+
.riscv32 => {
323+
// __clone(func, stack, flags, arg, ptid, tls, ctid)
324+
// a0, a1, a2, a3, a4, a5, a6
325+
326+
// syscall(SYS_clone, flags, stack, ptid, tls, ctid)
327+
// a7 a0, a1, a2, a3, a4
328+
asm volatile (
329+
\\ # Save func and arg to stack
330+
\\ addi a1, a1, -8
331+
\\ sw a0, 0(a1)
332+
\\ sw a3, 4(a1)
333+
\\
334+
\\ # Call SYS_clone
335+
\\ mv a0, a2
336+
\\ mv a2, a4
337+
\\ mv a3, a5
338+
\\ mv a4, a6
339+
\\ li a7, 220 # SYS_clone
340+
\\ ecall
341+
\\
342+
\\ beqz a0, 1f
343+
\\ # Parent
344+
\\ ret
345+
\\
346+
\\ # Child
347+
\\1: lw a1, 0(sp)
348+
\\ lw a0, 4(sp)
349+
\\ jalr a1
350+
\\
351+
\\ # Exit
352+
\\ li a7, 93 # SYS_exit
353+
\\ ecall
354+
);
355+
},
322356
.riscv64 => {
323357
// __clone(func, stack, flags, arg, ptid, tls, ctid)
324358
// a0, a1, a2, a3, a4, a5, a6

lib/std/Target.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,16 @@ pub const Abi = enum {
720720

721721
pub inline fn isGnu(abi: Abi) bool {
722722
return switch (abi) {
723-
.gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => true,
723+
.gnu,
724+
.gnuabin32,
725+
.gnuabi64,
726+
.gnueabi,
727+
.gnueabihf,
728+
.gnuf32,
729+
.gnusf,
730+
.gnux32,
731+
.gnuilp32,
732+
=> true,
724733
else => false,
725734
};
726735
}

lib/std/Thread.zig

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,11 +1082,11 @@ const LinuxThreadImpl = struct {
10821082
fn freeAndExit(self: *ThreadCompletion) noreturn {
10831083
switch (target.cpu.arch) {
10841084
.x86 => asm volatile (
1085-
\\ movl $91, %%eax
1085+
\\ movl $91, %%eax # SYS_munmap
10861086
\\ movl %[ptr], %%ebx
10871087
\\ movl %[len], %%ecx
10881088
\\ int $128
1089-
\\ movl $1, %%eax
1089+
\\ movl $1, %%eax # SYS_exit
10901090
\\ movl $0, %%ebx
10911091
\\ int $128
10921092
:
@@ -1095,21 +1095,21 @@ const LinuxThreadImpl = struct {
10951095
: "memory"
10961096
),
10971097
.x86_64 => asm volatile (
1098-
\\ movq $11, %%rax
1098+
\\ movq $11, %%rax # SYS_munmap
10991099
\\ syscall
1100-
\\ movq $60, %%rax
1100+
\\ movq $60, %%rax # SYS_exit
11011101
\\ movq $1, %%rdi
11021102
\\ syscall
11031103
:
11041104
: [ptr] "{rdi}" (@intFromPtr(self.mapped.ptr)),
11051105
[len] "{rsi}" (self.mapped.len),
11061106
),
11071107
.arm, .armeb, .thumb, .thumbeb => asm volatile (
1108-
\\ mov r7, #91
1108+
\\ mov r7, #91 // SYS_munmap
11091109
\\ mov r0, %[ptr]
11101110
\\ mov r1, %[len]
11111111
\\ svc 0
1112-
\\ mov r7, #1
1112+
\\ mov r7, #1 // SYS_exit
11131113
\\ mov r0, #0
11141114
\\ svc 0
11151115
:
@@ -1118,11 +1118,11 @@ const LinuxThreadImpl = struct {
11181118
: "memory"
11191119
),
11201120
.aarch64, .aarch64_be => asm volatile (
1121-
\\ mov x8, #215
1121+
\\ mov x8, #215 // SYS_munmap
11221122
\\ mov x0, %[ptr]
11231123
\\ mov x1, %[len]
11241124
\\ svc 0
1125-
\\ mov x8, #93
1125+
\\ mov x8, #93 // SYS_exit
11261126
\\ mov x0, #0
11271127
\\ svc 0
11281128
:
@@ -1132,11 +1132,11 @@ const LinuxThreadImpl = struct {
11321132
),
11331133
.mips, .mipsel => asm volatile (
11341134
\\ move $sp, $25
1135-
\\ li $2, 4091
1135+
\\ li $2, 4091 # SYS_munmap
11361136
\\ move $4, %[ptr]
11371137
\\ move $5, %[len]
11381138
\\ syscall
1139-
\\ li $2, 4001
1139+
\\ li $2, 4001 # SYS_exit
11401140
\\ li $4, 0
11411141
\\ syscall
11421142
:
@@ -1145,11 +1145,11 @@ const LinuxThreadImpl = struct {
11451145
: "memory"
11461146
),
11471147
.mips64, .mips64el => asm volatile (
1148-
\\ li $2, 4091
1148+
\\ li $2, 4091 # SYS_munmap
11491149
\\ move $4, %[ptr]
11501150
\\ move $5, %[len]
11511151
\\ syscall
1152-
\\ li $2, 4001
1152+
\\ li $2, 4001 # SYS_exit
11531153
\\ li $4, 0
11541154
\\ syscall
11551155
:
@@ -1158,11 +1158,11 @@ const LinuxThreadImpl = struct {
11581158
: "memory"
11591159
),
11601160
.powerpc, .powerpcle, .powerpc64, .powerpc64le => asm volatile (
1161-
\\ li 0, 91
1161+
\\ li 0, 91 # SYS_munmap
11621162
\\ mr %[ptr], 3
11631163
\\ mr %[len], 4
11641164
\\ sc
1165-
\\ li 0, 1
1165+
\\ li 0, 1 # SYS_exit
11661166
\\ li 3, 0
11671167
\\ sc
11681168
\\ blr
@@ -1171,12 +1171,25 @@ const LinuxThreadImpl = struct {
11711171
[len] "r" (self.mapped.len),
11721172
: "memory"
11731173
),
1174+
.riscv32 => asm volatile (
1175+
\\ li a7, 215 # SYS_munmap
1176+
\\ mv a0, %[ptr]
1177+
\\ mv a1, %[len]
1178+
\\ ecall
1179+
\\ li a7, 93 # SYS_exit
1180+
\\ mv a0, zero
1181+
\\ ecall
1182+
:
1183+
: [ptr] "r" (@intFromPtr(self.mapped.ptr)),
1184+
[len] "r" (self.mapped.len),
1185+
: "memory"
1186+
),
11741187
.riscv64 => asm volatile (
1175-
\\ li a7, 215
1188+
\\ li a7, 215 # SYS_munmap
11761189
\\ mv a0, %[ptr]
11771190
\\ mv a1, %[len]
11781191
\\ ecall
1179-
\\ li a7, 93
1192+
\\ li a7, 93 # SYS_exit
11801193
\\ mv a0, zero
11811194
\\ ecall
11821195
:
@@ -1196,14 +1209,14 @@ const LinuxThreadImpl = struct {
11961209
\\ ba 1b
11971210
\\ restore
11981211
\\ 2:
1199-
\\ mov 73, %%g1
1212+
\\ mov 73, %%g1 # SYS_munmap
12001213
\\ mov %[ptr], %%o0
12011214
\\ mov %[len], %%o1
12021215
\\ # Flush register window contents to prevent background
12031216
\\ # memory access before unmapping the stack.
12041217
\\ flushw
12051218
\\ t 0x6d
1206-
\\ mov 1, %%g1
1219+
\\ mov 1, %%g1 # SYS_exit
12071220
\\ mov 1, %%o0
12081221
\\ t 0x6d
12091222
:

lib/std/debug.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,8 @@ pub const StackIterator = struct {
747747
.SUCCESS => return bytes_read == buf.len,
748748
.FAULT => return false,
749749
.INVAL, .PERM, .SRCH => unreachable, // own pid is always valid
750-
.NOMEM, .NOSYS => {},
750+
.NOMEM => {},
751+
.NOSYS => {}, // QEMU is known not to implement this syscall.
751752
else => unreachable, // unexpected
752753
}
753754
var path_buf: [

lib/std/fs/Dir.zig

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ pub const Iterator = switch (native_os) {
334334
first_iter: bool,
335335

336336
const Self = @This();
337-
const linux = std.os.linux;
338337

339338
pub const Error = IteratorError;
340339

@@ -2690,8 +2689,33 @@ pub fn statFile(self: Dir, sub_path: []const u8) StatFileError!Stat {
26902689
const st = try std.os.fstatat_wasi(self.fd, sub_path, .{ .SYMLINK_FOLLOW = true });
26912690
return Stat.fromWasi(st);
26922691
}
2692+
if (native_os == .linux) {
2693+
const sub_path_c = try posix.toPosixPath(sub_path);
2694+
var stx = std.mem.zeroes(linux.Statx);
2695+
2696+
const rc = linux.statx(
2697+
self.fd,
2698+
&sub_path_c,
2699+
linux.AT.NO_AUTOMOUNT,
2700+
linux.STATX_TYPE | linux.STATX_MODE | linux.STATX_ATIME | linux.STATX_MTIME | linux.STATX_CTIME,
2701+
&stx,
2702+
);
2703+
2704+
return switch (linux.E.init(rc)) {
2705+
.SUCCESS => Stat.fromLinux(stx),
2706+
.ACCES => error.AccessDenied,
2707+
.BADF => unreachable,
2708+
.FAULT => unreachable,
2709+
.INVAL => unreachable,
2710+
.LOOP => error.SymLinkLoop,
2711+
.NAMETOOLONG => unreachable, // Handled by posix.toPosixPath() above.
2712+
.NOENT, .NOTDIR => error.FileNotFound,
2713+
.NOMEM => error.SystemResources,
2714+
else => |err| posix.unexpectedErrno(err),
2715+
};
2716+
}
26932717
const st = try posix.fstatat(self.fd, sub_path, 0);
2694-
return Stat.fromSystem(st);
2718+
return Stat.fromPosix(st);
26952719
}
26962720

26972721
pub const ChmodError = File.ChmodError;
@@ -2751,6 +2775,7 @@ const path = fs.path;
27512775
const fs = std.fs;
27522776
const Allocator = std.mem.Allocator;
27532777
const assert = std.debug.assert;
2778+
const linux = std.os.linux;
27542779
const windows = std.os.windows;
27552780
const native_os = builtin.os.tag;
27562781
const have_flock = @TypeOf(posix.system.flock) != void;

0 commit comments

Comments
 (0)