authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-04-08 06:04:16+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-04-08 06:04:16+02:00
log2d33cc2e42d40ce0ee798d4d56c2d7da93ead44a
treebad7965f8787aaf5a84b63996cb384d0866f0743
parentd8153fa74ab01f8cdc117e0049e0a2d2e4187892
parentbcb4ba9afd610a0f3ab66ad828c90fd57dc95a6f
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #23376 from sweiglbosker/m68k-archbits


13 files changed, 324 insertions(+), 51 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+32-11
......@@ -39,6 +39,7 @@ const arch_bits = switch (native_arch) {
3939 .riscv64 => @import("linux/riscv64.zig"),
4040 .sparc64 => @import("linux/sparc64.zig"),
4141 .loongarch64 => @import("linux/loongarch64.zig"),
42 .m68k => @import("linux/m68k.zig"),
4243 .mips, .mipsel => @import("linux/mips.zig"),
4344 .mips64, .mips64el => @import("linux/mips64.zig"),
4445 .powerpc, .powerpcle => @import("linux/powerpc.zig"),
......@@ -92,7 +93,6 @@ pub const Elf_Symndx = arch_bits.Elf_Symndx;
9293pub const F = arch_bits.F;
9394pub const Flock = arch_bits.Flock;
9495pub const HWCAP = arch_bits.HWCAP;
95pub const MMAP2_UNIT = arch_bits.MMAP2_UNIT;
9696pub const REG = arch_bits.REG;
9797pub const SC = arch_bits.SC;
9898pub const Stat = arch_bits.Stat;
......@@ -279,7 +279,7 @@ pub const MAP = switch (native_arch) {
279279 UNINITIALIZED: bool = false,
280280 _: u5 = 0,
281281 },
282 .hexagon, .s390x => packed struct(u32) {
282 .hexagon, .m68k, .s390x => packed struct(u32) {
283283 TYPE: MAP_TYPE,
284284 FIXED: bool = false,
285285 ANONYMOUS: bool = false,
......@@ -333,7 +333,7 @@ pub const O = switch (native_arch) {
333333 SYNC: bool = false,
334334 PATH: bool = false,
335335 TMPFILE: bool = false,
336 _: u9 = 0,
336 _23: u9 = 0,
337337 },
338338 .x86, .riscv32, .riscv64, .loongarch64 => packed struct(u32) {
339339 ACCMODE: ACCMODE = .RDONLY,
......@@ -355,7 +355,7 @@ pub const O = switch (native_arch) {
355355 SYNC: bool = false,
356356 PATH: bool = false,
357357 TMPFILE: bool = false,
358 _: u9 = 0,
358 _23: u9 = 0,
359359 },
360360 .aarch64, .aarch64_be, .arm, .armeb, .thumb, .thumbeb => packed struct(u32) {
361361 ACCMODE: ACCMODE = .RDONLY,
......@@ -377,7 +377,7 @@ pub const O = switch (native_arch) {
377377 SYNC: bool = false,
378378 PATH: bool = false,
379379 TMPFILE: bool = false,
380 _: u9 = 0,
380 _23: u9 = 0,
381381 },
382382 .sparc64 => packed struct(u32) {
383383 ACCMODE: ACCMODE = .RDONLY,
......@@ -402,7 +402,7 @@ pub const O = switch (native_arch) {
402402 SYNC: bool = false,
403403 PATH: bool = false,
404404 TMPFILE: bool = false,
405 _: u6 = 0,
405 _27: u6 = 0,
406406 },
407407 .mips, .mipsel, .mips64, .mips64el => packed struct(u32) {
408408 ACCMODE: ACCMODE = .RDONLY,
......@@ -426,7 +426,7 @@ pub const O = switch (native_arch) {
426426 _20: u1 = 0,
427427 PATH: bool = false,
428428 TMPFILE: bool = false,
429 _: u9 = 0,
429 _23: u9 = 0,
430430 },
431431 .powerpc, .powerpcle, .powerpc64, .powerpc64le => packed struct(u32) {
432432 ACCMODE: ACCMODE = .RDONLY,
......@@ -448,7 +448,7 @@ pub const O = switch (native_arch) {
448448 SYNC: bool = false,
449449 PATH: bool = false,
450450 TMPFILE: bool = false,
451 _: u9 = 0,
451 _23: u9 = 0,
452452 },
453453 .hexagon, .s390x => packed struct(u32) {
454454 ACCMODE: ACCMODE = .RDONLY,
......@@ -467,15 +467,36 @@ pub const O = switch (native_arch) {
467467 NOFOLLOW: bool = false,
468468 NOATIME: bool = false,
469469 CLOEXEC: bool = false,
470 _17: u1 = 0,
470 _20: u1 = 0,
471471 PATH: bool = false,
472 _: u10 = 0,
472 _22: u10 = 0,
473473
474474 // #define O_RSYNC 04010000
475475 // #define O_SYNC 04010000
476476 // #define O_TMPFILE 020200000
477477 // #define O_NDELAY O_NONBLOCK
478478 },
479 .m68k => packed struct(u32) {
480 ACCMODE: ACCMODE = .RDONLY,
481 _2: u4 = 0,
482 CREAT: bool = false,
483 EXCL: bool = false,
484 NOCTTY: bool = false,
485 TRUNC: bool = false,
486 APPEND: bool = false,
487 NONBLOCK: bool = false,
488 DSYNC: bool = false,
489 ASYNC: bool = false,
490 DIRECTORY: bool = false,
491 NOFOLLOW: bool = false,
492 DIRECT: bool = false,
493 LARGEFILE: bool = false,
494 NOATIME: bool = false,
495 CLOEXEC: bool = false,
496 _20: u1 = 0,
497 PATH: bool = false,
498 _22: u10 = 0,
499 },
479500 else => @compileError("missing std.os.linux.O constants for this architecture"),
480501};
481502
......@@ -906,7 +927,7 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: MAP, fd: i32, of
906927 prot,
907928 @as(u32, @bitCast(flags)),
908929 @bitCast(@as(isize, fd)),
909 @truncate(@as(u64, @bitCast(offset)) / MMAP2_UNIT),
930 @truncate(@as(u64, @bitCast(offset)) / std.heap.pageSize()),
910931 );
911932 } else {
912933 // 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 created+271
......@@ -0,0 +1,271 @@
1const builtin = @import("builtin");
2const std = @import("../../std.zig");
3const iovec = std.posix.iovec;
4const iovec_const = std.posix.iovec_const;
5const linux = std.os.linux;
6const SYS = linux.SYS;
7const uid_t = std.os.linux.uid_t;
8const gid_t = std.os.linux.uid_t;
9const pid_t = std.os.linux.pid_t;
10const sockaddr = linux.sockaddr;
11const socklen_t = linux.socklen_t;
12const timespec = std.os.linux.timespec;
13
14pub fn syscall0(number: SYS) usize {
15 return asm volatile ("trap #0"
16 : [ret] "={d0}" (-> usize),
17 : [number] "{d0}" (@intFromEnum(number)),
18 : "memory"
19 );
20}
21
22pub fn syscall1(number: SYS, arg1: usize) usize {
23 return asm volatile ("trap #0"
24 : [ret] "={d0}" (-> usize),
25 : [number] "{d0}" (@intFromEnum(number)),
26 [arg1] "{d1}" (arg1),
27 : "memory"
28 );
29}
30
31pub fn syscall2(number: SYS, arg1: usize, arg2: usize) usize {
32 return asm volatile ("trap #0"
33 : [ret] "={d0}" (-> usize),
34 : [number] "{d0}" (@intFromEnum(number)),
35 [arg1] "{d1}" (arg1),
36 [arg2] "{d2}" (arg2),
37 : "memory"
38 );
39}
40
41pub fn syscall3(number: SYS, arg1: usize, arg2: usize, arg3: usize) usize {
42 return asm volatile ("trap #0"
43 : [ret] "={d0}" (-> usize),
44 : [number] "{d0}" (@intFromEnum(number)),
45 [arg1] "{d1}" (arg1),
46 [arg2] "{d2}" (arg2),
47 [arg3] "{d3}" (arg3),
48 : "memory"
49 );
50}
51
52pub fn syscall4(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
53 return asm volatile ("trap #0"
54 : [ret] "={d0}" (-> usize),
55 : [number] "{d0}" (@intFromEnum(number)),
56 [arg1] "{d1}" (arg1),
57 [arg2] "{d2}" (arg2),
58 [arg3] "{d3}" (arg3),
59 [arg4] "{d4}" (arg4),
60 : "memory"
61 );
62}
63
64pub fn syscall5(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
65 return asm volatile ("trap #0"
66 : [ret] "={d0}" (-> usize),
67 : [number] "{d0}" (@intFromEnum(number)),
68 [arg1] "{d1}" (arg1),
69 [arg2] "{d2}" (arg2),
70 [arg3] "{d3}" (arg3),
71 [arg4] "{d4}" (arg4),
72 [arg5] "{d5}" (arg5),
73 : "memory"
74 );
75}
76
77pub fn syscall6(
78 number: SYS,
79 arg1: usize,
80 arg2: usize,
81 arg3: usize,
82 arg4: usize,
83 arg5: usize,
84 arg6: usize,
85) usize {
86 return asm volatile ("trap #0"
87 : [ret] "={d0}" (-> usize),
88 : [number] "{d0}" (@intFromEnum(number)),
89 [arg1] "{d1}" (arg1),
90 [arg2] "{d2}" (arg2),
91 [arg3] "{d3}" (arg3),
92 [arg4] "{d4}" (arg4),
93 [arg5] "{d5}" (arg5),
94 [arg6] "{a0}" (arg6),
95 : "memory"
96 );
97}
98
99pub fn clone() callconv(.naked) usize {
100 // __clone(func, stack, flags, arg, ptid, tls, ctid)
101 // +4, +8, +12, +16, +20, +24, +28
102 //
103 // syscall(SYS_clone, flags, stack, ptid, ctid, tls)
104 // d0, d1, d2, d3, d4, d5
105 asm volatile (
106 \\ // Save callee-saved registers.
107 \\ movem.l %%d2-%%d5, -(%%sp) // sp -= 16
108 \\
109 \\ // Save func and arg.
110 \\ move.l 16+4(%%sp), %%a0
111 \\ move.l 16+16(%%sp), %%a1
112 \\
113 \\ // d0 = syscall(d0, d1, d2, d3, d4, d5)
114 \\ move.l #120, %%d0 // SYS_clone
115 \\ move.l 16+12(%%sp), %%d1
116 \\ move.l 16+8(%%sp), %%d2
117 \\ move.l 16+20(%%sp), %%d3
118 \\ move.l 16+28(%%sp), %%d4
119 \\ move.l 16+24(%%sp), %%d5
120 \\ and.l #-4, %%d2 // Align the child stack pointer.
121 \\ trap #0
122 \\
123 \\ // Are we in the parent or child?
124 \\ tst.l %%d0
125 \\ beq 1f
126 \\ // Parent:
127 \\
128 \\ // Restore callee-saved registers and return.
129 \\ movem.l (%%sp)+, %%d2-%%d5 // sp += 16
130 \\ rts
131 \\
132 \\ // Child:
133 \\1:
134 );
135 if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
136 \\ .cfi_undefined %%pc
137 );
138 asm volatile (
139 \\ suba.l %%fp, %%fp
140 \\
141 \\ // d0 = func(a1)
142 \\ move.l %%a1, -(%%sp)
143 \\ jsr (%%a0)
144 \\
145 \\ // syscall(d0, d1)
146 \\ move.l %%d0, %%d1
147 \\ move.l #1, %%d0 // SYS_exit
148 \\ trap #0
149 );
150}
151
152pub const restore = restore_rt;
153
154pub fn restore_rt() callconv(.naked) noreturn {
155 asm volatile ("trap #0"
156 :
157 : [number] "{d0}" (@intFromEnum(SYS.rt_sigreturn)),
158 : "memory"
159 );
160}
161
162pub const F = struct {
163 pub const DUPFD = 0;
164 pub const GETFD = 1;
165 pub const SETFD = 2;
166 pub const GETFL = 3;
167 pub const SETFL = 4;
168
169 pub const SETOWN = 8;
170 pub const GETOWN = 9;
171 pub const SETSIG = 10;
172 pub const GETSIG = 11;
173
174 pub const GETLK = 12;
175 pub const SETLK = 13;
176 pub const SETLKW = 14;
177
178 pub const SETOWN_EX = 15;
179 pub const GETOWN_EX = 16;
180
181 pub const GETOWNER_UIDS = 17;
182
183 pub const RDLCK = 0;
184 pub const WRLCK = 1;
185 pub const UNLCK = 2;
186};
187
188pub const blksize_t = i32;
189pub const nlink_t = u32;
190pub const time_t = i32;
191pub const mode_t = u32;
192pub const off_t = i64;
193pub const ino_t = u64;
194pub const dev_t = u64;
195pub const blkcnt_t = i64;
196
197pub const timeval = extern struct {
198 sec: time_t,
199 usec: i32,
200};
201
202pub const Flock = extern struct {
203 type: i16,
204 whence: i16,
205 start: off_t,
206 len: off_t,
207 pid: pid_t,
208};
209
210// TODO: not 100% sure of padding for msghdr
211pub const msghdr = extern struct {
212 name: ?*sockaddr,
213 namelen: socklen_t,
214 iov: [*]iovec,
215 iovlen: i32,
216 control: ?*anyopaque,
217 controllen: socklen_t,
218 flags: i32,
219};
220
221pub const msghdr_const = extern struct {
222 name: ?*const sockaddr,
223 namelen: socklen_t,
224 iov: [*]const iovec_const,
225 iovlen: i32,
226 control: ?*const anyopaque,
227 controllen: socklen_t,
228 flags: i32,
229};
230
231pub const Stat = extern struct {
232 dev: dev_t,
233 __pad: i16,
234 __ino_truncated: i32,
235 mode: mode_t,
236 nlink: nlink_t,
237 uid: uid_t,
238 gid: gid_t,
239 rdev: dev_t,
240 __pad2: i16,
241 size: off_t,
242 blksize: blksize_t,
243 blocks: blkcnt_t,
244 atim: timespec,
245 mtim: timespec,
246 ctim: timespec,
247 ino: ino_t,
248
249 pub fn atime(self: @This()) timespec {
250 return self.atim;
251 }
252
253 pub fn mtime(self: @This()) timespec {
254 return self.mtim;
255 }
256
257 pub fn ctime(self: @This()) timespec {
258 return self.ctim;
259 }
260};
261
262pub const Elf_Symndx = u32;
263
264// No VDSO used as of glibc 112a0ae18b831bf31f44d81b82666980312511d6.
265pub const VDSO = void;
266
267/// TODO
268pub const ucontext_t = void;
269
270/// TODO
271pub const getcontext = {};
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;
lib/std/start.zig+7-3
......@@ -250,7 +250,7 @@ fn _start() callconv(.naked) noreturn {
250250 .csky => ".cfi_undefined lr",
251251 .hexagon => ".cfi_undefined r31",
252252 .loongarch32, .loongarch64 => ".cfi_undefined 1",
253 .m68k => ".cfi_undefined pc",
253 .m68k => ".cfi_undefined %%pc",
254254 .mips, .mipsel, .mips64, .mips64el => ".cfi_undefined $ra",
255255 .powerpc, .powerpcle, .powerpc64, .powerpc64le => ".cfi_undefined lr",
256256 .riscv32, .riscv64 => if (builtin.zig_backend == .stage2_riscv64)
......@@ -343,7 +343,7 @@ fn _start() callconv(.naked) noreturn {
343343 \\ r30 = #0
344344 \\ r31 = #0
345345 \\ r0 = r29
346 \\ r29 = and(r29, #-16)
346 \\ r29 = and(r29, #-8)
347347 \\ memw(r29 + #-8) = r29
348348 \\ r29 = add(r29, #-8)
349349 \\ call %[posixCallMainAndExit]
......@@ -366,7 +366,11 @@ fn _start() callconv(.naked) noreturn {
366366 // Note that the - 8 is needed because pc in the jsr instruction points into the middle
367367 // of the jsr instruction. (The lea is 6 bytes, the jsr is 4 bytes.)
368368 \\ suba.l %%fp, %%fp
369 \\ move.l %%sp, -(%%sp)
369 \\ move.l %%sp, %%a0
370 \\ move.l %%a0, %%d0
371 \\ and.l #-4, %%d0
372 \\ move.l %%d0, %%sp
373 \\ move.l %%a0, -(%%sp)
370374 \\ lea %[posixCallMainAndExit] - . - 8, %%a0
371375 \\ jsr (%%pc, %%a0)
372376 ,