authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-06 21:12:11-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-06 21:12:11-07:00
log9f3165540ed0e2b1fec0ab2eb16d4c06d899c86a
treec452568f46d251765d42d7db3d3b2d8471eae787
parent3da6043e2c3fa657cc7261e8cffdf3564c02dda9

std.os.linux.MAP: use a packed struct

Introduces type safety to this constant. Eliminates one use of `usingnamespace`.

20 files changed, 147 insertions(+), 155 deletions(-)

lib/std/Thread.zig+1-1
......@@ -1237,7 +1237,7 @@ const LinuxThreadImpl = struct {
12371237 null,
12381238 map_bytes,
12391239 os.PROT.NONE,
1240 os.MAP.PRIVATE | os.MAP.ANONYMOUS,
1240 .{ .TYPE = .PRIVATE, .ANONYMOUS = true },
12411241 -1,
12421242 0,
12431243 ) catch |err| switch (err) {
lib/std/crypto/tlcsprng.zig+1-1
......@@ -83,7 +83,7 @@ fn tlsCsprngFill(_: *anyopaque, buffer: []u8) void {
8383 null,
8484 @sizeOf(Context),
8585 os.PROT.READ | os.PROT.WRITE,
86 os.MAP.PRIVATE | os.MAP.ANONYMOUS,
86 .{ .TYPE = .PRIVATE, .ANONYMOUS = true },
8787 -1,
8888 0,
8989 ) catch {
lib/std/debug.zig+1-1
......@@ -1652,7 +1652,7 @@ fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {
16521652 null,
16531653 file_len,
16541654 os.PROT.READ,
1655 os.MAP.SHARED,
1655 .{ .TYPE = .SHARED },
16561656 file.handle,
16571657 0,
16581658 );
lib/std/dynamic_library.zig+4-4
......@@ -127,7 +127,7 @@ pub const ElfDynLib = struct {
127127 null,
128128 mem.alignForward(usize, size, mem.page_size),
129129 os.PROT.READ,
130 os.MAP.PRIVATE,
130 .{ .TYPE = .PRIVATE },
131131 fd,
132132 0,
133133 );
......@@ -165,7 +165,7 @@ pub const ElfDynLib = struct {
165165 null,
166166 virt_addr_end,
167167 os.PROT.NONE,
168 os.MAP.PRIVATE | os.MAP.ANONYMOUS,
168 .{ .TYPE = .PRIVATE, .ANONYMOUS = true },
169169 -1,
170170 0,
171171 );
......@@ -197,7 +197,7 @@ pub const ElfDynLib = struct {
197197 ptr,
198198 extended_memsz,
199199 prot,
200 os.MAP.PRIVATE | os.MAP.FIXED,
200 .{ .TYPE = .PRIVATE, .FIXED = true },
201201 fd,
202202 ph.p_offset - extra_bytes,
203203 );
......@@ -206,7 +206,7 @@ pub const ElfDynLib = struct {
206206 ptr,
207207 extended_memsz,
208208 prot,
209 os.MAP.PRIVATE | os.MAP.FIXED | os.MAP.ANONYMOUS,
209 .{ .TYPE = .PRIVATE, .FIXED = true, .ANONYMOUS = true },
210210 -1,
211211 0,
212212 );
lib/std/heap/PageAllocator.zig+1-1
......@@ -35,7 +35,7 @@ fn alloc(_: *anyopaque, n: usize, log2_align: u8, ra: usize) ?[*]u8 {
3535 hint,
3636 aligned_len,
3737 os.PROT.READ | os.PROT.WRITE,
38 os.MAP.PRIVATE | os.MAP.ANONYMOUS,
38 .{ .TYPE = .PRIVATE, .ANONYMOUS = true },
3939 -1,
4040 0,
4141 ) catch return null;
lib/std/os.zig+3-3
......@@ -4657,14 +4657,14 @@ pub fn mmap(
46574657 ptr: ?[*]align(mem.page_size) u8,
46584658 length: usize,
46594659 prot: u32,
4660 flags: u32,
4660 flags: system.MAP,
46614661 fd: fd_t,
46624662 offset: u64,
46634663) MMapError![]align(mem.page_size) u8 {
46644664 const mmap_sym = if (lfs64_abi) system.mmap64 else system.mmap;
46654665
4666 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
4667 const rc = mmap_sym(ptr, length, prot, flags, fd, ioffset);
4666 const ioffset: i64 = @bitCast(offset); // the OS treats this as unsigned
4667 const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, ioffset);
46684668 const err = if (builtin.link_libc) blk: {
46694669 if (rc != std.c.MAP.FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length];
46704670 break :blk @as(E, @enumFromInt(system._errno().*));
lib/std/os/linux.zig+130-30
......@@ -109,36 +109,136 @@ pub const SYS = switch (@import("builtin").cpu.arch) {
109109 else => @compileError("The Zig Standard Library is missing syscall definitions for the target CPU architecture"),
110110};
111111
112pub const MAP = struct {
113 pub usingnamespace arch_bits.MAP;
114
115 /// Share changes
116 pub const SHARED = 0x01;
117 /// Changes are private
118 pub const PRIVATE = 0x02;
119 /// share + validate extension flags
120 pub const SHARED_VALIDATE = 0x03;
121 /// Mask for type of mapping
122 pub const TYPE = 0x0f;
123 /// Interpret addr exactly
124 pub const FIXED = 0x10;
125 /// don't use a file
126 pub const ANONYMOUS = if (is_mips) 0x800 else 0x20;
127 // MAP_ 0x0100 - 0x4000 flags are per architecture
128 /// populate (prefault) pagetables
129 pub const POPULATE = if (is_mips) 0x10000 else 0x8000;
130 /// do not block on IO
131 pub const NONBLOCK = if (is_mips) 0x20000 else 0x10000;
132 /// give out an address that is best suited for process/thread stacks
133 pub const STACK = if (is_mips) 0x40000 else 0x20000;
134 /// create a huge page mapping
135 pub const HUGETLB = if (is_mips) 0x80000 else 0x40000;
136 /// perform synchronous page faults for the mapping
137 pub const SYNC = 0x80000;
138 /// MAP_FIXED which doesn't unmap underlying mapping
139 pub const FIXED_NOREPLACE = 0x100000;
140 /// For anonymous mmap, memory could be uninitialized
141 pub const UNINITIALIZED = 0x4000000;
112pub const MAP_TYPE = enum(u4) {
113 SHARED = 0x01,
114 PRIVATE = 0x02,
115 SHARED_VALIDATE = 0x03,
116};
117
118pub const MAP = switch (native_arch) {
119 .x86_64, .x86 => packed struct(u32) {
120 TYPE: MAP_TYPE,
121 FIXED: bool = false,
122 ANONYMOUS: bool = false,
123 @"32BIT": bool = false,
124 _7: u1 = 0,
125 GROWSDOWN: bool = false,
126 _9: u2 = 0,
127 DENYWRITE: bool = false,
128 EXECUTABLE: bool = false,
129 LOCKED: bool = false,
130 NORESERVE: bool = false,
131 POPULATE: bool = false,
132 NONBLOCK: bool = false,
133 STACK: bool = false,
134 HUGETLB: bool = false,
135 SYNC: bool = false,
136 FIXED_NOREPLACE: bool = false,
137 _21: u5 = 0,
138 UNINITIALIZED: bool = false,
139 _: u5 = 0,
140 },
141 .aarch64, .aarch64_be, .arm, .thumb => packed struct(u32) {
142 TYPE: MAP_TYPE,
143 FIXED: bool = false,
144 ANONYMOUS: bool = false,
145 _6: u2 = 0,
146 GROWSDOWN: bool = false,
147 _9: u2 = 0,
148 DENYWRITE: bool = false,
149 EXECUTABLE: bool = false,
150 LOCKED: bool = false,
151 NORESERVE: bool = false,
152 POPULATE: bool = false,
153 NONBLOCK: bool = false,
154 STACK: bool = false,
155 HUGETLB: bool = false,
156 SYNC: bool = false,
157 FIXED_NOREPLACE: bool = false,
158 _21: u5 = 0,
159 UNINITIALIZED: bool = false,
160 _: u5 = 0,
161 },
162 .riscv64 => packed struct(u32) {
163 TYPE: MAP_TYPE,
164 FIXED: bool = false,
165 ANONYMOUS: bool = false,
166 _6: u9 = 0,
167 POPULATE: bool = false,
168 NONBLOCK: bool = false,
169 STACK: bool = false,
170 HUGETLB: bool = false,
171 SYNC: bool = false,
172 FIXED_NOREPLACE: bool = false,
173 _21: u5 = 0,
174 UNINITIALIZED: bool = false,
175 _: u5 = 0,
176 },
177 .sparc64 => packed struct(u32) {
178 TYPE: MAP_TYPE,
179 FIXED: bool = false,
180 ANONYMOUS: bool = false,
181 NORESERVE: bool = false,
182 _7: u1 = 0,
183 LOCKED: bool = false,
184 GROWSDOWN: bool = false,
185 _10: u1 = 0,
186 DENYWRITE: bool = false,
187 EXECUTABLE: bool = false,
188 _13: u2 = 0,
189 POPULATE: bool = false,
190 NONBLOCK: bool = false,
191 STACK: bool = false,
192 HUGETLB: bool = false,
193 SYNC: bool = false,
194 FIXED_NOREPLACE: bool = false,
195 _21: u5 = 0,
196 UNINITIALIZED: bool = false,
197 _: u5 = 0,
198 },
199 .mips, .mipsel, .mips64, .mips64el => packed struct(u32) {
200 TYPE: MAP_TYPE,
201 FIXED: bool = false,
202 _5: u1 = 0,
203 @"32BIT": bool = false,
204 _7: u3 = 0,
205 NORESERVE: bool = false,
206 ANONYMOUS: bool = false,
207 GROWSDOWN: bool = false,
208 DENYWRITE: bool = false,
209 EXECUTABLE: bool = false,
210 LOCKED: bool = false,
211 POPULATE: bool = false,
212 NONBLOCK: bool = false,
213 STACK: bool = false,
214 HUGETLB: bool = false,
215 FIXED_NOREPLACE: bool = false,
216 _21: u5 = 0,
217 UNINITIALIZED: bool = false,
218 _: u5 = 0,
219 },
220 .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
221 TYPE: MAP_TYPE,
222 FIXED: bool = false,
223 ANONYMOUS: bool = false,
224 NORESERVE: bool = false,
225 LOCKED: bool = false,
226 GROWSDOWN: bool = false,
227 _9: u2 = 0,
228 DENYWRITE: bool = false,
229 EXECUTABLE: bool = false,
230 _13: u2 = 0,
231 POPULATE: bool = false,
232 NONBLOCK: bool = false,
233 STACK: bool = false,
234 HUGETLB: bool = false,
235 SYNC: bool = false,
236 FIXED_NOREPLACE: bool = false,
237 _21: u5 = 0,
238 UNINITIALIZED: bool = false,
239 _: u5 = 0,
240 },
241 else => @compileError("missing std.os.linux.MAP constants for this architecture"),
142242};
143243
144244pub const O = struct {
lib/std/os/linux/arm-eabi.zig-13
......@@ -197,19 +197,6 @@ pub const LOCK = struct {
197197 pub const NB = 4;
198198};
199199
200pub const MAP = struct {
201 /// stack-like segment
202 pub const GROWSDOWN = 0x0100;
203 /// ETXTBSY
204 pub const DENYWRITE = 0x0800;
205 /// mark it as an executable
206 pub const EXECUTABLE = 0x1000;
207 /// pages are locked
208 pub const LOCKED = 0x2000;
209 /// don't check for reservations
210 pub const NORESERVE = 0x4000;
211};
212
213200pub const VDSO = struct {
214201 pub const CGT_SYM = "__vdso_clock_gettime";
215202 pub const CGT_VER = "LINUX_2.6";
lib/std/os/linux/arm64.zig-13
......@@ -179,19 +179,6 @@ pub const LOCK = struct {
179179 pub const NB = 4;
180180};
181181
182pub const MAP = struct {
183 /// stack-like segment
184 pub const GROWSDOWN = 0x0100;
185 /// ETXTBSY
186 pub const DENYWRITE = 0x0800;
187 /// mark it as an executable
188 pub const EXECUTABLE = 0x1000;
189 /// pages are locked
190 pub const LOCKED = 0x2000;
191 /// don't check for reservations
192 pub const NORESERVE = 0x4000;
193};
194
195182pub const VDSO = struct {
196183 pub const CGT_SYM = "__kernel_clock_gettime";
197184 pub const CGT_VER = "LINUX_2.6.39";
lib/std/os/linux/io_uring.zig+2-2
......@@ -1344,7 +1344,7 @@ pub const SubmissionQueue = struct {
13441344 null,
13451345 size,
13461346 os.PROT.READ | os.PROT.WRITE,
1347 os.MAP.SHARED | os.MAP.POPULATE,
1347 .{ .TYPE = .SHARED, .POPULATE = true },
13481348 fd,
13491349 linux.IORING_OFF_SQ_RING,
13501350 );
......@@ -1358,7 +1358,7 @@ pub const SubmissionQueue = struct {
13581358 null,
13591359 size_sqes,
13601360 os.PROT.READ | os.PROT.WRITE,
1361 os.MAP.SHARED | os.MAP.POPULATE,
1361 .{ .TYPE = .SHARED, .POPULATE = true },
13621362 fd,
13631363 linux.IORING_OFF_SQES,
13641364 );
lib/std/os/linux/mips.zig-9
......@@ -271,15 +271,6 @@ pub const LOCK = struct {
271271
272272pub const MMAP2_UNIT = 4096;
273273
274pub const MAP = struct {
275 pub const NORESERVE = 0x0400;
276 pub const GROWSDOWN = 0x1000;
277 pub const DENYWRITE = 0x2000;
278 pub const EXECUTABLE = 0x4000;
279 pub const LOCKED = 0x8000;
280 pub const @"32BIT" = 0x40;
281};
282
283274pub const VDSO = struct {
284275 pub const CGT_SYM = "__kernel_clock_gettime";
285276 pub const CGT_VER = "LINUX_2.6.39";
lib/std/os/linux/mips64.zig-9
......@@ -256,15 +256,6 @@ pub const LOCK = struct {
256256
257257pub const MMAP2_UNIT = 4096;
258258
259pub const MAP = struct {
260 pub const NORESERVE = 0x0400;
261 pub const GROWSDOWN = 0x1000;
262 pub const DENYWRITE = 0x2000;
263 pub const EXECUTABLE = 0x4000;
264 pub const LOCKED = 0x8000;
265 pub const @"32BIT" = 0x40;
266};
267
268259pub const VDSO = struct {
269260 pub const CGT_SYM = "__kernel_clock_gettime";
270261 pub const CGT_VER = "LINUX_2.6.39";
lib/std/os/linux/powerpc.zig-13
......@@ -198,19 +198,6 @@ pub const LOCK = struct {
198198 pub const NB = 4;
199199};
200200
201pub const MAP = struct {
202 /// stack-like segment
203 pub const GROWSDOWN = 0x0100;
204 /// ETXTBSY
205 pub const DENYWRITE = 0x0800;
206 /// mark it as an executable
207 pub const EXECUTABLE = 0x1000;
208 /// pages are locked
209 pub const LOCKED = 0x0080;
210 /// don't check for reservations
211 pub const NORESERVE = 0x0040;
212};
213
214201pub const VDSO = struct {
215202 pub const CGT_SYM = "__kernel_clock_gettime";
216203 pub const CGT_VER = "LINUX_2.6.15";
lib/std/os/linux/powerpc64.zig-13
......@@ -198,19 +198,6 @@ pub const LOCK = struct {
198198 pub const NB = 4;
199199};
200200
201pub const MAP = struct {
202 /// stack-like segment
203 pub const GROWSDOWN = 0x0100;
204 /// ETXTBSY
205 pub const DENYWRITE = 0x0800;
206 /// mark it as an executable
207 pub const EXECUTABLE = 0x1000;
208 /// pages are locked
209 pub const LOCKED = 0x0080;
210 /// don't check for reservations
211 pub const NORESERVE = 0x0040;
212};
213
214201pub const VDSO = struct {
215202 pub const CGT_SYM = "__kernel_clock_gettime";
216203 pub const CGT_VER = "LINUX_2.6.15";
lib/std/os/linux/riscv64.zig-1
......@@ -246,4 +246,3 @@ pub const Stat = extern struct {
246246pub const Elf_Symndx = u32;
247247
248248pub const VDSO = struct {};
249pub const MAP = struct {};
lib/std/os/linux/sparc64.zig-13
......@@ -248,19 +248,6 @@ pub const LOCK = struct {
248248 pub const UN = 8;
249249};
250250
251pub const MAP = struct {
252 /// stack-like segment
253 pub const GROWSDOWN = 0x0200;
254 /// ETXTBSY
255 pub const DENYWRITE = 0x0800;
256 /// mark it as an executable
257 pub const EXECUTABLE = 0x1000;
258 /// pages are locked
259 pub const LOCKED = 0x0100;
260 /// don't check for reservations
261 pub const NORESERVE = 0x0040;
262};
263
264251pub const VDSO = struct {
265252 pub const CGT_SYM = "__vdso_clock_gettime";
266253 pub const CGT_VER = "LINUX_2.6";
lib/std/os/linux/tls.zig+1-1
......@@ -324,7 +324,7 @@ pub fn initStaticTLS(phdrs: []elf.Phdr) void {
324324 null,
325325 tls_image.alloc_size + tls_image.alloc_align - 1,
326326 os.PROT.READ | os.PROT.WRITE,
327 os.MAP.PRIVATE | os.MAP.ANONYMOUS,
327 .{ .TYPE = .PRIVATE, .ANONYMOUS = true },
328328 -1,
329329 0,
330330 ) catch os.abort();
lib/std/os/linux/x86.zig-9
......@@ -211,15 +211,6 @@ pub const LOCK = struct {
211211 pub const UN = 8;
212212};
213213
214pub const MAP = struct {
215 pub const NORESERVE = 0x4000;
216 pub const GROWSDOWN = 0x0100;
217 pub const DENYWRITE = 0x0800;
218 pub const EXECUTABLE = 0x1000;
219 pub const LOCKED = 0x2000;
220 pub const @"32BIT" = 0x40;
221};
222
223214pub const MMAP2_UNIT = 4096;
224215
225216pub const VDSO = struct {
lib/std/os/linux/x86_64.zig-15
......@@ -177,21 +177,6 @@ pub const F = struct {
177177 pub const UNLCK = 2;
178178};
179179
180pub const MAP = struct {
181 /// only give out 32bit addresses
182 pub const @"32BIT" = 0x40;
183 /// stack-like segment
184 pub const GROWSDOWN = 0x0100;
185 /// ETXTBSY
186 pub const DENYWRITE = 0x0800;
187 /// mark it as an executable
188 pub const EXECUTABLE = 0x1000;
189 /// pages are locked
190 pub const LOCKED = 0x2000;
191 /// don't check for reservations
192 pub const NORESERVE = 0x4000;
193};
194
195180pub const VDSO = struct {
196181 pub const CGT_SYM = "__vdso_clock_gettime";
197182 pub const CGT_VER = "LINUX_2.6";
lib/std/os/test.zig+3-3
......@@ -576,7 +576,7 @@ test "mmap" {
576576 null,
577577 1234,
578578 os.PROT.READ | os.PROT.WRITE,
579 os.MAP.ANONYMOUS | os.MAP.PRIVATE,
579 .{ .TYPE = .PRIVATE, .ANONYMOUS = true },
580580 -1,
581581 0,
582582 );
......@@ -618,7 +618,7 @@ test "mmap" {
618618 null,
619619 alloc_size,
620620 os.PROT.READ,
621 os.MAP.PRIVATE,
621 .{ .TYPE = .PRIVATE },
622622 file.handle,
623623 0,
624624 );
......@@ -642,7 +642,7 @@ test "mmap" {
642642 null,
643643 alloc_size / 2,
644644 os.PROT.READ,
645 os.MAP.PRIVATE,
645 .{ .TYPE = .PRIVATE },
646646 file.handle,
647647 alloc_size / 2,
648648 );