Raspberry Pi Bare Bones: Difference between revisions

m
Use {{main}}
[unchecked revision][unchecked revision]
mNo edit summary
m (Use {{main}})
 
(6 intermediate revisions by 4 users not shown)
Line 14:
 
== Building a Cross-Compiler ==
:''Main article: [[{{main|GCC Cross-Compiler]], [[|Why do I need a Cross Compiler?]]}}
 
The first thing you should do is set up a [[GCC Cross-Compiler]] for '''arm-none-eabi'''. You have not yet modified your compiler to know about the existence of your operating system, so we use a generic target called arm-none-eabi, which provides you with a toolchain targeting the [[System V ABI]].
Line 38:
 
The environment is set up and execution to _start transferred from [https://github.com/raspberrypi/tools/blob/master/armstubs/armstub.S#L35 armstub.s].
<sourcesyntaxhighlight lang="text">
// AArch32 mode
 
Line 84:
wfe
b halt
</syntaxhighlight>
</source>
 
The section ".text.boot" will be used in the linker script to place the boot.S as the very first thing in our kernel image. The code initializes a minimum C environment, which means having a stack and zeroing the BSS segment, before calling the kernel_main function. Note that the code avoids using r0-r2 so the remain valid for the kernel_main call.
Line 90:
You can then assemble boot.S using:
 
<sourcesyntaxhighlight lang="bash">
arm-none-eabi-gcc -mcpu=arm1176jzf-s -fpic -ffreestanding -c boot.S -o boot.o
</syntaxhighlight>
</source>
 
===Pi 2===
Line 99:
 
The environment is set up and execution to _start transferred from [https://github.com/raspberrypi/tools/blob/master/armstubs/armstub7.S#L167 armstub7.s].
<sourcesyntaxhighlight lang="text">
// AArch32 mode
 
Line 152:
wfe
b halt
</syntaxhighlight>
</source>
 
You can assemble boot.S using:
 
<sourcesyntaxhighlight lang="bash">
arm-none-eabi-gcc -mcpu=cortex-a7 -fpic -ffreestanding -c boot.S -o boot.o
</syntaxhighlight>
</source>
 
===Pi 3, 4===
Line 167:
 
The environment is set up and execution to _start transferred from [https://github.com/raspberrypi/tools/blob/master/armstubs/armstub8.S#L154 armstub8.s].
<sourcesyntaxhighlight lang="text">
// AArch64 mode
 
Line 191:
ldr x5, =__bss_start
ldr w6, =__bss_size
31: cbz w6, 4f2f
str xzr, [x5], #8
sub w6, w6, #1
cbnz w6, 3b1b
 
// jump to C code, should not return
42: bl kernel_main
// for failsafe, halt this core too
halt:
b 1b
wfe
</source>
b 1bhalt
 
</syntaxhighlight>
 
Compile your code with:
 
<sourcesyntaxhighlight lang="bash">
aarch64-elf-as -c boot.S -o boot.o
</syntaxhighlight>
</source>
 
== Implementing the Kernel ==
Line 220 ⟶ 223:
The following shows how to create a simple kernel in C. Please take a few moments to understand the code. To set the value for "int raspi" in run-time, see [[Detecting_Raspberry_Pi_Board|detecting the board type]].
 
<sourcesyntaxhighlight lang="cpp">
#include <stddef.h>
#include <stdint.h>
Line 302 ⟶ 305:
};
 
void uart_init(int raspi)
{
mmio_init(raspi);
 
// Disable UART0.
mmio_write(UART0_CR, 0x00000000);
Line 387 ⟶ 392:
#endif
{
// initialize MMIO_BASE addressUART for Raspi2
mmio_inituart_init(2);
 
uart_init();
uart_puts("Hello, kernel World!\r\n");
 
Line 396 ⟶ 399:
uart_putc(uart_getc());
}
</syntaxhighlight>
</source>
 
The GPU bootloader passes arguments to the AArch32 kernel via r0-r2 and the boot.S makes sure to preserve those 3 registers. They are the first 3 arguments in a C function call. The argument r0 contains a code for the device the RPi was booted from. This is generally 0 but its actual value depends on the firmware of the board. r1 contains the 'ARM Linux Machine Type' which for the RPi is 3138 (0xc42) identifying the BCM2708 CPU. A full list of ARM Machine Types is available from [http://www.arm.linux.org.uk/developer/machines/ here]. r2 contains the address of the ATAGs.
Line 408 ⟶ 411:
Compile using:
 
<sourcesyntaxhighlight lang="bash">
arm-none-eabi-gcc -mcpu=arm1176jzf-s -fpic -ffreestanding -std=gnu99 -c kernel.c -o kernel.o -O2 -Wall -Wextra
</syntaxhighlight>
</source>
 
or for 64 bit:
 
<sourcesyntaxhighlight lang="bash">
aarch64-elf-gcc -ffreestanding -c kernel.c -o kernel.o -O2 -Wall -Wextra
</syntaxhighlight>
</source>
 
Note that the above code uses a few extensions and hence we build as the GNU version of C99.
Line 426 ⟶ 429:
The linker script for 64 bit mode looks exactly the same, except for the starting address.
 
<sourcesyntaxhighlight lang="text">
ENTRY(_start)
 
Line 471 ⟶ 474:
__end = .;
}
</syntaxhighlight>
</source>
 
There is a lot of text here but don't despair. The script is rather simple if you look at it bit by bit.
Line 479 ⟶ 482:
SECTIONS declares sections. It decides where the bits and pieces of our code and data go and also sets a few symbols that help us track the size of each section.
 
<sourcesyntaxhighlight lang="text">
. = 0x8000;
__start = .;
</syntaxhighlight>
</source>
 
The "." denotes the current address so the first line tells the linker to set the current address to 0x8000 (or 0x80000), where the kernel starts. The current address is automatically incremented when the linker adds data. The second line then creates a symbol "__start" and sets it to the current address.
Line 488 ⟶ 491:
After that sections are defined for text (code), read-only data, read-write data and BSS (0 initialized memory). Other than the name the sections are identical so lets just look at one of them:
 
<sourcesyntaxhighlight lang="text">
__text_start = .;
.text : {
Line 496 ⟶ 499:
. = ALIGN(4096); /* align to page size */
__text_end = .;
</syntaxhighlight>
</source>
 
The first line creates a __text_start symbol for the section. The second line opens a .text section for the output file which gets closed in the fifth line. Lines 3 and 4 declare what sections from the input files will be placed inside the output .text section. In our case ".text.boot" is to be placed first followed by the more general ".text". ".text.boot" is only used in boot.S and ensures that it ends up at the beginning of the kernel image. ".text" then contains all the remaining code. Any data added by the linker automatically increments the current address ("."). In line 6 we explicitly increment it so that it is aligned to a 4096 byte boundary (which is the page size for the RPi). And last line 7 creates a __text_end symbol so we know where the section ends.
Line 502 ⟶ 505:
What are the __text_start and __text_end for and why use page alignment? The 2 symbols can be used in the kernel source and the linker will then place the correct addresses into the binary. As an example the __bss_start and __bss_end are used in boot.S. But you can also use the symbols from C by declaring them extern first. While not required I made all sections aligned to page size. This later allows mapping them in the page tables with executable, read-only and read-write permissions without having to handle overlaps (2 sections in one page).
 
<sourcesyntaxhighlight lang="text">
__end = .;
</syntaxhighlight>
</source>
 
After all sections are declared the __end symbol is created. If you ever want to know how large your kernel is at runtime you can use __start and __end to find out.
Line 512 ⟶ 515:
You can then link your kernel using:
 
<sourcesyntaxhighlight lang="bash">
arm-none-eabi-gcc -T linker.ld -o myos.elf -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc
arm-none-eabi-objcopy myos.elf -O binary kernel7.img
</syntaxhighlight>
</source>
 
or for 64 bit:
 
<sourcesyntaxhighlight lang="bash">
aarch64-elf-gcc -T linker.ld -o myos.elf -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc
aarch64-elf-objcopy myos.elf -O binary kernel8.img
</syntaxhighlight>
</source>
 
== Booting the Kernel ==
Line 534 ⟶ 537:
Now mount the first partition from the SD card and look at it:
 
<sourcesyntaxhighlight lang="text">
bootcode.bin fixup.dat kernel.img start.elf
cmdline.txt fixup_cd.dat kernel_cutdown.img start_cd.elf
config.txt issue.txt kernel_emergency.img
</syntaxhighlight>
</source>
 
If you don't have a raspbian image, you can create a FAT32 partition, and [https://github.com/raspberrypi/firmware/tree/master/boot download the firmware files] from the official repository. You'll need only three files:
Line 553 ⟶ 556:
Your Minicom should then show the following:
 
<sourcesyntaxhighlight lang="text">
Hello, kernel World!
</syntaxhighlight>
</source>
 
=== Testing your operating system (QEMU) ===
Line 565 ⟶ 568:
With QEMU you do not need to objcopy the kernel into a plain binary; QEMU also supports ELF kernels:
 
<sourcesyntaxhighlight lang="text">
$YOURINSTALLLOCATION/bin/qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel kernel.elf
</syntaxhighlight>
</source>
 
==== Updated Support for AArch64 (raspi2, raspi3) ====
As of QEMU 2.12 (April 2018), emulation for 64-bit ARM, ''qemu-system-aarch64'', now supports direct emulation of both Raspberry Pi 2 and 3 using the machine types '''raspi2''' and '''raspi3''', respectively. This should allow for testing of 64-bit system code.
 
<sourcesyntaxhighlight lang="bash">
qemu-system-aarch64 -M raspi3 -serial stdio -kernel kernel8.img
</syntaxhighlight>
</source>
 
Note that in most cases, there will be few if any differences between 32-bit ARM code and 64-bit ARM, but there can be difference in the way the code behaves, in particular regarding kernel memory management. Also, some AArch64 implementations may support features not found on any of their 32-bit counterparts (e.g., cryptographic extensions, enhanced NEON SIMD support).
Line 594 ⟶ 597:
 
[[Category:ARM]]
[[Category:ARM_RaspberryPiRaspberry Pi]]
[[Category:Bare bones tutorials]]
[[Category:C]]