How to get CPU feature flags in C userspace code without going through /proc/cpuinfo?

307 views
Skip to first unread message

Simon Que

unread,
Apr 12, 2018, 12:08:09 PM4/12/18
to chromium-os-dev
/proc/cpuinfo exposes CPU feature flags like SSE2.

Is there a way to get this CPU feature info in a C program in userspace
using some kind of syscall?

If not, is the format of the existing text content of /proc/cpuinfo
expected to be stable? i.e. existing fields will always have the same
format. If that's the case, and there is no other way to easily get this
info in C code, then I'm fine with just parsing /proc/cpuinfo.

Simon

Lei Zhang

unread,
Apr 12, 2018, 12:37:03 PM4/12/18
to Simon Que, chromium-os-dev
Take a look at Chromium's base::CPU. If you only care about x86 and
you are rolling your own code, you can try calling cpuid directly and
parsing the output.
> --
> --
> Chromium OS Developers mailing list: chromiu...@chromium.org
> View archives, change email options, or unsubscribe:
> http://groups.google.com/a/chromium.org/group/chromium-os-dev?hl=en
>
>

Daniel Kurtz

unread,
Apr 12, 2018, 12:48:41 PM4/12/18
to Lei Zhang, Simon Que, Chromium OS dev
Simon,

I happened to write this exact C program 2 days ago (based on Chromium's base::CPU)... 
HTH!

-djk
cpuinfo.c

Simon Que

unread,
Apr 12, 2018, 1:24:47 PM4/12/18
to djk...@chromium.org, Lei Zhang, chromium-os-dev
Oh I didn't realize there's some Chromium code written in assembly. Thanks djkurtz, this is what I'm looking for.

Mike Frysinger

unread,
Apr 22, 2018, 12:39:32 PM4/22/18
to Daniel Kurtz, Lei Zhang, Simon Que, chromium-os-dev
gcc provides a cpuid.h header so there's no need to write your own inline asm anymore.
-mike

Do Thanh

unread,
May 1, 2018, 4:12:35 PM5/1/18
to Chromium OS dev

Noel Gordon

unread,
May 15, 2018, 10:19:46 AM5/15/18
to Chromium OS Development, djk...@chromium.org, the...@chromium.org, sq...@chromium.org
Linux exposes /proc/cpuinfo CPU feature flags SSE and NEON to user-space: see <asm/hwcap.h> and the getauxval() [1] call for ELF binaries.

We use this in chrome's third_party/zlib on ARM for some chrome ports (not chrome-os as yet, sadly), to detect NEON CPU features from user-space ...



~noel

Mike Frysinger

unread,
May 15, 2018, 11:16:28 AM5/15/18
to no...@chromium.org, chromium-os-dev, Daniel Kurtz, Lei Zhang, Simon Que
the AT_HWCAP/AT_HWCAP2 entries are useful on other platforms (like arm and ppc and s390), but they're not nearly as useful on x86: AT_HWCAP is simply EDX from cpuid(eax=1), but ECX isn't available (which is where all the newer stuff lives like sse3/fma/avx/etc...).  maybe ECX is all Simon needs, otherwise cpuid is your only choice.
-mike

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages