Commit bcb4ba9afd

Stefan Weigl-Bosker <stefan@s00.xyz>
2025-03-29 23:31:55
std.os.linux: use heap.pageSize() instead of MMAP2_UNIT
1 parent 6fd358d
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;
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
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;
 
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";
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";
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 = {};
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";
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,
         });
     }
 }
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";
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
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,
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;