Motorola just released an x86 based Android phone. I'm a little confused as to how native apps/libraries written for ARM(netflix for example) can run on this phone.
I'd be grateful if someone could explain.
Motorola just released an x86 based Android phone. I'm a little confused as to how native apps/libraries written for ARM(netflix for example) can run on this phone.
I'd be grateful if someone could explain.
You can actually include different native code for different architecture, not sure how Netflix is running but if you open apk you can see
/lib/armeabi-v7a/
, so I assume there can be a folder something like/lib/x86/
Edit: I just checked Amazon shopping app it has native code for arm and x86. So maybe Thats how netflix does it too.
In Trend Micro Safe Mobile Workforce, we have an ARM runtime (not Intel's houdini) for native library in Android apps. So that we can support running APK with only ARM lib on powerful x86 server.
Yes, ARM native code runs on Intel x86 using an emulation feature named Houdini
What this library does is reads ARM instructions on the fly and converts them to equivalent x86 instructions. This is the reason why many apps may work as is on x86 without actually having to build an equivalent library.
The Android Studio 3 emulator uses QEMU as a backend
https://en.wikipedia.org/wiki/QEMU
QEMU is arguably the leading open source cross arch emulator. It is GPL software, and supports many, many more archs in addition to x86 and ARM.
Android then just adds some bit of UI magic on top of QEMU and possibly some patches, but the core is definitely in QEMU upstream.
QEMU uses a technique called binary translation to achieve reasonably fast emulation: https://en.wikipedia.org/wiki/Binary_translation
Binary translation basically translates ARM instructions into equivalent x86 instructions.
Therefore, to understand the details, the best way is to:
Theory
Therefore, it is clear that any CPU can emulate any CPU given enough memory.
The hard question is how to do that fast.
Practice: QEMU user mode simulation
QEMU has an userland mode that makes it very easy to play with userland ARM code on your x86 machine to see what is happening, as long as your guest and host are the same OS.
In this mode, what happens is that binary translation takes care of the basic instructions, and system calls are just forwarded to the host system calls.
E.g., for Linux on Linux with a Linux freestanding (no glibc) hello world:
main.S
GitHub upstream.
Then assemble and run as:
and it outputs the expected:
You can even run ARM programs compiled against the C standard library, and GDB step debug the program! See this concrete example: How to single step ARM assembly in GDB on QEMU?
Since we are talking about binary translation, we can also enable some logging to see the exact translation that QEMU is doing:
Here:
in_asm
refers to the ARM guest input assemblyout_asm
refers to X86 host generated assembly that gets runThe output contains:
so in the
IN
section, we see our hand written ARM assembly code, and in theOUT
section we see the generated x86 assembly.Tested in Ubuntu 16.04 amd64, QEMU 2.5.0, binutils 2.26.1.
QEMU full system emulation
When you boot Android in QEMU however, it is not running an userland binary of course, but rather doing full system simulation, where it runs the actual Linux kernel and all devices in the simulation.
Full system simulation is more accurate, but a bit slower, and you need to give a kernel and disk image to QEMU.
To try that out, have a look at the following setups:
KVM
If you run Android X86 on QEMU, you will notice that it is much faster.
The reason is that QEMU uses KVM, which is a Linux kernel feature that can run the guest instructions directly on the host!
If you happen to have a powerful ARM machine (yet rare as of 2019), you can also run ARM on ARM with KVM much faster.
For this reason, I recommend that you stick to X86 simulation of AOSP if you are on an X86 host as mentioned at: How to compile the Android AOSP kernel and test it with the Android Emulator?, unless you really need to touch something low level.