Skip to content

Commit bcb4ba9

Browse files
sweiglboskeralexrp
authored andcommitted
std.os.linux: use heap.pageSize() instead of MMAP2_UNIT
1 parent 6fd358d commit bcb4ba9

File tree

12 files changed

+15
-43
lines changed

12 files changed

+15
-43
lines changed

lib/std/c.zig

-4
Original file line numberDiff line numberDiff line change
@@ -1601,10 +1601,6 @@ pub const MSF = switch (native_os) {
16011601
},
16021602
else => void,
16031603
};
1604-
pub const MMAP2_UNIT = switch (native_os) {
1605-
.linux => linux.MMAP2_UNIT,
1606-
else => void,
1607-
};
16081604
pub const NAME_MAX = switch (native_os) {
16091605
.linux => linux.NAME_MAX,
16101606
.emscripten => emscripten.NAME_MAX,

lib/std/os/linux.zig

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx;
9393
pub const F = arch_bits.F;
9494
pub const Flock = arch_bits.Flock;
9595
pub const HWCAP = arch_bits.HWCAP;
96-
pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT;
9796
pub const REG = arch_bits.REG;
9897
pub const SC = arch_bits.SC;
9998
pub 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
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ pub fn restore_rt() callconv(.naked) noreturn {
170170
}
171171
}
172172

173-
pub const MMAP2_UNIT = 4096;
174-
175173
pub const F = struct {
176174
pub const DUPFD = 0;
177175
pub const GETFD = 1;

lib/std/os/linux/hexagon.zig

-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ pub const Stat = extern struct {
251251

252252
pub const Elf_Symndx = u32;
253253

254-
pub const MMAP2_UNIT = 4096;
255-
256254
pub const VDSO = void;
257255

258256
/// TODO

lib/std/os/linux/m68k.zig

-4
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,6 @@ pub const Stat = extern struct {
261261

262262
pub 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.
266-
pub const MMAP2_UNIT = -1;
267-
268264
// No VDSO used as of glibc 112a0ae18b831bf31f44d81b82666980312511d6.
269265
pub const VDSO = void;
270266

lib/std/os/linux/mips.zig

-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ pub const F = struct {
294294
pub const GETOWNER_UIDS = 17;
295295
};
296296

297-
pub const MMAP2_UNIT = 4096;
298-
299297
pub 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
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,6 @@ pub const F = struct {
273273
pub const GETOWNER_UIDS = 17;
274274
};
275275

276-
pub const MMAP2_UNIT = 4096;
277-
278276
pub 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
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,5 @@ pub const ucontext_t = extern struct {
348348

349349
pub const Elf_Symndx = u32;
350350

351-
pub const MMAP2_UNIT = 4096;
352-
353351
/// TODO
354352
pub const getcontext = {};

lib/std/os/linux/riscv32.zig

-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ pub const Stat = extern struct {
256256

257257
pub const Elf_Symndx = u32;
258258

259-
pub const MMAP2_UNIT = 4096;
260-
261259
pub 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
Original file line numberDiff line numberDiff line change
@@ -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,38 +523,41 @@ pub fn initStatic(phdrs: []elf.Phdr) void {
530523
setThreadPointer(tp_value);
531524
}
532525

533-
inline fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: linux.MAP, fd: i32, offset: i64) usize {
526+
inline 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
546542
// it takes a single pointer to an array of arguments instead.
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
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ pub const F = struct {
230230
pub const UNLCK = 2;
231231
};
232232

233-
pub const MMAP2_UNIT = 4096;
234-
235233
pub const VDSO = struct {
236234
pub const CGT_SYM = "__vdso_clock_gettime";
237235
pub const CGT_VER = "LINUX_2.6";

lib/std/posix.zig

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ pub const MADV = system.MADV;
8282
pub const MAP = system.MAP;
8383
pub const MAX_ADDR_LEN = system.MAX_ADDR_LEN;
8484
pub const MFD = system.MFD;
85-
pub const MMAP2_UNIT = system.MMAP2_UNIT;
8685
pub const MREMAP = system.MREMAP;
8786
pub const MSF = system.MSF;
8887
pub const MSG = system.MSG;

0 commit comments

Comments
 (0)