User:Vanjo9800/Raspberry Pi 3 - 64-bit Gentoo

From Gentoo Wiki
Jump to:navigation Jump to:search

Do you really need the 64-bit Gentoo?

The first question you should ask yourself before trying anything is whether you need that 64-bit OS on an ARM processor with just 1GB of RAM. As a whole, the 64-bit operating system is a good thing because it can work better with long numbers. The reason for that are the 64-bit pointers. Some problems use such numbers and the 64-bit Gentoo will speed up their work. However, there is a big drawback. These 64-bit pointers require RAM memory. While normal PC's have around 2 or even more than 4, the Raspberry Pi 3 has only 1 GB, so sometimes the RAM is filled with the pointers and the Pi goes out of RAM. In order, to avoid the RAM to be filled, the processor should do less tasks simultaneously which lowers performance. Up to now I cannot give you a proper benchmark, but I would try to give you ASAP. I put 64-bit experimenting which one would prove better. If you are like me, you can freely continue the rest. If you are going to use that somewhere important, think about whether do you really need this.

SD card setup

For a start, the partitions of the SD card should be configured properly. The SD card must have two partitions: one boot partition and one root partition. The boot partition should have a FAT32 file system while the user can choose between multiple file systems for the root partition. In my configuration, I use EXT4 file system for the root partition. Here is how you can create your partitions:

root # fdisk /dev/sdX

where sdX is the SD card how is it displayed from lsblk.

Then just do the following:

   Type o. This will clear out any partitions on the drive.
   Type p to list partitions. There should be no partitions left.
   Type n, then p for primary, 1 for the first partition on the drive, press ENTER to accept the default first sector, then type +100M for the last sector.
   Type t, then c to set the first partition to type W95 FAT32 (LBA).
   Type n, then p for primary, 2 for the second partition on the drive, and then press ENTER twice to accept the default first and last sector.
   Write the partition table and exit by typing w.

At last, you should format the two partitions doing this:

root # mkfs.vfat /dev/sdX1
root # mkfs.ext4 /dev/sdX2

Then we would mount these partitions:

root # mkdir /mnt/raspberrypiboot
root # mount /dev/sdX1 /mnt/raspberrypiboot
root # mkdir /mnt/raspberrypiroot
root # mount /dev/sdX2 /mnt/raspberrypiroot

Now, the SD card is properly formatted and you shall continue with the next steps

Booting process

The first thing that should be done in order to boot a 64-bit kernel, is to have a 64-bit bootloader.

Das U-Boot

Currently, I have managed to setup my Raspberry Pi 3 with a U-Boot bootloader. It is hard to just download the source code of U-Boot, configure it and compile it. This is not enough because it should be stubbed in order the four cores of the Raspberry to work. I have not managed to stub it on myself, so I use Electron752's stubbed version. You can download it with the following command:

This allows us to have a working bootloader with a shell. But it would be better to have an autoboot. So in order to have an autoboot we need to create a boot.scr.uimg. This is an image which U-Boot loads automatically if its shell is not activated by the user.

In order to create that image, we need to create a list of commands which the bootloader should run in order to boot. The list of commands I use is the following:

   fatload mmc 0:1 ${fdt_addr_r} bcm2837-rpi-3-b.dtb
   fatload mmc 0:1 ${kernel_addr_r} image
   setenv bootargs console=tty0 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
   booti ${kernel_addr_r} - ${fdt_addr_r}

The list of commands should be saved as boot.scr. In the list of commands above, the third line is the one with the kernel command line. While booting we would ignore the built-in command line of the kernel and would use our own. Our own line is configurable in the third line of the boot commands. To it init=/usr/lib/systemd/systemd can be added in order systemd to be enabled on the Raspberry Pi.

When we have boot.scr containing all boot commands, the next step is creating the image. It can be done by directly installing the U-Boot tools.

root # emerge -a dev-embedded/u-boot-tools

After that the following command should be run in order to create the image:

root # mkimage -A arm64 -O linux -T script -C none -n boot.scr -d boot.scr boot.scr.uimg

Then the uboot-stubbed.bin and boot.scr.uimg should be copied into the boot partition:

root # cp uboot-stubbed.bin /mnt/raspberrypiboot
root # cp boot.scr.uimg /mnt/raspberrypiboot

After that, the bootloader setup is ready and we should go to setup the kernel and the root filesystem.

Cross compiler setup

After we setup the booting, we need to setup the kernel. But before we compile the kernel ourselves, we need to install the cross-compiler. We install it doing the following:

root # emerge --ask crossdev -t aarch64-unknown-linux-gnu

That is all for the cross-compiler up to now. It is important to install especially the aarch64-unknown-linux-gnu version, not another one.

Kernel setup

Compiling the system

After we installed the kernel, we need to install the user space. There is not an available stage3 file for the Raspberry Pi 3, so we need to install it on our own. We would do that using the system which crossdev pre-built when we installed the cross-compiler. We would install the bare user space using the @system set provided by portage. First we need to select a profile for Portage. I will suggest choosing the most general one without anything in specific.

root # ln -s /usr/portage/profiles/default/linux/arm64/13.0 /usr/aarch64-unknown-linux-gnu/etc/portage/make.profile

After we installed the cross-compiler, under /usr/aarch64-unknown-linux-gnu is a base of the user space. However, it is not a complete user space some of the packages of the @system set are missing.

The way we would install the @system set is by telling Portage that we want it to compile for AArch64 architecture and then install the @system the proper way.

root # emerge-wrapper --target aarch64-unknomn-linux-gnu --init
root # aarch64-unknown-linux-gnu-emerge @system

Be aware that this way many packages will fail to install, so in order to install the most, we would run them without the C++11 use flag.

root # USE="-cxx" aarch64-unknown-linux-gnu-emerge -DuNav @system

The first error is caused by a missing dependency. That can be fixed easily by installing the dependency. When the errors reach gcc, we are finished installing the @system set for now. Do not panic, the cross compilers do not always work and have issues in them, so that is why we could not compile the @system set. We will compile the part we could not later on the Pi.

After we are ready with the user space we would be able to build with the cross compiler and we would go onto booting the Pi with the kernel and that user space.

Compiling GCC

What follows next?