📜 ⬆️ ⬇️

Linux for ARM in qemu emulator

Displaying something on the VersatilePB emulated device is not easy. All examples of simple kernels for ARM that were found at the time of writing this article are limited to working with a serial port.

This post is the beginning of a series telling how a simple kernel was going to display an emulated device on screen.

Using the example of 2 and a few thousand lines of code, the memory initialization, memory zones, and the slab allocator used in Linux will be described in detail.
')



Kernel build for ARM architecture (for example linux-2.6.32.3)


The commands given below are taken from * .cmd files. These files are generated automatically by the kernel build system, but no one forbids the use of commands directly.

The kernel launched in qemu ./arch/arm/boot/zImage is obtained by cutting off unnecessary sections from the compiled decompression code:

arm-unknown-linux-gnueabi-objcopy -O binary -R .note -R .note.gnu.build-id -R .comment -S arch/arm/boot/compressed/vmlinux arch/arm/boot/zImage 


This code is compiled from a library (libgcc.a), a file containing an entry point ( head.o ), a file in which binary data of the packed kernel ( piggy.o ) is included, and a C code that performs unpacking ( misc.o ):

 /opt/arm/bin/arm-unknown-linux-gnueabi-ld -EL --defsym zreladdr=0x00008000 --defsym initrd_phys=0x00800000 --defsym params_phys=0x00000100 -p --no-undefined -X /opt/arm/bin/../lib/gcc/arm-unknown-linux-gnueabi/4.4.1/libgcc.a -T arch/arm/boot/compressed/vmlinux.lds arch/arm/boot/compressed/head.o arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/misc.o -o arch/arm/boot/compressed/vmlinux 


Packed core is added to the piggy.S line:

 .incbin "arch/arm/boot/compressed/piggy.gz" 


piggy.o is compiled by the command:

 /opt/arm/bin/arm-unknown-linux-gnueabi-gcc -Wp,-MD,arch/arm/boot/compressed/.piggy.od -nostdinc -isystem /opt/arm/bin/../lib/gcc/arm-unknown-linux-gnueabi/4.4.1/include -Iinclude -I/home/tlx/linux-2.6.32.3_e/arch/arm/include -include include/linux/autoconf.h -D__KERNEL__ -mlittle-endian -Iarch/arm/mach-versatile/include -D__ASSEMBLY__ -mabi=apcs-gnu -mno-thumb-interwork -D__LINUX_ARM_ARCH__=5 -march=armv5te -mtune=arm9tdmi -include asm/unified.h -msoft-float -Wa,-march=all -c -o arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/piggy.S 


File piggy.gz is obtained with the command:

 cat arch/arm/boot/compressed/../Image | gzip -f -9 > arch/arm/boot/compressed/piggy.gz 


Notice the two dots between the compressed and Image directories. They mean going up one level in the file system tree, i.e. Image located in arch / arm / boot / .
Such difficulties are due to the automatic generation of assembly commands.

Image is obtained by cutting off unnecessary sections from the compiled kernel .:

 /opt/arm/bin/arm-unknown-linux-gnueabi-objcopy -O binary -R .note -R .note.gnu.build-id -R .comment -S vmlinux arch/arm/boot/Image 


An unpacked kernel ( vmlinux ) is obtained as follows:

 /opt/arm/bin/arm-unknown-linux-gnueabi-ld -EL -p --no-undefined -X --build-id -o vmlinux -T arch/arm/kernel/vmlinux.lds arch/arm/kernel/head.o arch/arm/kernel/init_task.o init/built-in.o --start-group usr/built-in.o arch/arm/kernel/built-in.o arch/arm/mm/built-in.o arch/arm/common/built-in.o arch/arm/mach-versatile/built-in.o arch/arm/nwfpe/built-in.o arch/arm/vfp/built-in.o kernel/built-in.o mm/built-in.o fs/built-in.o ipc/built-in.o security/built-in.o crypto/built-in.o block/built-in.o arch/arm/lib/lib.a lib/lib.a arch/arm/lib/built-in.o lib/built-in.o drivers/built-in.o sound/built-in.o firmware/built-in.o net/built-in.o --end-group .tmp_kallsyms2.o 


Finally, the main.c file, which we will consider is part of init / built-in.o :

 arm-unknown-linux-gnueabi-ld -EL -r -o init/built-in.o init/main.o init/version.o init/mounts.o init/initramfs.o init/calibrate.o 


After completing the work of separating the necessary code from the kernel source tree, the following command sequence was obtained, allowing you to build a minimal kernel that can display information on the ARM architecture emulator display:

 ~user/arm-2011.09/bin/arm-none-linux-gnueabi-gcc -nostdinc -mlittle-endian -mabi=apcs-gnu -mno-thumb-interwork -D__LINUX_ARM_ARCH__=5 -march=armv5te -mtune=arm9tdmi -msoft-float -DTEXT_OFFSET=0x00008000 -c -o arch/arm/kernel/head.o arch/arm/kernel/head.S ~user/arm-2011.09/bin/arm-none-linux-gnueabi-gcc -nostdinc -mlittle-endian -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -fno-delete-null-pointer-checks -Os -marm -fno-omit-frame-pointer -mapcs -mno-sched-prolog -mabi=apcs-gnu -mno-thumb-interwork -D__LINUX_ARM_ARCH__=5 -march=armv5te -mtune=arm9tdmi -msoft-float -Uarm -Wframe-larger-than=1024 -fno-stack-protector -fno-omit-frame-pointer -fno-optimize-sibling-calls -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fno-dwarf2-cfi-asm -fconserve-stack -c -o init/main.o init/main.c ~user/arm-2011.09/bin/arm-none-linux-gnueabi-ld -EL -p --no-undefined -X --build-id -o vmlinux -T arch/arm/kernel/vmlinux.lds arch/arm/kernel/head.o init/main.o --start-group --end-group ~user/arm-2011.09/bin/arm-none-linux-gnueabi-objcopy -O binary -R .note -R .note.gnu.build-id -R .comment -S vmlinux arch/arm/boot/Image cat arch/arm/boot/compressed/../Image | gzip -f -9 > arch/arm/boot/compressed/piggy.gz ~user/arm-2011.09/bin/arm-none-linux-gnueabi-gcc -nostdinc -mlittle-endian -mabi=apcs-gnu -mno-thumb-interwork -march=armv5te -mtune=arm9tdmi -msoft-float -march=armv5te -c -o arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/piggy.S ~user/arm-2011.09/bin/arm-none-linux-gnueabi-gcc -nostdinc -D__KERNEL__ -mlittle-endian -D__ASSEMBLY__ -mabi=apcs-gnu -mno-thumb-interwork -D__LINUX_ARM_ARCH__=5 -march=armv5te -mtune=arm9tdmi -msoft-float -c -o arch/arm/boot/compressed/head.o arch/arm/boot/compressed/head.S ~user/arm-2011.09/bin/arm-none-linux-gnueabi-gcc -nostdinc -D__KERNEL__ -mlittle-endian -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -fno-delete-null-pointer-checks -Os -marm -fno-omit-frame-pointer -mapcs -mno-sched-prolog -mabi=apcs-gnu -mno-thumb-interwork -D__LINUX_ARM_ARCH__=5 -march=armv5te -mtune=arm9tdmi -msoft-float -Uarm -Wframe-larger-than=1024 -fno-stack-protector -fno-omit-frame-pointer -fno-optimize-sibling-calls -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fno-dwarf2-cfi-asm -fconserve-stack -fpic -fno-builtin -Dstatic= -c -o arch/arm/boot/compressed/misc.o arch/arm/boot/compressed/misc.c ~user/arm-2011.09/bin/arm-none-linux-gnueabi-ld -EL --defsym zreladdr=0x00008000 --defsym initrd_phys=0x00800000 --defsym params_phys=0x00000100 -p --no-undefined -X -T arch/arm/boot/compressed/vmlinux.lds arch/arm/boot/compressed/head.o arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/misc.o -o arch/arm/boot/compressed/vmlinux ~user/arm-2011.09/bin/arm-none-linux-gnueabi-objcopy -O binary -R .note -R .note.gnu.build-id -R .comment -S arch/arm/boot/compressed/vmlinux zImage 


~ user / arm-2011.09 / bin / - the path starting from the author’s home directory to the directory containing the toolchain. If you copy the toolchain for ARM into your home directory and change the “user” to the username, then you should succeed.

Commands are combined into an executable make file (not to be confused with the utility of the same name).

The code immediately after separation from the kernel source tree, including everything that will be discussed in the following posts arm_qemu_max .

Abbreviated version, without initialization of memory and slab-allocator (display only) arm_qemu_min .

The text of the remaining articles written. It remains only to publish.

Source: https://habr.com/ru/post/143808/


All Articles