authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-09-07 10:43:18+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-09-07 11:45:07+02:00
logc4fec122cd829c3502e47e22b08ed220aaabda0a
treeb6dcb110aa629aa8edc3bb120c77f0cd823599fa
parentb529ea661e34e35f45a7b6032e87937cd56ee53a
signature Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.zig.system.linux: add csky CPU model detection


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

lib/std/zig/system/linux.zig+61
......@@ -232,6 +232,66 @@ const arm = struct {
232232 }
233233};
234234
235const csky = struct {
236 const cpuinfo = struct {
237 const Impl = struct {
238 model: ?*const Target.Cpu.Model = null,
239
240 const cpu_names = .{
241 .{ "ck807", &Target.csky.cpu.ck807 },
242 .{ "ck807f", &Target.csky.cpu.ck807f },
243 .{ "ck810", &Target.csky.cpu.ck810 },
244 .{ "ck810f", &Target.csky.cpu.ck810f },
245 .{ "ck810t", &Target.csky.cpu.ck810t },
246 .{ "ck810ft", &Target.csky.cpu.ck810ft },
247 .{ "ck860", &Target.csky.cpu.ck860 },
248 .{ "ck860f", &Target.csky.cpu.ck860f },
249 .{ "ck860fv", &Target.csky.cpu.ck860fv },
250 };
251
252 fn lineHook(self: *Impl, key: []const u8, value: []const u8) !bool {
253 if (mem.eql(u8, key, "C-SKY CPU model")) {
254 inline for (cpu_names) |pair| {
255 if (mem.eql(u8, value, pair[0])) {
256 self.model = pair[1];
257 break;
258 }
259 }
260
261 return false;
262 }
263
264 return true;
265 }
266
267 fn finalize(self: *const Impl, arch: Target.Cpu.Arch) ?Target.Cpu {
268 const model = self.model orelse return null;
269 return .{
270 .arch = arch,
271 .model = model,
272 .features = model.features,
273 };
274 }
275 };
276
277 const Parser = CpuinfoParser(Impl);
278 };
279
280 pub fn detectNativeCpu(io: Io, arch: Target.Cpu.Arch) ?Target.Cpu {
281 var file = Io.Dir.openFileAbsolute(io, "/proc/cpuinfo", .{}) catch return null;
282 defer file.close(io);
283
284 var buffer: [4096]u8 = undefined;
285 var file_reader = file.reader(io, &buffer);
286
287 var cpu = (cpuinfo.Parser.parse(arch, &file_reader.interface) catch null) orelse return null;
288
289 cpu.features.populateDependencies(cpu.arch.allFeaturesList());
290
291 return cpu;
292 }
293};
294
235295const powerpc = struct {
236296 const cpuinfo = struct {
237297 const Impl = struct {
......@@ -757,6 +817,7 @@ pub fn detectNativeCpuAndFeatures(io: Io) ?Target.Cpu {
757817 return switch (current_arch) {
758818 .aarch64, .aarch64_be => aarch64.detectNativeCpuAndFeatures(current_arch),
759819 .arm, .armeb, .thumb, .thumbeb => arm.detectNativeCpu(io, current_arch),
820 .csky => csky.detectNativeCpu(io, current_arch),
760821 .powerpc, .powerpcle, .powerpc64, .powerpc64le => powerpc.detectNativeCpu(io, current_arch),
761822 .riscv64, .riscv32 => riscv.detectNativeCpuAndFeatures(io, current_arch),
762823 .s390x => s390x.detectNativeCpu(io, current_arch),