shepmaster / cupid

Get information about the x86 and x86_64 processor
MIT License
34 stars 9 forks source link

Detecting cpuid support #11

Open gnzlbg opened 6 years ago

gnzlbg commented 6 years ago

It turns out that not all x86 cpus have the cpuid instruction, so one should probably check whether the CPU supports it before using it by checking whether the ID bit of the EFLAGS register can be written.

The stdsimd crate has a PR with the __readeflags and __writeeflags intrinsics that can be used to check this, and will hopefully also expose a has_cpuid() -> bool that does the check for you.

FWIW the PR also exposes the __cpuid and __cpuid_count intrinsics. That might allow you to remove inline assembly, at least on nightly rust (and hopefully on stable someday).

shepmaster commented 6 years ago

It turns out that not all x86 cpus have the cpuid instruction

While technically true...

[CPUID] was introduced by Intel in 1993 when it introduced the Pentium and SL-enhanced 486 processors. - Wikipedia

I was under the impression that it was basically impossible to target processors of this age with Rust / LLVM. Is it truly possible to compile for them?

gnzlbg commented 6 years ago

LLVM can target i386 and you can run the binaries on QEMU, but this does not mean that you don't have a point. The stdsimd crate uses conditional compilation to always return true on x86_64 which is what most users have nowadays.