Commit d3e20e71be

Alex Rønne Petersen <alex@alexrp.com>
2025-11-20 06:50:05
std.zig.system.linux: implement s390x native CPU detection
1 parent 1cd913a
Changed files (1)
lib
std
zig
system
lib/std/zig/system/linux.zig
@@ -188,6 +188,77 @@ test "cpuinfo: PowerPC" {
     );
 }
 
+const S390xCpuinfoImpl = struct {
+    model: ?*const Target.Cpu.Model = null,
+
+    const cpu_names = .{
+        // z900: 2064, 2066
+        // z990: 2084, 2086
+        // z9: 2094, 2096
+
+        .{ "2097", &Target.s390x.cpu.z10 },
+        .{ "2098", &Target.s390x.cpu.z10 },
+        .{ "2817", &Target.s390x.cpu.z196 },
+        .{ "2818", &Target.s390x.cpu.z196 },
+        .{ "2827", &Target.s390x.cpu.zEC12 },
+        .{ "2828", &Target.s390x.cpu.zEC12 },
+        .{ "2964", &Target.s390x.cpu.z13 },
+        .{ "2965", &Target.s390x.cpu.z13 },
+        .{ "3906", &Target.s390x.cpu.z14 },
+        .{ "3907", &Target.s390x.cpu.z14 },
+        .{ "8561", &Target.s390x.cpu.z15 },
+        .{ "8562", &Target.s390x.cpu.z15 },
+        .{ "3931", &Target.s390x.cpu.z16 },
+        .{ "3932", &Target.s390x.cpu.z16 },
+        .{ "9175", &Target.s390x.cpu.z17 },
+        .{ "9176", &Target.s390x.cpu.z17 },
+    };
+
+    fn line_hook(self: *S390xCpuinfoImpl, key: []const u8, value: []const u8) !bool {
+        if (mem.eql(u8, key, "machine")) {
+            inline for (cpu_names) |pair| {
+                if (mem.eql(u8, value, pair[0])) {
+                    self.model = pair[1];
+                    break;
+                }
+            }
+
+            return false;
+        }
+
+        return true;
+    }
+
+    fn finalize(self: *const S390xCpuinfoImpl, arch: Target.Cpu.Arch) ?Target.Cpu {
+        const model = self.model orelse return null;
+        return Target.Cpu{
+            .arch = arch,
+            .model = model,
+            .features = model.features,
+        };
+    }
+};
+
+const S390xCpuinfoParser = CpuinfoParser(S390xCpuinfoImpl);
+
+test "cpuinfo: S390x" {
+    try testParser(S390xCpuinfoParser, .s390x, &Target.s390x.cpu.z15,
+        \\physical id     : 5
+        \\core id         : 5
+        \\book id         : 5
+        \\drawer id       : 5
+        \\dedicated       : 0
+        \\address         : 5
+        \\siblings        : 1
+        \\cpu cores       : 1
+        \\version         : FF
+        \\identification  : 09DD98
+        \\machine         : 8561
+        \\cpu MHz dynamic : 5200
+        \\cpu MHz static  : 5200
+    );
+}
+
 const ArmCpuinfoImpl = struct {
     const num_cores = 4;
 
@@ -414,6 +485,9 @@ pub fn detectNativeCpuAndFeatures(io: Io) ?Target.Cpu {
         .riscv64, .riscv32 => {
             return RiscvCpuinfoParser.parse(current_arch, &file_reader.interface) catch null;
         },
+        .s390x => {
+            return S390xCpuinfoParser.parse(current_arch, &file_reader.interface) catch null;
+        },
         else => {},
     }