Skip to content

Commit 1f8a721

Browse files
authored
Merge pull request #25610 from alexrp/std-os-linux-cleanup
`std.os.linux`: some miscellaneous cleanup in arch bits
2 parents 3091efa + 29fb9e4 commit 1f8a721

File tree

20 files changed

+1155
-1457
lines changed

20 files changed

+1155
-1457
lines changed

lib/std/os/linux.zig

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,27 @@ test {
3232
}
3333

3434
const arch_bits = switch (native_arch) {
35-
.x86 => @import("linux/x86.zig"),
36-
.x86_64 => @import("linux/x86_64.zig"),
3735
.aarch64, .aarch64_be => @import("linux/aarch64.zig"),
3836
.arm, .armeb, .thumb, .thumbeb => @import("linux/arm.zig"),
3937
.hexagon => @import("linux/hexagon.zig"),
40-
.riscv32 => @import("linux/riscv32.zig"),
41-
.riscv64 => @import("linux/riscv64.zig"),
42-
.sparc64 => @import("linux/sparc64.zig"),
4338
.loongarch64 => @import("linux/loongarch64.zig"),
4439
.m68k => @import("linux/m68k.zig"),
4540
.mips, .mipsel => @import("linux/mips.zig"),
46-
.mips64, .mips64el => @import("linux/mips64.zig"),
41+
.mips64, .mips64el => switch (builtin.abi) {
42+
.gnuabin32, .muslabin32 => @import("linux/mipsn32.zig"),
43+
else => @import("linux/mips64.zig"),
44+
},
4745
.powerpc, .powerpcle => @import("linux/powerpc.zig"),
4846
.powerpc64, .powerpc64le => @import("linux/powerpc64.zig"),
47+
.riscv32 => @import("linux/riscv32.zig"),
48+
.riscv64 => @import("linux/riscv64.zig"),
4949
.s390x => @import("linux/s390x.zig"),
50+
.sparc64 => @import("linux/sparc64.zig"),
51+
.x86 => @import("linux/x86.zig"),
52+
.x86_64 => switch (builtin.abi) {
53+
.gnux32, .muslx32 => @import("linux/x32.zig"),
54+
else => @import("linux/x86_64.zig"),
55+
},
5056
else => struct {},
5157
};
5258

@@ -88,9 +94,6 @@ pub fn clone(
8894
}
8995

9096
pub const ARCH = arch_bits.ARCH;
91-
pub const Elf_Symndx = arch_bits.Elf_Symndx;
92-
pub const F = arch_bits.F;
93-
pub const Flock = arch_bits.Flock;
9497
pub const HWCAP = arch_bits.HWCAP;
9598
pub const SC = arch_bits.SC;
9699
pub const Stat = arch_bits.Stat;
@@ -103,8 +106,6 @@ pub const mode_t = arch_bits.mode_t;
103106
pub const nlink_t = arch_bits.nlink_t;
104107
pub const off_t = arch_bits.off_t;
105108
pub const time_t = arch_bits.time_t;
106-
pub const timeval = arch_bits.timeval;
107-
pub const timezone = arch_bits.timezone;
108109
pub const user_desc = arch_bits.user_desc;
109110

110111
pub const tls = @import("linux/tls.zig");
@@ -113,10 +114,10 @@ pub const IOCTL = @import("linux/ioctl.zig");
113114
pub const SECCOMP = @import("linux/seccomp.zig");
114115

115116
pub const syscalls = @import("linux/syscalls.zig");
116-
pub const SYS = switch (@import("builtin").cpu.arch) {
117+
pub const SYS = switch (native_arch) {
117118
.arc => syscalls.Arc,
118-
.arm, .armeb, .thumb, .thumbeb => syscalls.Arm,
119119
.aarch64, .aarch64_be => syscalls.Arm64,
120+
.arm, .armeb, .thumb, .thumbeb => syscalls.Arm,
120121
.csky => syscalls.CSky,
121122
.hexagon => syscalls.Hexagon,
122123
.loongarch64 => syscalls.LoongArch64,
@@ -126,20 +127,20 @@ pub const SYS = switch (@import("builtin").cpu.arch) {
126127
.gnuabin32, .muslabin32 => syscalls.MipsN32,
127128
else => syscalls.MipsN64,
128129
},
130+
.or1k => syscalls.OpenRisc,
131+
.powerpc, .powerpcle => syscalls.PowerPC,
132+
.powerpc64, .powerpc64le => syscalls.PowerPC64,
129133
.riscv32 => syscalls.RiscV32,
130134
.riscv64 => syscalls.RiscV64,
131135
.s390x => syscalls.S390x,
132136
.sparc => syscalls.Sparc,
133137
.sparc64 => syscalls.Sparc64,
134-
.powerpc, .powerpcle => syscalls.PowerPC,
135-
.powerpc64, .powerpc64le => syscalls.PowerPC64,
136138
.x86 => syscalls.X86,
137139
.x86_64 => switch (builtin.abi) {
138140
.gnux32, .muslx32 => syscalls.X32,
139141
else => syscalls.X64,
140142
},
141143
.xtensa => syscalls.Xtensa,
142-
.or1k => syscalls.OpenRisc,
143144
else => @compileError("The Zig Standard Library is missing syscall definitions for the target CPU architecture"),
144145
};
145146

@@ -1600,6 +1601,70 @@ pub fn waitid(id_type: P, id: i32, infop: *siginfo_t, flags: u32) usize {
16001601
return syscall5(.waitid, @intFromEnum(id_type), @as(usize, @bitCast(@as(isize, id))), @intFromPtr(infop), flags, 0);
16011602
}
16021603

1604+
pub const F = struct {
1605+
pub const DUPFD = 0;
1606+
pub const GETFD = 1;
1607+
pub const SETFD = 2;
1608+
pub const GETFL = 3;
1609+
pub const SETFL = 4;
1610+
1611+
pub const GETLK = GET_SET_LK.GETLK;
1612+
pub const SETLK = GET_SET_LK.SETLK;
1613+
pub const SETLKW = GET_SET_LK.SETLKW;
1614+
1615+
const GET_SET_LK = if (@sizeOf(usize) == 64) extern struct {
1616+
pub const GETLK = if (is_mips) 14 else if (is_sparc) 7 else 5;
1617+
pub const SETLK = if (is_mips) 6 else if (is_sparc) 8 else 6;
1618+
pub const SETLKW = if (is_mips) 7 else if (is_sparc) 9 else 7;
1619+
} else extern struct {
1620+
// Ensure that 32-bit code uses the large-file variants (GETLK64, etc).
1621+
1622+
pub const GETLK = if (is_mips) 33 else 12;
1623+
pub const SETLK = if (is_mips) 34 else 13;
1624+
pub const SETLKW = if (is_mips) 35 else 14;
1625+
};
1626+
1627+
pub const SETOWN = if (is_mips) 24 else if (is_sparc) 6 else 8;
1628+
pub const GETOWN = if (is_mips) 23 else if (is_sparc) 5 else 9;
1629+
1630+
pub const SETSIG = 10;
1631+
pub const GETSIG = 11;
1632+
1633+
pub const SETOWN_EX = 15;
1634+
pub const GETOWN_EX = 16;
1635+
1636+
pub const GETOWNER_UIDS = 17;
1637+
1638+
pub const OFD_GETLK = 36;
1639+
pub const OFD_SETLK = 37;
1640+
pub const OFD_SETLKW = 38;
1641+
1642+
pub const RDLCK = if (is_sparc) 1 else 0;
1643+
pub const WRLCK = if (is_sparc) 2 else 1;
1644+
pub const UNLCK = if (is_sparc) 3 else 2;
1645+
};
1646+
1647+
pub const F_OWNER = enum(i32) {
1648+
TID = 0,
1649+
PID = 1,
1650+
PGRP = 2,
1651+
_,
1652+
};
1653+
1654+
pub const f_owner_ex = extern struct {
1655+
type: F_OWNER,
1656+
pid: pid_t,
1657+
};
1658+
1659+
pub const Flock = extern struct {
1660+
type: i16,
1661+
whence: i16,
1662+
start: off_t,
1663+
len: off_t,
1664+
pid: pid_t,
1665+
_unused: if (is_sparc) i16 else void,
1666+
};
1667+
16031668
pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) usize {
16041669
if (@hasField(SYS, "fcntl64")) {
16051670
return syscall3(.fcntl64, @as(usize, @bitCast(@as(isize, fd))), @as(usize, @bitCast(@as(isize, cmd))), arg);
@@ -1612,6 +1677,8 @@ pub fn flock(fd: fd_t, operation: i32) usize {
16121677
return syscall2(.flock, @as(usize, @bitCast(@as(isize, fd))), @as(usize, @bitCast(@as(isize, operation))));
16131678
}
16141679

1680+
pub const Elf_Symndx = if (native_arch == .s390x) u64 else u32;
1681+
16151682
// We must follow the C calling convention when we call into the VDSO
16161683
const VdsoClockGettime = *align(1) const fn (clockid_t, *timespec) callconv(.c) usize;
16171684
var vdso_clock_gettime: ?VdsoClockGettime = &init_vdso_clock_gettime;
@@ -8361,6 +8428,16 @@ pub const POSIX_FADV = switch (native_arch) {
83618428
},
83628429
};
83638430

8431+
pub const timeval = extern struct {
8432+
tv_sec: isize,
8433+
tv_usec: i64,
8434+
};
8435+
8436+
pub const timezone = extern struct {
8437+
minuteswest: i32,
8438+
dsttime: i32,
8439+
};
8440+
83648441
/// The timespec struct used by the kernel.
83658442
pub const kernel_timespec = extern struct {
83668443
sec: i64,

0 commit comments

Comments
 (0)