From bcb4ba9afd610a0f3ab66ad828c90fd57dc95a6f Mon Sep 17 00:00:00 2001 From: Stefan Weigl-Bosker Date: Sat, 29 Mar 2025 18:31:55 -0400 Subject: [PATCH] std.os.linux: use heap.pageSize() instead of MMAP2_UNIT --- lib/std/c.zig | 4 ---- lib/std/os/linux.zig | 3 +-- lib/std/os/linux/arm.zig | 2 -- lib/std/os/linux/hexagon.zig | 2 -- lib/std/os/linux/m68k.zig | 4 ---- lib/std/os/linux/mips.zig | 2 -- lib/std/os/linux/mips64.zig | 2 -- lib/std/os/linux/powerpc.zig | 2 -- lib/std/os/linux/riscv32.zig | 2 -- lib/std/os/linux/tls.zig | 32 ++++++++++++++------------------ lib/std/os/linux/x86.zig | 2 -- lib/std/posix.zig | 1 - 12 files changed, 15 insertions(+), 43 deletions(-) diff --git a/lib/std/c.zig b/lib/std/c.zig index fc5497a5be..8e50eb9792 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -1601,10 +1601,6 @@ pub const MSF = switch (native_os) { }, else => void, }; -pub const MMAP2_UNIT = switch (native_os) { - .linux => linux.MMAP2_UNIT, - else => void, -}; pub const NAME_MAX = switch (native_os) { .linux => linux.NAME_MAX, .emscripten => emscripten.NAME_MAX, diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 4cf305bb20..04dc452c0c 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -93,7 +93,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx; pub const F = arch_bits.F; pub const Flock = arch_bits.Flock; pub const HWCAP = arch_bits.HWCAP; -pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT; pub const REG = arch_bits.REG; pub const SC = arch_bits.SC; pub const Stat = arch_bits.Stat; @@ -928,7 +927,7 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: MAP, fd: i32, of prot, @as(u32, @bitCast(flags)), @bitCast(@as(isize, fd)), - @truncate(@as(u64, @bitCast(offset)) / MMAP2_UNIT), + @truncate(@as(u64, @bitCast(offset)) / std.heap.pageSize()), ); } else { // The s390x mmap() syscall existed before Linux supported syscalls with 5+ parameters, so diff --git a/lib/std/os/linux/arm.zig b/lib/std/os/linux/arm.zig index 10561da270..8f83bb60a1 100644 --- a/lib/std/os/linux/arm.zig +++ b/lib/std/os/linux/arm.zig @@ -170,8 +170,6 @@ pub fn restore_rt() callconv(.naked) noreturn { } } -pub const MMAP2_UNIT = 4096; - pub const F = struct { pub const DUPFD = 0; pub const GETFD = 1; diff --git a/lib/std/os/linux/hexagon.zig b/lib/std/os/linux/hexagon.zig index b6549fe4a9..dcc19cd96b 100644 --- a/lib/std/os/linux/hexagon.zig +++ b/lib/std/os/linux/hexagon.zig @@ -251,8 +251,6 @@ pub const Stat = extern struct { pub const Elf_Symndx = u32; -pub const MMAP2_UNIT = 4096; - pub const VDSO = void; /// TODO diff --git a/lib/std/os/linux/m68k.zig b/lib/std/os/linux/m68k.zig index 95277223c9..0b89c329a8 100644 --- a/lib/std/os/linux/m68k.zig +++ b/lib/std/os/linux/m68k.zig @@ -261,10 +261,6 @@ pub const Stat = extern struct { pub const Elf_Symndx = u32; -// m68k has multiple minimum page sizes. -// glibc sets MMAP2_PAGE_UNIT to -1 so it is queried at runtime. -pub const MMAP2_UNIT = -1; - // No VDSO used as of glibc 112a0ae18b831bf31f44d81b82666980312511d6. pub const VDSO = void; diff --git a/lib/std/os/linux/mips.zig b/lib/std/os/linux/mips.zig index b31d5e4fed..ed87b4f734 100644 --- a/lib/std/os/linux/mips.zig +++ b/lib/std/os/linux/mips.zig @@ -294,8 +294,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const MMAP2_UNIT = 4096; - pub const VDSO = struct { pub const CGT_SYM = "__vdso_clock_gettime"; pub const CGT_VER = "LINUX_2.6"; diff --git a/lib/std/os/linux/mips64.zig b/lib/std/os/linux/mips64.zig index 393d71dffe..11148fd5c9 100644 --- a/lib/std/os/linux/mips64.zig +++ b/lib/std/os/linux/mips64.zig @@ -273,8 +273,6 @@ pub const F = struct { pub const GETOWNER_UIDS = 17; }; -pub const MMAP2_UNIT = 4096; - pub const VDSO = struct { pub const CGT_SYM = "__vdso_clock_gettime"; pub const CGT_VER = "LINUX_2.6"; diff --git a/lib/std/os/linux/powerpc.zig b/lib/std/os/linux/powerpc.zig index 200744ce2d..443a32769a 100644 --- a/lib/std/os/linux/powerpc.zig +++ b/lib/std/os/linux/powerpc.zig @@ -348,7 +348,5 @@ pub const ucontext_t = extern struct { pub const Elf_Symndx = u32; -pub const MMAP2_UNIT = 4096; - /// TODO pub const getcontext = {}; diff --git a/lib/std/os/linux/riscv32.zig b/lib/std/os/linux/riscv32.zig index f9240ceafc..c37b0ee76c 100644 --- a/lib/std/os/linux/riscv32.zig +++ b/lib/std/os/linux/riscv32.zig @@ -256,8 +256,6 @@ pub const Stat = extern struct { pub const Elf_Symndx = u32; -pub const MMAP2_UNIT = 4096; - pub const VDSO = struct { pub const CGT_SYM = "__vdso_clock_gettime"; pub const CGT_VER = "LINUX_4.15"; diff --git a/lib/std/os/linux/tls.zig b/lib/std/os/linux/tls.zig index fdaac29e5d..3587512e65 100644 --- a/lib/std/os/linux/tls.zig +++ b/lib/std/os/linux/tls.zig @@ -508,14 +508,7 @@ pub fn initStatic(phdrs: []elf.Phdr) void { break :blk main_thread_area_buffer[0..area_desc.size]; } - const begin_addr = mmap( - null, - area_desc.size + area_desc.alignment - 1, - posix.PROT.READ | posix.PROT.WRITE, - .{ .TYPE = .PRIVATE, .ANONYMOUS = true }, - -1, - 0, - ); + const begin_addr = mmap_tls(area_desc.size + area_desc.alignment - 1); if (@call(.always_inline, linux.E.init, .{begin_addr}) != .SUCCESS) @trap(); const area_ptr: [*]align(page_size_min) u8 = @ptrFromInt(begin_addr); @@ -530,16 +523,19 @@ pub fn initStatic(phdrs: []elf.Phdr) void { setThreadPointer(tp_value); } -inline fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: linux.MAP, fd: i32, offset: i64) usize { +inline fn mmap_tls(length: usize) usize { + const prot = posix.PROT.READ | posix.PROT.WRITE; + const flags: linux.MAP = .{ .TYPE = .PRIVATE, .ANONYMOUS = true }; + if (@hasField(linux.SYS, "mmap2")) { return @call(.always_inline, linux.syscall6, .{ .mmap2, - @intFromPtr(address), + 0, length, prot, @as(u32, @bitCast(flags)), - @as(usize, @bitCast(@as(isize, fd))), - @as(usize, @truncate(@as(u64, @bitCast(offset)) / linux.MMAP2_UNIT)), + @as(usize, @bitCast(@as(isize, -1))), + 0, }); } else { // The s390x mmap() syscall existed before Linux supported syscalls with 5+ parameters, so @@ -547,21 +543,21 @@ inline fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: linux.MAP, fd return if (native_arch == .s390x) @call(.always_inline, linux.syscall1, .{ .mmap, @intFromPtr(&[_]usize{ - @intFromPtr(address), + 0, length, prot, @as(u32, @bitCast(flags)), - @as(usize, @bitCast(@as(isize, fd))), - @as(u64, @bitCast(offset)), + @as(usize, @bitCast(@as(isize, -1))), + 0, }), }) else @call(.always_inline, linux.syscall6, .{ .mmap, - @intFromPtr(address), + 0, length, prot, @as(u32, @bitCast(flags)), - @as(usize, @bitCast(@as(isize, fd))), - @as(u64, @bitCast(offset)), + @as(usize, @bitCast(@as(isize, -1))), + 0, }); } } diff --git a/lib/std/os/linux/x86.zig b/lib/std/os/linux/x86.zig index 4b4c76a4ca..cef0388206 100644 --- a/lib/std/os/linux/x86.zig +++ b/lib/std/os/linux/x86.zig @@ -230,8 +230,6 @@ pub const F = struct { pub const UNLCK = 2; }; -pub const MMAP2_UNIT = 4096; - pub const VDSO = struct { pub const CGT_SYM = "__vdso_clock_gettime"; pub const CGT_VER = "LINUX_2.6"; diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 2399fbc9c8..c693393ae2 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -82,7 +82,6 @@ pub const MADV = system.MADV; pub const MAP = system.MAP; pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN; pub const MFD = system.MFD; -pub const MMAP2_UNIT = system.MMAP2_UNIT; pub const MREMAP = system.MREMAP; pub const MSF = system.MSF; pub const MSG = system.MSG;