authorgravatar for stefan@s00.xyzStefan Weigl-Bosker <stefan@s00.xyz> 2025-03-29 18:31:55-04:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-04-07 13:37:01+02:00
logbcb4ba9afd610a0f3ab66ad828c90fd57dc95a6f
treef60101eaee6284a6bce0bed184b2b447f024fd6a
parent6fd358d287ee90de80769bd16b4321039acb570e
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.os.linux: use heap.pageSize() instead of MMAP2_UNIT


12 files changed, 15 insertions(+), 43 deletions(-)

lib/std/c.zig-4
......@@ -1601,10 +1601,6 @@ pub const MSF = switch (native_os) {
16011601 },
16021602 else => void,
16031603};
1604pub const MMAP2_UNIT = switch (native_os) {
1605 .linux => linux.MMAP2_UNIT,
1606 else => void,
1607};
16081604pub const NAME_MAX = switch (native_os) {
16091605 .linux => linux.NAME_MAX,
16101606 .emscripten => emscripten.NAME_MAX,
lib/std/os/linux.zig+1-2
......@@ -93,7 +93,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx;
9393pub const F = arch_bits.F;
9494pub const Flock = arch_bits.Flock;
9595pub const HWCAP = arch_bits.HWCAP;
96pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT;
9796pub const REG = arch_bits.REG;
9897pub const SC = arch_bits.SC;
9998pub const Stat = arch_bits.Stat;
......@@ -928,7 +927,7 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: MAP, fd: i32, of
928927 prot,
929928 @as(u32, @bitCast(flags)),
930929 @bitCast(@as(isize, fd)),
931 @truncate(@as(u64, @bitCast(offset)) / MMAP2_UNIT),
930 @truncate(@as(u64, @bitCast(offset)) / std.heap.pageSize()),
932931 );
933932 } else {
934933 // The s390x mmap() syscall existed before Linux supported syscalls with 5+ parameters, so
lib/std/os/linux/arm.zig-2
......@@ -170,8 +170,6 @@ pub fn restore_rt() callconv(.naked) noreturn {
170170 }
171171}
172172
173pub const MMAP2_UNIT = 4096;
174
175173pub const F = struct {
176174 pub const DUPFD = 0;
177175 pub const GETFD = 1;
lib/std/os/linux/hexagon.zig-2
......@@ -251,8 +251,6 @@ pub const Stat = extern struct {
251251
252252pub const Elf_Symndx = u32;
253253
254pub const MMAP2_UNIT = 4096;
255
256254pub const VDSO = void;
257255
258256/// TODO
lib/std/os/linux/m68k.zig-4
......@@ -261,10 +261,6 @@ pub const Stat = extern struct {
261261
262262pub const Elf_Symndx = u32;
263263
264// m68k has multiple minimum page sizes.
265// glibc sets MMAP2_PAGE_UNIT to -1 so it is queried at runtime.
266pub const MMAP2_UNIT = -1;
267
268264// No VDSO used as of glibc 112a0ae18b831bf31f44d81b82666980312511d6.
269265pub const VDSO = void;
270266
lib/std/os/linux/mips.zig-2
......@@ -294,8 +294,6 @@ pub const F = struct {
294294 pub const GETOWNER_UIDS = 17;
295295};
296296
297pub const MMAP2_UNIT = 4096;
298
299297pub const VDSO = struct {
300298 pub const CGT_SYM = "__vdso_clock_gettime";
301299 pub const CGT_VER = "LINUX_2.6";
lib/std/os/linux/mips64.zig-2
......@@ -273,8 +273,6 @@ pub const F = struct {
273273 pub const GETOWNER_UIDS = 17;
274274};
275275
276pub const MMAP2_UNIT = 4096;
277
278276pub const VDSO = struct {
279277 pub const CGT_SYM = "__vdso_clock_gettime";
280278 pub const CGT_VER = "LINUX_2.6";
lib/std/os/linux/powerpc.zig-2
......@@ -348,7 +348,5 @@ pub const ucontext_t = extern struct {
348348
349349pub const Elf_Symndx = u32;
350350
351pub const MMAP2_UNIT = 4096;
352
353351/// TODO
354352pub const getcontext = {};
lib/std/os/linux/riscv32.zig-2
......@@ -256,8 +256,6 @@ pub const Stat = extern struct {
256256
257257pub const Elf_Symndx = u32;
258258
259pub const MMAP2_UNIT = 4096;
260
261259pub const VDSO = struct {
262260 pub const CGT_SYM = "__vdso_clock_gettime";
263261 pub const CGT_VER = "LINUX_4.15";
lib/std/os/linux/tls.zig+14-18
......@@ -508,14 +508,7 @@ pub fn initStatic(phdrs: []elf.Phdr) void {
508508 break :blk main_thread_area_buffer[0..area_desc.size];
509509 }
510510
511 const begin_addr = mmap(
512 null,
513 area_desc.size + area_desc.alignment - 1,
514 posix.PROT.READ | posix.PROT.WRITE,
515 .{ .TYPE = .PRIVATE, .ANONYMOUS = true },
516 -1,
517 0,
518 );
511 const begin_addr = mmap_tls(area_desc.size + area_desc.alignment - 1);
519512 if (@call(.always_inline, linux.E.init, .{begin_addr}) != .SUCCESS) @trap();
520513
521514 const area_ptr: [*]align(page_size_min) u8 = @ptrFromInt(begin_addr);
......@@ -530,16 +523,19 @@ pub fn initStatic(phdrs: []elf.Phdr) void {
530523 setThreadPointer(tp_value);
531524}
532525
533inline fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: linux.MAP, fd: i32, offset: i64) usize {
526inline fn mmap_tls(length: usize) usize {
527 const prot = posix.PROT.READ | posix.PROT.WRITE;
528 const flags: linux.MAP = .{ .TYPE = .PRIVATE, .ANONYMOUS = true };
529
534530 if (@hasField(linux.SYS, "mmap2")) {
535531 return @call(.always_inline, linux.syscall6, .{
536532 .mmap2,
537 @intFromPtr(address),
533 0,
538534 length,
539535 prot,
540536 @as(u32, @bitCast(flags)),
541 @as(usize, @bitCast(@as(isize, fd))),
542 @as(usize, @truncate(@as(u64, @bitCast(offset)) / linux.MMAP2_UNIT)),
537 @as(usize, @bitCast(@as(isize, -1))),
538 0,
543539 });
544540 } else {
545541 // 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
547543 return if (native_arch == .s390x) @call(.always_inline, linux.syscall1, .{
548544 .mmap,
549545 @intFromPtr(&[_]usize{
550 @intFromPtr(address),
546 0,
551547 length,
552548 prot,
553549 @as(u32, @bitCast(flags)),
554 @as(usize, @bitCast(@as(isize, fd))),
555 @as(u64, @bitCast(offset)),
550 @as(usize, @bitCast(@as(isize, -1))),
551 0,
556552 }),
557553 }) else @call(.always_inline, linux.syscall6, .{
558554 .mmap,
559 @intFromPtr(address),
555 0,
560556 length,
561557 prot,
562558 @as(u32, @bitCast(flags)),
563 @as(usize, @bitCast(@as(isize, fd))),
564 @as(u64, @bitCast(offset)),
559 @as(usize, @bitCast(@as(isize, -1))),
560 0,
565561 });
566562 }
567563}
lib/std/os/linux/x86.zig-2
......@@ -230,8 +230,6 @@ pub const F = struct {
230230 pub const UNLCK = 2;
231231};
232232
233pub const MMAP2_UNIT = 4096;
234
235233pub const VDSO = struct {
236234 pub const CGT_SYM = "__vdso_clock_gettime";
237235 pub const CGT_VER = "LINUX_2.6";
lib/std/posix.zig-1
......@@ -82,7 +82,6 @@ pub const MADV = system.MADV;
8282pub const MAP = system.MAP;
8383pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN;
8484pub const MFD = system.MFD;
85pub const MMAP2_UNIT = system.MMAP2_UNIT;
8685pub const MREMAP = system.MREMAP;
8786pub const MSF = system.MSF;
8887pub const MSG = system.MSG;