128bit atomic operation in arm64

In x86_64 architecture, we could use cmpxchg16b to compare and exchange a 128bit value in one atomic operation. But how to operate a 128bit value in aarch64 architecture (arm64) machine ? The answer is __atomic_compare_exchange().

As below code:

use “gcc test.c -o test” to compile code, but it reports:

The reason is we have not use “libatomic.so”, so use “gcc test.c -o test -latomic” to compile and link the code agin. It works in Fedora 21 (aarch64).

Running Fedora 21 on qemu-system-aarch64

Foundation Model V8 is a emulator for ARM64 architecture, but it has many limits, such as slow CPU, small memory (about 2GB), etc. Therefore I want to run fedora on qemu emulator for aarch64. There are already many docs to introduce how to run Foundation Model V8 on Fedora 21 on the web, but none of them could point out the way to run qemu-aarch64 on Fedora 21. Spent about a week, I finally find out the path.

Step 1 Build the edk2 UEFI
Followed this article,you should download the linaro toolchain and edit the “Conf/target.txt”. Then follow these steps to build a file in “Build/ArmVirtualizationQemu-AARCH64/DEBUG_GCC48/FV/QEMU_EFI.fd”

Step 2 Build the qemu-aarch64
I recommend the latest stable QEMU-2.2.0

Now, you have file in “qemu-2.2.0/aarch64-softmmu/qemu-system-aarch64”

Step 3 Download the Fedora 21 image for Foundation Model V8

Now you have “F21-20140829-foundation-v8/F21-20140829-foundation-v8.img”

Step 4 Run the qemu

The Qemu will boot into the UEFI, you could select into the “shell”, in the UEFI shell:

Then you can enter into a grub provided by Fedora 21 and boot into it automaticly.

Step 5 Add the UEFI entry for fedora permanently (optional)

Launch qemu-system-aarch64 by change “-bios xxx” to “-pflash flash0.img -pflash flash1.img” and after enter the UEFI shell:

The UEFI boot entry will write into flash*.img file and the Qemu could boot into Fedora 21 automaticly now.

Run aarch64 binary on x86_64 machines

If we use qemu-arm64-system directly, it will cost too much time on IO and systemcalls.So I try to use SuSE’s userpsace mode qemu, which only reinterprets the arm64 instructions to x86_64 but processes all systemcalls to local host.
This installation manual for user-mode qemu-arm64 has tested on debian-7.7.0

Step 1, make directory for chroot.

Step 2, build qemu of SuSE:

Add this patch to SuSE’s qemu before make it:

Make the qemu and install it:

Step 3, download arm64 disk image of Fedora 19, and extract it.

Copy the qemu-arm64

Step 4, configure the binfmts-misc

and put the content below into /usr/share/binfmts/qemu-arm64 in normal system(not the chroot)

Then run

Step 5, you could chroot into the user mode qemu now:

Now, If you typein “arch”, it will report “aarch64”
Addtionally, add below contents into “aarch64” environtment’s /etc/yum.repos.d/stage4.repo:

You could use yum to install rpm of aarch64 now. If it report DNS unavailable, you could append

into /etc/hosts

I use this user-mode of arm64 Qemu to build linux kernel, but the speed of compiling is not very perfrect. After all, the user-mode Qemu only accelerate the speed of systemcall, not the reinterpreter of instructions. But in my opinion, it is still very useful because it could use host’s resource of network and storage efficiently (Don’t you think it looks like linux-container or lxc/docker ?). If this news is true, I think facebook could use cheap x86_64 machines to test their IOS apps instead of costly Mac minis 🙂

Reflinks:
How to run aarch64 binaries on an x86-64 host using qemu userspace emulation
Arm64Qemu