authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-11-20 06:50:05+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-11-21 12:15:15+01:00
logd3e20e71be8d94b8c0534d2cb57a1a27c451db9f
tree0b2f76e5bf67a3f6966c9be98f88840e13eb2639
parent1cd913a0ec816f44d157980ade00a6560832a348
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.zig.system.linux: implement s390x native CPU detection


1 files changed, 74 insertions(+), 0 deletions(-)

lib/std/zig/system/linux.zig+74
......@@ -188,6 +188,77 @@ test "cpuinfo: PowerPC" {
188188 );
189189}
190190
191const S390xCpuinfoImpl = struct {
192 model: ?*const Target.Cpu.Model = null,
193
194 const cpu_names = .{
195 // z900: 2064, 2066
196 // z990: 2084, 2086
197 // z9: 2094, 2096
198
199 .{ "2097", &Target.s390x.cpu.z10 },
200 .{ "2098", &Target.s390x.cpu.z10 },
201 .{ "2817", &Target.s390x.cpu.z196 },
202 .{ "2818", &Target.s390x.cpu.z196 },
203 .{ "2827", &Target.s390x.cpu.zEC12 },
204 .{ "2828", &Target.s390x.cpu.zEC12 },
205 .{ "2964", &Target.s390x.cpu.z13 },
206 .{ "2965", &Target.s390x.cpu.z13 },
207 .{ "3906", &Target.s390x.cpu.z14 },
208 .{ "3907", &Target.s390x.cpu.z14 },
209 .{ "8561", &Target.s390x.cpu.z15 },
210 .{ "8562", &Target.s390x.cpu.z15 },
211 .{ "3931", &Target.s390x.cpu.z16 },
212 .{ "3932", &Target.s390x.cpu.z16 },
213 .{ "9175", &Target.s390x.cpu.z17 },
214 .{ "9176", &Target.s390x.cpu.z17 },
215 };
216
217 fn line_hook(self: *S390xCpuinfoImpl, key: []const u8, value: []const u8) !bool {
218 if (mem.eql(u8, key, "machine")) {
219 inline for (cpu_names) |pair| {
220 if (mem.eql(u8, value, pair[0])) {
221 self.model = pair[1];
222 break;
223 }
224 }
225
226 return false;
227 }
228
229 return true;
230 }
231
232 fn finalize(self: *const S390xCpuinfoImpl, arch: Target.Cpu.Arch) ?Target.Cpu {
233 const model = self.model orelse return null;
234 return Target.Cpu{
235 .arch = arch,
236 .model = model,
237 .features = model.features,
238 };
239 }
240};
241
242const S390xCpuinfoParser = CpuinfoParser(S390xCpuinfoImpl);
243
244test "cpuinfo: S390x" {
245 try testParser(S390xCpuinfoParser, .s390x, &Target.s390x.cpu.z15,
246 \\physical id : 5
247 \\core id : 5
248 \\book id : 5
249 \\drawer id : 5
250 \\dedicated : 0
251 \\address : 5
252 \\siblings : 1
253 \\cpu cores : 1
254 \\version : FF
255 \\identification : 09DD98
256 \\machine : 8561
257 \\cpu MHz dynamic : 5200
258 \\cpu MHz static : 5200
259 );
260}
261
191262const ArmCpuinfoImpl = struct {
192263 const num_cores = 4;
193264
......@@ -414,6 +485,9 @@ pub fn detectNativeCpuAndFeatures(io: Io) ?Target.Cpu {
414485 .riscv64, .riscv32 => {
415486 return RiscvCpuinfoParser.parse(current_arch, &file_reader.interface) catch null;
416487 },
488 .s390x => {
489 return S390xCpuinfoParser.parse(current_arch, &file_reader.interface) catch null;
490 },
417491 else => {},
418492 }
419493