Skip to content

Commit 5f3d9e0

Browse files
authored
Merge pull request ziglang#21261 from alexrp/riscv32
More `riscv32-linux` port work
2 parents f29bdd6 + ae10adb commit 5f3d9e0

File tree

6 files changed

+85
-5
lines changed

6 files changed

+85
-5
lines changed

lib/std/dynamic_library.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ pub const ElfDynLib = struct {
215215
const fd = try resolveFromName(path);
216216
defer posix.close(fd);
217217

218-
const stat = try posix.fstat(fd);
218+
const file: std.fs.File = .{ .handle = fd };
219+
const stat = try file.stat();
219220
const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig;
220221

221222
// This one is to read the ELF info. We do more mmapping later

lib/std/os/linux.zig

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,16 @@ pub fn clock_settime(clk_id: i32, tp: *const timespec) usize {
14561456
return syscall2(.clock_settime, @as(usize, @bitCast(@as(isize, clk_id))), @intFromPtr(tp));
14571457
}
14581458

1459+
pub fn clock_nanosleep(clockid: clockid_t, flags: TIMER, request: *const timespec, remain: ?*timespec) usize {
1460+
return syscall4(
1461+
.clock_nanosleep,
1462+
@intFromEnum(clockid),
1463+
@as(u32, @bitCast(flags)),
1464+
@intFromPtr(request),
1465+
@intFromPtr(remain),
1466+
);
1467+
}
1468+
14591469
pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) usize {
14601470
return syscall2(.gettimeofday, @intFromPtr(tv), @intFromPtr(tz));
14611471
}
@@ -1465,7 +1475,9 @@ pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize {
14651475
}
14661476

14671477
pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {
1468-
return syscall2(.nanosleep, @intFromPtr(req), @intFromPtr(rem));
1478+
if (native_arch == .riscv32) {
1479+
@compileError("No nanosleep syscall on this architecture.");
1480+
} else return syscall2(.nanosleep, @intFromPtr(req), @intFromPtr(rem));
14691481
}
14701482

14711483
pub fn pause() usize {
@@ -4525,6 +4537,11 @@ pub const clockid_t = enum(u32) {
45254537
_,
45264538
};
45274539

4540+
pub const TIMER = packed struct(u32) {
4541+
ABSTIME: bool,
4542+
_: u31 = 0,
4543+
};
4544+
45284545
pub const CSIGNAL = 0x000000ff;
45294546

45304547
pub const CLONE = struct {
@@ -7452,7 +7469,7 @@ pub const kernel_timespec = extern struct {
74527469
};
74537470

74547471
// https://github.com/ziglang/zig/issues/4726#issuecomment-2190337877
7455-
pub const timespec = if (!builtin.link_libc and native_arch == .riscv32) kernel_timespec else extern struct {
7472+
pub const timespec = if (native_arch == .riscv32) kernel_timespec else extern struct {
74567473
sec: isize,
74577474
nsec: isize,
74587475
};

lib/std/os/linux/riscv32.zig

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,37 @@ pub const msghdr_const = extern struct {
212212
flags: i32,
213213
};
214214

215-
/// No `Stat` structure on this platform, only `Statx`.
216-
pub const Stat = void;
215+
// The `stat` definition used by the Linux kernel.
216+
pub const Stat = extern struct {
217+
dev: dev_t,
218+
ino: ino_t,
219+
mode: mode_t,
220+
nlink: nlink_t,
221+
uid: uid_t,
222+
gid: gid_t,
223+
rdev: dev_t,
224+
__pad: usize,
225+
size: off_t,
226+
blksize: blksize_t,
227+
__pad2: i32,
228+
blocks: blkcnt_t,
229+
atim: timespec,
230+
mtim: timespec,
231+
ctim: timespec,
232+
__unused: [2]u32,
233+
234+
pub fn atime(self: @This()) timespec {
235+
return self.atim;
236+
}
237+
238+
pub fn mtime(self: @This()) timespec {
239+
return self.mtim;
240+
}
241+
242+
pub fn ctime(self: @This()) timespec {
243+
return self.ctim;
244+
}
245+
};
217246

218247
pub const Elf_Symndx = u32;
219248

lib/std/posix/test.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ test "linkat with different directories" {
349349
}
350350

351351
test "fstatat" {
352+
if (builtin.cpu.arch == .riscv32 and builtin.os.tag == .linux and !builtin.link_libc) return error.SkipZigTest; // No `fstatat()`.
352353
// enable when `fstat` and `fstatat` are implemented on Windows
353354
if (native_os == .windows) return error.SkipZigTest;
354355

@@ -1264,6 +1265,9 @@ test "fchmodat smoke test" {
12641265
0o644,
12651266
);
12661267
posix.close(fd);
1268+
1269+
if (builtin.cpu.arch == .riscv32 and builtin.os.tag == .linux and !builtin.link_libc) return error.SkipZigTest; // No `fstatat()`.
1270+
12671271
try posix.symlinkat("regfile", tmp.dir.fd, "symlink");
12681272
const sym_mode = blk: {
12691273
const st = try posix.fstatat(tmp.dir.fd, "symlink", posix.AT.SYMLINK_NOFOLLOW);

lib/std/time.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ pub fn sleep(nanoseconds: u64) void {
5050

5151
const s = nanoseconds / ns_per_s;
5252
const ns = nanoseconds % ns_per_s;
53+
54+
// Newer kernel ports don't have old `nanosleep()` and `clock_nanosleep()` has been around
55+
// since Linux 2.6 and glibc 2.1 anyway.
56+
if (builtin.os.tag == .linux) {
57+
const linux = std.os.linux;
58+
59+
var req: linux.timespec = .{
60+
.sec = std.math.cast(linux.time_t, s) orelse std.math.maxInt(linux.time_t),
61+
.nsec = std.math.cast(linux.time_t, ns) orelse std.math.maxInt(linux.time_t),
62+
};
63+
var rem: linux.timespec = undefined;
64+
65+
while (true) {
66+
switch (linux.E.init(linux.clock_nanosleep(.MONOTONIC, .{ .ABSTIME = false }, &req, &rem))) {
67+
.SUCCESS => return,
68+
.INTR => {
69+
req = rem;
70+
continue;
71+
},
72+
.FAULT,
73+
.INVAL,
74+
.OPNOTSUPP,
75+
=> unreachable,
76+
else => return,
77+
}
78+
}
79+
}
80+
5381
posix.nanosleep(s, ns);
5482
}
5583

src/codegen/llvm.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12398,6 +12398,7 @@ fn backendSupportsF16(target: std.Target) bool {
1239812398
.mipsel,
1239912399
.mips64,
1240012400
.mips64el,
12401+
.riscv32,
1240112402
.s390x,
1240212403
=> false,
1240312404
.aarch64,

0 commit comments

Comments
 (0)