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) {...@@ -1601,10 +1601,6 @@ pub const MSF = switch (native_os) {
1601 },1601 },
1602 else => void,1602 else => void,
1603};1603};
1604pub const MMAP2_UNIT = switch (native_os) {
1605 .linux => linux.MMAP2_UNIT,
1606 else => void,
1607};
1608pub const NAME_MAX = switch (native_os) {1604pub const NAME_MAX = switch (native_os) {
1609 .linux => linux.NAME_MAX,1605 .linux => linux.NAME_MAX,
1610 .emscripten => emscripten.NAME_MAX,1606 .emscripten => emscripten.NAME_MAX,
lib/std/os/linux.zig+1-2
...@@ -93,7 +93,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx;...@@ -93,7 +93,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx;
93pub const F = arch_bits.F;93pub const F = arch_bits.F;
94pub const Flock = arch_bits.Flock;94pub const Flock = arch_bits.Flock;
95pub const HWCAP = arch_bits.HWCAP;95pub const HWCAP = arch_bits.HWCAP;
96pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT;
97pub const REG = arch_bits.REG;96pub const REG = arch_bits.REG;
98pub const SC = arch_bits.SC;97pub const SC = arch_bits.SC;
99pub const Stat = arch_bits.Stat;98pub const Stat = arch_bits.Stat;
...@@ -928,7 +927,7 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: MAP, fd: i32, of...@@ -928,7 +927,7 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: MAP, fd: i32, of
928 prot,927 prot,
929 @as(u32, @bitCast(flags)),928 @as(u32, @bitCast(flags)),
930 @bitCast(@as(isize, fd)),929 @bitCast(@as(isize, fd)),
931 @truncate(@as(u64, @bitCast(offset)) / MMAP2_UNIT),930 @truncate(@as(u64, @bitCast(offset)) / std.heap.pageSize()),
932 );931 );
933 } else {932 } else {
934 // The s390x mmap() syscall existed before Linux supported syscalls with 5+ parameters, so933 // 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 {...@@ -170,8 +170,6 @@ pub fn restore_rt() callconv(.naked) noreturn {
170 }170 }
171}171}
172172
173pub const MMAP2_UNIT = 4096;
174
175pub const F = struct {173pub const F = struct {
176 pub const DUPFD = 0;174 pub const DUPFD = 0;
177 pub const GETFD = 1;175 pub const GETFD = 1;
lib/std/os/linux/hexagon.zig-2
...@@ -251,8 +251,6 @@ pub const Stat = extern struct {...@@ -251,8 +251,6 @@ pub const Stat = extern struct {
251251
252pub const Elf_Symndx = u32;252pub const Elf_Symndx = u32;
253253
254pub const MMAP2_UNIT = 4096;
255
256pub const VDSO = void;254pub const VDSO = void;
257255
258/// TODO256/// TODO
lib/std/os/linux/m68k.zig-4
...@@ -261,10 +261,6 @@ pub const Stat = extern struct {...@@ -261,10 +261,6 @@ pub const Stat = extern struct {
261261
262pub const Elf_Symndx = u32;262pub 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
268// No VDSO used as of glibc 112a0ae18b831bf31f44d81b82666980312511d6.264// No VDSO used as of glibc 112a0ae18b831bf31f44d81b82666980312511d6.
269pub const VDSO = void;265pub const VDSO = void;
270266
lib/std/os/linux/mips.zig-2
...@@ -294,8 +294,6 @@ pub const F = struct {...@@ -294,8 +294,6 @@ pub const F = struct {
294 pub const GETOWNER_UIDS = 17;294 pub const GETOWNER_UIDS = 17;
295};295};
296296
297pub const MMAP2_UNIT = 4096;
298
299pub const VDSO = struct {297pub const VDSO = struct {
300 pub const CGT_SYM = "__vdso_clock_gettime";298 pub const CGT_SYM = "__vdso_clock_gettime";
301 pub const CGT_VER = "LINUX_2.6";299 pub const CGT_VER = "LINUX_2.6";
lib/std/os/linux/mips64.zig-2
...@@ -273,8 +273,6 @@ pub const F = struct {...@@ -273,8 +273,6 @@ pub const F = struct {
273 pub const GETOWNER_UIDS = 17;273 pub const GETOWNER_UIDS = 17;
274};274};
275275
276pub const MMAP2_UNIT = 4096;
277
278pub const VDSO = struct {276pub const VDSO = struct {
279 pub const CGT_SYM = "__vdso_clock_gettime";277 pub const CGT_SYM = "__vdso_clock_gettime";
280 pub const CGT_VER = "LINUX_2.6";278 pub const CGT_VER = "LINUX_2.6";
lib/std/os/linux/powerpc.zig-2
...@@ -348,7 +348,5 @@ pub const ucontext_t = extern struct {...@@ -348,7 +348,5 @@ pub const ucontext_t = extern struct {
348348
349pub const Elf_Symndx = u32;349pub const Elf_Symndx = u32;
350350
351pub const MMAP2_UNIT = 4096;
352
353/// TODO351/// TODO
354pub const getcontext = {};352pub const getcontext = {};
lib/std/os/linux/riscv32.zig-2
...@@ -256,8 +256,6 @@ pub const Stat = extern struct {...@@ -256,8 +256,6 @@ pub const Stat = extern struct {
256256
257pub const Elf_Symndx = u32;257pub const Elf_Symndx = u32;
258258
259pub const MMAP2_UNIT = 4096;
260
261pub const VDSO = struct {259pub const VDSO = struct {
262 pub const CGT_SYM = "__vdso_clock_gettime";260 pub const CGT_SYM = "__vdso_clock_gettime";
263 pub const CGT_VER = "LINUX_4.15";261 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 {...@@ -508,14 +508,7 @@ pub fn initStatic(phdrs: []elf.Phdr) void {
508 break :blk main_thread_area_buffer[0..area_desc.size];508 break :blk main_thread_area_buffer[0..area_desc.size];
509 }509 }
510510
511 const begin_addr = mmap(511 const begin_addr = mmap_tls(area_desc.size + area_desc.alignment - 1);
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 );
519 if (@call(.always_inline, linux.E.init, .{begin_addr}) != .SUCCESS) @trap();512 if (@call(.always_inline, linux.E.init, .{begin_addr}) != .SUCCESS) @trap();
520513
521 const area_ptr: [*]align(page_size_min) u8 = @ptrFromInt(begin_addr);514 const area_ptr: [*]align(page_size_min) u8 = @ptrFromInt(begin_addr);
...@@ -530,16 +523,19 @@ pub fn initStatic(phdrs: []elf.Phdr) void {...@@ -530,16 +523,19 @@ pub fn initStatic(phdrs: []elf.Phdr) void {
530 setThreadPointer(tp_value);523 setThreadPointer(tp_value);
531}524}
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
534 if (@hasField(linux.SYS, "mmap2")) {530 if (@hasField(linux.SYS, "mmap2")) {
535 return @call(.always_inline, linux.syscall6, .{531 return @call(.always_inline, linux.syscall6, .{
536 .mmap2,532 .mmap2,
537 @intFromPtr(address),533 0,
538 length,534 length,
539 prot,535 prot,
540 @as(u32, @bitCast(flags)),536 @as(u32, @bitCast(flags)),
541 @as(usize, @bitCast(@as(isize, fd))),537 @as(usize, @bitCast(@as(isize, -1))),
542 @as(usize, @truncate(@as(u64, @bitCast(offset)) / linux.MMAP2_UNIT)),538 0,
543 });539 });
544 } else {540 } else {
545 // The s390x mmap() syscall existed before Linux supported syscalls with 5+ parameters, so541 // 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...@@ -547,21 +543,21 @@ inline fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: linux.MAP, fd
547 return if (native_arch == .s390x) @call(.always_inline, linux.syscall1, .{543 return if (native_arch == .s390x) @call(.always_inline, linux.syscall1, .{
548 .mmap,544 .mmap,
549 @intFromPtr(&[_]usize{545 @intFromPtr(&[_]usize{
550 @intFromPtr(address),546 0,
551 length,547 length,
552 prot,548 prot,
553 @as(u32, @bitCast(flags)),549 @as(u32, @bitCast(flags)),
554 @as(usize, @bitCast(@as(isize, fd))),550 @as(usize, @bitCast(@as(isize, -1))),
555 @as(u64, @bitCast(offset)),551 0,
556 }),552 }),
557 }) else @call(.always_inline, linux.syscall6, .{553 }) else @call(.always_inline, linux.syscall6, .{
558 .mmap,554 .mmap,
559 @intFromPtr(address),555 0,
560 length,556 length,
561 prot,557 prot,
562 @as(u32, @bitCast(flags)),558 @as(u32, @bitCast(flags)),
563 @as(usize, @bitCast(@as(isize, fd))),559 @as(usize, @bitCast(@as(isize, -1))),
564 @as(u64, @bitCast(offset)),560 0,
565 });561 });
566 }562 }
567}563}
lib/std/os/linux/x86.zig-2
...@@ -230,8 +230,6 @@ pub const F = struct {...@@ -230,8 +230,6 @@ pub const F = struct {
230 pub const UNLCK = 2;230 pub const UNLCK = 2;
231};231};
232232
233pub const MMAP2_UNIT = 4096;
234
235pub const VDSO = struct {233pub const VDSO = struct {
236 pub const CGT_SYM = "__vdso_clock_gettime";234 pub const CGT_SYM = "__vdso_clock_gettime";
237 pub const CGT_VER = "LINUX_2.6";235 pub const CGT_VER = "LINUX_2.6";
lib/std/posix.zig-1
...@@ -82,7 +82,6 @@ pub const MADV = system.MADV;...@@ -82,7 +82,6 @@ pub const MADV = system.MADV;
82pub const MAP = system.MAP;82pub const MAP = system.MAP;
83pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN;83pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN;
84pub const MFD = system.MFD;84pub const MFD = system.MFD;
85pub const MMAP2_UNIT = system.MMAP2_UNIT;
86pub const MREMAP = system.MREMAP;85pub const MREMAP = system.MREMAP;
87pub const MSF = system.MSF;86pub const MSF = system.MSF;
88pub const MSG = system.MSG;87pub const MSG = system.MSG;