When building zig natively from source on an RK3588 SoC board, thegenerated stage3 compiler will use unsupported 'sha256h.4s' instructionsdue to mis-identified CPU features. This happens when trying tocompile a new project generated with "zig init"
$ ~/devel/zig/build/stage3/bin/zig buildthread 919 panic: Illegal instruction at address 0x1fdc0c4/home/dliviu/devel/zig/lib/std/crypto/sha2.zig:223:29: 0x1fdc0c4 in round (zig) asm volatile ( ^/home/dliviu/devel/zig/lib/std/crypto/sha2.zig:168:20: 0x1fdca87 in final (zig) d.round(&d.buf); ^/home/dliviu/devel/zig/lib/std/crypto/sha2.zig:180:20: 0x1c8bb33 in finalResult (zig) d.final(&result); ^/mnt/home/dliviu/devel/zig/src/Package/Fetch.zig:754:49: 0x1a3a8eb in relativePathDigest (zig) return Manifest.hexDigest(hasher.finalResult()); ^/mnt/home/dliviu/devel/zig/src/main.zig:5128:53: 0x1a37413 in cmdBuild (zig) Package.Fetch.relativePathDigest(build_mod.root, global_cache_directory), ^???:?:?: 0xff09ffffffffffff in ??? (???)Unwind information for ???:0xff09ffffffffffff was not available, trace may be incomplete
???:?:?: 0x3f7f137 in ??? (???)Aborted (core dumped)
system/linux.zig parses "/proc/cpuinfo" to determine the CPU features, but itseems to generate the wrong set of features from:
$ cat /proc/cpuinfoprocessor : 0BogoMIPS : 48.00Features : fp asimd evtstrm crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddpCPU implementer : 0x41CPU architecture: 8CPU variant : 0x2CPU part : 0xd05CPU revision : 0.....processor : 4BogoMIPS : 48.00Features : fp asimd evtstrm crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddpCPU implementer : 0x41CPU architecture: 8CPU variant : 0x4CPU part : 0xd0bCPU revision : 0.....
To fix this, use the Linux kernel way of reading the feature registers as documentedhere: https://www.kernel.org/doc/html/latest/arch/arm64/cpu-feature-registers.html
arm.zig already has the code to parse the feature register values, we just need tocollect them in an array and pass them for identification.
Signed-off-by: Liviu Dudau <liviu@dudau.co.uk>