ARM CP15 c0 (CPU ID) via inline assembly?

3,616 views
Skip to first unread message

Bryan Ashby

unread,
Apr 5, 2010, 6:08:54 PM4/5/10
to android-ndk
Hello,

I'm attempting to access the ARM CPU ID register via inline assembly
in my C code (ARM mode). I can compile, but the call crashes. Is this
not supported by the NDK?

Example (pseudo code):

unsigned int getArmCpuId()
{
unsigned int id = 0;
asm volatile("mrc p15 0, %0, c0, c0, 0" : "=r" (id)); // <--- this
crashes
return id;
}

I'd like to avoid reading /proc/cpuinfo if at all possible.

Thanks for any tips!

David Turner

unread,
Apr 5, 2010, 6:16:34 PM4/5/10
to andro...@googlegroups.com
What is the crash exactly ? on which device ?


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Bryan Ashby

unread,
Apr 5, 2010, 6:22:13 PM4/5/10
to android-ndk
On Apr 5, 4:16 pm, David Turner <di...@android.com> wrote:
> What is the crash exactly ? on which device ?
>

Currently I'm working in the emulator -- The physical device I have on
hand right now is a G1, but I've yet to try it.

As for the crash, from logcat:
I/DEBUG ( 28): signal 4 (SIGILL), fault addr 821821e0

David Turner

unread,
Apr 5, 2010, 6:40:51 PM4/5/10
to andro...@googlegroups.com
Ah, I wonder if this is an emulation bug, or comes from the fact that CPUID is not supported before ARMv6. Will look into it.


--

Dave Butcher

unread,
Apr 6, 2010, 10:44:16 AM4/6/10
to android-ndk

Which register do you want to get at? The Main ID register with the
basic Architecture, variant, revision information
the CPU ID registers with information on the specific cpu features or
the Multiprocessor Affinity Register (which has
been referred to as the CPU ID register)?

Although, none of these are accessible in user-mode - and therefore
not accessible from the NDK. You need to be
running in privileged mode to be able to access them:

Main ID - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0301h/Bgbiddeb.html
CPU ID - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0301h/Babgbeed.html
MPIDR - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0388e/CIHEBGFG.html

On the emulator I suspect it is being (correctly) treated as an
UNDEFINED instruction (the emulator is running as an ARM926 I
believe), under user-mode on an ARM1176 or an ARMv7-a processor it
will still be treated as an UNDEFINED
while in user mode. The SIGILL is what I would expect in either case.

In theory, the OS is supposed to be the source of information about
what features are available - this is because
the OS is also responsible for setting up and supporting some of
them. If you can describe what it is you want to
do I (and by 'I' I mean people I know who I can ask) might be able to
suggest something.

--

On 5 Apr, 23:40, David Turner <di...@android.com> wrote:
> Ah, I wonder if this is an emulation bug, or comes from the fact that CPUID
> is not supported before ARMv6. Will look into it.
>
>
>
> On Mon, Apr 5, 2010 at 3:22 PM, Bryan Ashby <nuskoo...@gmail.com> wrote:
> > On Apr 5, 4:16 pm, David Turner <di...@android.com> wrote:
> > > What is the crash exactly ? on which device ?
>
> > Currently I'm working in the emulator -- The physical device I have on
> > hand right now is a G1, but I've yet to try it.
>
> > As for the crash, from logcat:
> > I/DEBUG   (   28): signal 4 (SIGILL), fault addr 821821e0
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>

Bryan Ashby

unread,
Apr 6, 2010, 11:25:52 AM4/6/10
to android-ndk

On Apr 6, 8:44 am, Dave Butcher <djb....@googlemail.com> wrote:
> Which register do you want to get at?

I'm after the main ID register. It appears I somehow missed the part
about privileged only -- which explains my issues.

> In theory, the OS is supposed to be the source of information about
> what features are available - this is because
> the OS is also responsible for setting up and supporting some of
> them.  If you can describe what it is you want to
> do I (and by 'I' I mean people I know who I can ask) might be able to
> suggest something.

Our seat management uses CPU information as part of it's identifier --
on x86 for example this is from the CPUID instruction; I was hoping to
carry some of that over. /proc/cpuinfo has a lot of the information,
which should do.

fadden

unread,
Apr 7, 2010, 2:12:41 AM4/7/10
to android-ndk
On Apr 6, 8:25 am, Bryan Ashby <nuskoo...@gmail.com> wrote:
> Our seat management uses CPU information as part of it's identifier --
> on x86 for example this is from the CPUID instruction; I was hoping to
> carry some of that over. /proc/cpuinfo has a lot of the information,
> which should do.

In some ways /proc/cpuinfo is better, at least for feature detection.
For example, if the hardware supports VFP but the feature isn't
enabled in the kernel, you won't actually have floating point support
available.

Is there something missing from /proc/cpuinfo that you would find
useful?

Bryan Ashby

unread,
Apr 7, 2010, 12:15:58 PM4/7/10
to android-ndk
On Apr 7, 12:12 am, fadden <fad...@android.com> wrote:
> Is there something missing from /proc/cpuinfo that you would find
> useful?

Everything the cp15 c0 gives back appears to be there, so it looks
fine. My only gripe is that /proc/cpuinfo itself is quite non-standard
in it's formatting and contents. This is a Linux gripe though, not a
Android one :)

fadden

unread,
Apr 9, 2010, 5:58:01 PM4/9/10
to android-ndk
On Apr 7, 9:15 am, Bryan Ashby <nuskoo...@gmail.com> wrote:
> Everything the cp15 c0 gives back appears to be there, so it looks
> fine. My only gripe is that /proc/cpuinfo itself is quite non-standard
> in it's formatting and contents. This is a Linux gripe though, not a
> Android one :)

The NDK is now including an API function that parses /proc/cpuinfo and
turns it into feature flags:


http://android.git.kernel.org/?p=platform/ndk.git;a=blob;f=sources/cpufeatures/cpu-features.c;h=c46b884f8eeb9b1b37dbf27c20dde1be5372906c;hb=HEAD

(If the URL gets broken up, it's in ndk/sources/cpufeatures/cpu-
features.c)

This should help smooth over Linux-version-dependent differences. I
don't know if what you want is there though -- it's currently just
interested in CPU family (ARM vs x86) and yes/no on "is ARMv7", "has
VFPv3", "has NEON".

David Turner

unread,
Apr 9, 2010, 6:06:41 PM4/9/10
to andro...@googlegroups.com
Yes, and for the record, this will go into r4, but is not in r3 yet.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

David Turner

unread,
Apr 9, 2010, 6:07:20 PM4/9/10
to andro...@googlegroups.com
Another clarification: this is a static library that does not depend on specific system ones, so feel free to use the code from the open-source tree in your applications if you want to, it should work even if you target Android 1.5.

Bryan Ashby

unread,
Apr 14, 2010, 9:14:27 PM4/14/10
to android-ndk
This is great news! I had already written a small class for doing such
things, but I'd much rather rely on an official one. Thanks for the
info!

Oyin Oluwatimi

unread,
Jul 30, 2012, 2:27:43 AM7/30/12
to andro...@googlegroups.com, di...@android.com
Hi 

I am trying read the system control register to print out the bits, but it does not print out.  I tried using this command for the SCR, but to no avail. 
asm("MRC p15, 0, %0, c1, c1, 2 " : "=r" (id));

I think im sending it to register number "r0" (%0).

 Is it not possible?

Though, I was able to succesfully print out the cpsr using:
asm("mrs %[ps], cpsr" : [ps]"=r" (id));

Damola
To unsubscribe from this group, send email to android-ndk+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages