authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-06-22 19:20:02+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-07-29 09:50:41+02:00
log2d1ee678eb2a9d6713e5b7758c7620ccfe1eb1ff
tree289f72d1e8bf5e7d26ef0cc66257826498d5a936
parent4e7c3cca916d0dbfc4baa7c21f6d5ab6b8d84b2b
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.os.linux: Some adjustments after syscall generation strategy changes.


1 files changed, 32 insertions(+), 18 deletions(-)

lib/std/os/linux.zig+32-18
......@@ -474,7 +474,7 @@ pub fn dup2(old: i32, new: i32) usize {
474474 } else {
475475 if (old == new) {
476476 if (std.debug.runtime_safety) {
477 const rc = syscall2(.fcntl, @as(usize, @bitCast(@as(isize, old))), F.GETFD);
477 const rc = fcntl(F.GETFD, @as(fd_t, old), 0);
478478 if (@as(isize, @bitCast(rc)) < 0) return rc;
479479 }
480480 return @as(usize, @intCast(old));
......@@ -1211,7 +1211,7 @@ pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize {
12111211 // NOTE: The offset parameter splitting is independent from the target
12121212 // endianness.
12131213 return syscall5(
1214 ._llseek,
1214 .llseek,
12151215 @as(usize, @bitCast(@as(isize, fd))),
12161216 @as(usize, @truncate(offset >> 32)),
12171217 @as(usize, @truncate(offset)),
......@@ -1370,7 +1370,11 @@ pub fn waitid(id_type: P, id: i32, infop: *siginfo_t, flags: u32) usize {
13701370}
13711371
13721372pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) usize {
1373 return syscall3(.fcntl, @as(usize, @bitCast(@as(isize, fd))), @as(usize, @bitCast(@as(isize, cmd))), arg);
1373 if (@hasField(SYS, "fcntl64")) {
1374 return syscall3(.fcntl64, @as(usize, @bitCast(@as(isize, fd))), @as(usize, @bitCast(@as(isize, cmd))), arg);
1375 } else {
1376 return syscall3(.fcntl, @as(usize, @bitCast(@as(isize, fd))), @as(usize, @bitCast(@as(isize, cmd))), arg);
1377 }
13741378}
13751379
13761380pub fn flock(fd: fd_t, operation: i32) usize {
......@@ -2198,40 +2202,40 @@ pub fn process_vm_writev(pid: pid_t, local: []const iovec_const, remote: []const
21982202}
21992203
22002204pub fn fadvise(fd: fd_t, offset: i64, len: i64, advice: usize) usize {
2201 if (comptime builtin.cpu.arch.isMIPS()) {
2202 // MIPS requires a 7 argument syscall
2205 if (comptime native_arch.isARM() or native_arch.isPPC()) {
2206 // These architectures reorder the arguments so that a register is not skipped to align the
2207 // register number that `offset` is passed in.
22032208
22042209 const offset_halves = splitValue64(offset);
22052210 const length_halves = splitValue64(len);
22062211
2207 return syscall7(
2208 .fadvise64,
2212 return syscall6(
2213 .fadvise64_64,
22092214 @as(usize, @bitCast(@as(isize, fd))),
2210 0,
2215 advice,
22112216 offset_halves[0],
22122217 offset_halves[1],
22132218 length_halves[0],
22142219 length_halves[1],
2215 advice,
22162220 );
2217 } else if (comptime builtin.cpu.arch.isARM()) {
2218 // ARM reorders the arguments
2221 } else if (comptime native_arch == .mips or native_arch == .mipsel) {
2222 // MIPS O32 does not deal with the register alignment issue, so pass a dummy value.
22192223
22202224 const offset_halves = splitValue64(offset);
22212225 const length_halves = splitValue64(len);
22222226
2223 return syscall6(
2224 .fadvise64_64,
2227 return syscall7(
2228 .fadvise64,
22252229 @as(usize, @bitCast(@as(isize, fd))),
2226 advice,
2230 0,
22272231 offset_halves[0],
22282232 offset_halves[1],
22292233 length_halves[0],
22302234 length_halves[1],
2235 advice,
22312236 );
2232 } else if (@hasField(SYS, "fadvise64_64") and usize_bits != 64) {
2233 // The extra usize check is needed to avoid SPARC64 because it provides both
2234 // fadvise64 and fadvise64_64 but the latter behaves differently than other platforms.
2237 } else if (comptime usize_bits < 64) {
2238 // Other 32-bit architectures do not require register alignment.
22352239
22362240 const offset_halves = splitValue64(offset);
22372241 const length_halves = splitValue64(len);
......@@ -2246,8 +2250,18 @@ pub fn fadvise(fd: fd_t, offset: i64, len: i64, advice: usize) usize {
22462250 advice,
22472251 );
22482252 } else {
2253 // On 64-bit architectures, fadvise64_64 and fadvise64 are the same. Generally, older ports
2254 // call it fadvise64 (x86, PowerPC, etc), while newer ports call it fadvise64_64 (RISC-V,
2255 // LoongArch, etc). SPARC is the odd one out because it has both.
22492256 return syscall4(
2250 .fadvise64,
2257 // 64-bit SPARC (apparently?) has a broken fadvise64_64, so use its fadvise64 instead.
2258 // TODO: I can't make sense of this. They go to the same code in the kernel, and there
2259 // is no special-casing for SPARC in glibc and musl. I suspect a QEMU bug, which is
2260 // really not our responsibility.
2261 if (@hasField(SYS, "fadvise64_64") and native_arch != .sparc64)
2262 .fadvise64_64
2263 else
2264 .fadvise64,
22512265 @as(usize, @bitCast(@as(isize, fd))),
22522266 @as(usize, @bitCast(offset)),
22532267 @as(usize, @bitCast(len)),