authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-09-24 13:35:12-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-09-24 13:35:12-07:00
log5e4da1ff30d673d96809497e9bce555edef813a7
tree69d973147adbe7bb8662cad2f3c3e3f98504c822
parent8d76c02f9a7ca664ee669e1f9c1a70b841eb2774
signaturebadge-check Signed by PGP key B5690EEEBB952194

std: add arch bits for s390x-linux (#21342)

see #21402

5 files changed, 333 insertions(+), 0 deletions(-)

lib/std/os/linux.zig+50
......@@ -47,6 +47,7 @@ const arch_bits = switch (native_arch) {
4747 .mips64, .mips64el => @import("linux/mips64.zig"),
4848 .powerpc, .powerpcle => @import("linux/powerpc.zig"),
4949 .powerpc64, .powerpc64le => @import("linux/powerpc64.zig"),
50 .s390x => @import("linux/s390x.zig"),
5051 else => struct {
5152 pub const ucontext_t = void;
5253 pub const getcontext = {};
......@@ -276,6 +277,29 @@ pub const MAP = switch (native_arch) {
276277 UNINITIALIZED: bool = false,
277278 _: u5 = 0,
278279 },
280 .s390x => packed struct(u32) {
281 TYPE: MAP_TYPE,
282 FIXED: bool = false,
283 ANONYMOUS: bool = false,
284 _4: u1 = 0,
285 _5: u1 = 0,
286 GROWSDOWN: bool = false,
287 _7: u1 = 0,
288 _8: u1 = 0,
289 DENYWRITE: bool = false,
290 EXECUTABLE: bool = false,
291 LOCKED: bool = false,
292 NORESERVE: bool = false,
293 POPULATE: bool = false,
294 NONBLOCK: bool = false,
295 STACK: bool = false,
296 HUGETLB: bool = false,
297 SYNC: bool = false,
298 FIXED_NOREPLACE: bool = false,
299 _19: u5 = 0,
300 UNINITIALIZED: bool = false,
301 _: u5 = 0,
302 },
279303 else => @compileError("missing std.os.linux.MAP constants for this architecture"),
280304};
281305
......@@ -417,6 +441,32 @@ pub const O = switch (native_arch) {
417441 TMPFILE: bool = false,
418442 _: u9 = 0,
419443 },
444 .s390x => packed struct(u32) {
445 ACCMODE: ACCMODE = .RDONLY,
446 _2: u4 = 0,
447 CREAT: bool = false,
448 EXCL: bool = false,
449 NOCTTY: bool = false,
450 TRUNC: bool = false,
451 APPEND: bool = false,
452 NONBLOCK: bool = false,
453 DSYNC: bool = false,
454 ASYNC: bool = false,
455 DIRECT: bool = false,
456 LARGEFILE: bool = false,
457 DIRECTORY: bool = false,
458 NOFOLLOW: bool = false,
459 NOATIME: bool = false,
460 CLOEXEC: bool = false,
461 _17: u1 = 0,
462 PATH: bool = false,
463 _: u10 = 0,
464
465 // #define O_RSYNC 04010000
466 // #define O_SYNC 04010000
467 // #define O_TMPFILE 020200000
468 // #define O_NDELAY O_NONBLOCK
469 },
420470 else => @compileError("missing std.os.linux.O constants for this architecture"),
421471};
422472
lib/std/os/linux/s390x.zig created+276
......@@ -0,0 +1,276 @@
1const std = @import("../../std.zig");
2const iovec = std.posix.iovec;
3const iovec_const = std.posix.iovec_const;
4const linux = std.os.linux;
5const SYS = linux.SYS;
6const uid_t = std.os.linux.uid_t;
7const gid_t = std.os.linux.gid_t;
8const pid_t = std.os.linux.pid_t;
9const sockaddr = linux.sockaddr;
10const socklen_t = linux.socklen_t;
11const timespec = std.os.linux.timespec;
12const stack_t = std.os.linux.stack_t;
13const sigset_t = std.os.linux.sigset_t;
14
15pub fn syscall0(number: SYS) usize {
16 return asm volatile ("svc 0"
17 : [ret] "={r2}" (-> usize),
18 : [number] "{r1}" (@intFromEnum(number)),
19 : "memory"
20 );
21}
22
23pub fn syscall1(number: SYS, arg1: usize) usize {
24 return asm volatile ("svc 0"
25 : [ret] "={r2}" (-> usize),
26 : [number] "{r1}" (@intFromEnum(number)),
27 [arg1] "{r2}" (arg1),
28 : "memory"
29 );
30}
31
32pub fn syscall2(number: SYS, arg1: usize, arg2: usize) usize {
33 return asm volatile ("svc 0"
34 : [ret] "={r2}" (-> usize),
35 : [number] "{r1}" (@intFromEnum(number)),
36 [arg1] "{r2}" (arg1),
37 [arg2] "{r3}" (arg2),
38 : "memory"
39 );
40}
41
42pub fn syscall3(number: SYS, arg1: usize, arg2: usize, arg3: usize) usize {
43 return asm volatile ("svc 0"
44 : [ret] "={r2}" (-> usize),
45 : [number] "{r1}" (@intFromEnum(number)),
46 [arg1] "{r2}" (arg1),
47 [arg2] "{r3}" (arg2),
48 [arg3] "{r4}" (arg3),
49 : "memory"
50 );
51}
52
53pub fn syscall4(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
54 return asm volatile ("svc 0"
55 : [ret] "={r2}" (-> usize),
56 : [number] "{r1}" (@intFromEnum(number)),
57 [arg1] "{r2}" (arg1),
58 [arg2] "{r3}" (arg2),
59 [arg3] "{r4}" (arg3),
60 [arg4] "{r5}" (arg4),
61 : "memory"
62 );
63}
64
65pub fn syscall5(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
66 return asm volatile ("svc 0"
67 : [ret] "={r2}" (-> usize),
68 : [number] "{r1}" (@intFromEnum(number)),
69 [arg1] "{r2}" (arg1),
70 [arg2] "{r3}" (arg2),
71 [arg3] "{r4}" (arg3),
72 [arg4] "{r5}" (arg4),
73 [arg5] "{r6}" (arg5),
74 : "memory"
75 );
76}
77
78pub fn syscall6(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize, arg6: usize) usize {
79 return asm volatile ("svc 0"
80 : [ret] "={r2}" (-> usize),
81 : [number] "{r1}" (@intFromEnum(number)),
82 [arg1] "{r2}" (arg1),
83 [arg2] "{r3}" (arg2),
84 [arg3] "{r4}" (arg3),
85 [arg4] "{r5}" (arg4),
86 [arg5] "{r6}" (arg5),
87 [arg6] "{r7}" (arg6),
88 : "memory"
89 );
90}
91
92pub fn clone() callconv(.Naked) usize {
93 asm volatile (
94 \\# int clone(
95 \\# fn, a = r2
96 \\# stack, b = r3
97 \\# flags, c = r4
98 \\# arg, d = r5
99 \\# ptid, e = r6
100 \\# tls, f = *(r15+160)
101 \\# ctid) g = *(r15+168)
102 \\#
103 \\# pseudo C code:
104 \\# tid = syscall(SYS_clone,b,c,e,g,f);
105 \\# if (!tid) syscall(SYS_exit, a(d));
106 \\# return tid;
107 \\
108 \\# preserve call-saved register used as syscall arg
109 \\stg %%r6, 48(%%r15)
110 \\
111 \\# create initial stack frame for new thread
112 \\nill %%r3, 0xfff8
113 \\aghi %%r3, -160
114 \\lghi %%r0, 0
115 \\stg %%r0, 0(%%r3)
116 \\
117 \\# save fn and arg to child stack
118 \\stg %%r2, 8(%%r3)
119 \\stg %%r5, 16(%%r3)
120 \\
121 \\# shuffle args into correct registers and call SYS_clone
122 \\lgr %%r2, %%r3
123 \\lgr %%r3, %%r4
124 \\lgr %%r4, %%r6
125 \\lg %%r5, 168(%%r15)
126 \\lg %%r6, 160(%%r15)
127 \\svc 120
128 \\
129 \\# restore call-saved register
130 \\lg %%r6, 48(%%r15)
131 \\
132 \\# if error or if we're the parent, return
133 \\ltgr %%r2, %%r2
134 \\bnzr %%r14
135 \\
136 \\# we're the child. call fn(arg)
137 \\lg %%r1, 8(%%r15)
138 \\lg %%r2, 16(%%r15)
139 \\basr %%r14, %%r1
140 \\
141 \\# call SYS_exit. exit code is already in r2 from fn return value
142 \\svc 1
143 \\
144 );
145}
146
147pub const restore = restore_rt;
148
149pub fn restore_rt() callconv(.Naked) noreturn {
150 asm volatile (
151 \\svc 0
152 :
153 : [number] "{r1}" (@intFromEnum(SYS.rt_sigreturn)),
154 : "memory"
155 );
156}
157
158pub const F = struct {
159 pub const DUPFD = 0;
160 pub const GETFD = 1;
161 pub const SETFD = 2;
162 pub const GETFL = 3;
163 pub const SETFL = 4;
164 pub const GETLK = 5;
165 pub const SETLK = 6;
166 pub const SETLKW = 7;
167 pub const SETOWN = 8;
168 pub const GETOWN = 9;
169 pub const SETSIG = 10;
170 pub const GETSIG = 11;
171
172 pub const SETOWN_EX = 15;
173 pub const GETOWN_EX = 16;
174
175 pub const GETOWNER_UIDS = 17;
176};
177
178pub const blksize_t = i64;
179pub const nlink_t = u64;
180pub const time_t = i64;
181pub const mode_t = u32;
182pub const off_t = i64;
183pub const ino_t = u64;
184pub const dev_t = u64;
185pub const blkcnt_t = i64;
186
187pub const timeval = extern struct {
188 sec: time_t,
189 usec: i64,
190};
191
192pub const Flock = extern struct {
193 type: i16,
194 whence: i16,
195 start: off_t,
196 len: off_t,
197 pid: pid_t,
198};
199
200pub const msghdr = extern struct {
201 name: ?*sockaddr,
202 namelen: socklen_t,
203 iov: [*]iovec,
204 __pad1: i32 = 0,
205 iovlen: i32,
206 control: ?*anyopaque,
207 __pad2: i32 = 0,
208 controllen: socklen_t,
209 flags: i32,
210};
211
212pub const msghdr_const = extern struct {
213 name: ?*const sockaddr,
214 namelen: socklen_t,
215 iov: [*]const iovec_const,
216 __pad1: i32 = 0,
217 iovlen: i32,
218 control: ?*const anyopaque,
219 __pad2: i32 = 0,
220 controllen: socklen_t,
221 flags: i32,
222};
223
224// The `stat` definition used by the Linux kernel.
225pub const Stat = extern struct {
226 dev: dev_t,
227 ino: ino_t,
228 nlink: nlink_t,
229 mode: mode_t,
230 uid: uid_t,
231 gid: gid_t,
232 rdev: dev_t,
233 size: off_t,
234 atim: timespec,
235 mtim: timespec,
236 ctim: timespec,
237 blksize: blksize_t,
238 blocks: blkcnt_t,
239 __unused: [3]c_ulong,
240
241 pub fn atime(self: @This()) timespec {
242 return self.atim;
243 }
244
245 pub fn mtime(self: @This()) timespec {
246 return self.mtim;
247 }
248
249 pub fn ctime(self: @This()) timespec {
250 return self.ctim;
251 }
252};
253
254pub const Elf_Symndx = u64;
255
256pub const VDSO = struct {
257 pub const CGT_SYM = "__kernel_clock_gettime";
258 pub const CGT_VER = "LINUX_2.6.29";
259};
260
261pub const ucontext_t = extern struct {
262 flags: u64,
263 link: ?*ucontext_t,
264 stack: stack_t,
265 mcontext: mcontext_t,
266 sigmask: sigset_t,
267};
268
269pub const mcontext_t = extern struct {
270 __regs1: [18]u64,
271 __regs2: [18]u32,
272 __regs3: [16]f64,
273};
274
275/// TODO
276pub const getcontext = {};
test/behavior/basic.zig+1
......@@ -1425,6 +1425,7 @@ test "allocation and looping over 3-byte integer" {
14251425 if (builtin.zig_backend == .stage2_llvm and builtin.os.tag == .macos) {
14261426 return error.SkipZigTest; // TODO
14271427 }
1428 if (builtin.cpu.arch == .s390x and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
14281429
14291430 try expect(@sizeOf(u24) == 4);
14301431 try expect(@sizeOf([1]u24) == 4);
test/behavior/union.zig+1
......@@ -1893,6 +1893,7 @@ test "reinterpret packed union" {
18931893 if (builtin.cpu.arch.isPowerPC()) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21050
18941894 if (builtin.cpu.arch.isMIPS()) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21050
18951895 if (builtin.cpu.arch.isWasm()) return error.SkipZigTest; // TODO
1896 if (builtin.cpu.arch == .s390x and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
18961897 try S.doTheTest();
18971898}
18981899
test/behavior/var_args.zig+5
......@@ -105,6 +105,7 @@ test "simple variadic function" {
105105 return error.SkipZigTest;
106106 }
107107 if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .windows) return error.SkipZigTest; // TODO
108 if (builtin.cpu.arch == .s390x and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21350
108109
109110 const S = struct {
110111 fn simple(...) callconv(.C) c_int {
......@@ -165,6 +166,7 @@ test "coerce reference to var arg" {
165166 return error.SkipZigTest;
166167 }
167168 if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .windows) return error.SkipZigTest; // TODO
169 if (builtin.cpu.arch == .s390x and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21350
168170
169171 const S = struct {
170172 fn addPtr(count: c_int, ...) callconv(.C) c_int {
......@@ -197,6 +199,7 @@ test "variadic functions" {
197199 return error.SkipZigTest;
198200 }
199201 if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .windows) return error.SkipZigTest; // TODO
202 if (builtin.cpu.arch == .s390x and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21350
200203
201204 const S = struct {
202205 fn printf(list_ptr: *std.ArrayList(u8), format: [*:0]const u8, ...) callconv(.C) void {
......@@ -241,6 +244,7 @@ test "copy VaList" {
241244 return error.SkipZigTest;
242245 }
243246 if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .windows) return error.SkipZigTest; // TODO
247 if (builtin.cpu.arch == .s390x and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21350
244248
245249 const S = struct {
246250 fn add(count: c_int, ...) callconv(.C) c_int {
......@@ -277,6 +281,7 @@ test "unused VaList arg" {
277281 // https://github.com/ziglang/zig/issues/16961
278282 return error.SkipZigTest; // TODO
279283 }
284 if (builtin.cpu.arch == .s390x and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/21350
280285
281286 const S = struct {
282287 fn thirdArg(dummy: c_int, ...) callconv(.C) c_int {