C Index

System Development

System development requires knowing how to debug kernel know how, in this example will be used Qemu and GDB. Qemu creates the virtual machine that kernel will run on and GDB will connect to it to help us understand how things tick.

Kernel Build

        $ tar xf linux-4.9.48.tar.xz
        $ cd linux-4.9.48
        

Default configuration disable some security configurations that allow us to debug (random memory layout).

        $ make x86_64_defconfig
        

Enable CONFIG_DEBUG_INFO, CONFIG_DEBUG_INFO_DWARF4 and CONFIG_GDB_SCRIPTS in the kernel;

        make x86_64_defconfig
        cat <<EOF >.config-fragment
        CONFIG_DEBUG_INFO=y
        CONFIG_DEBUG_KERNEL=y
        CONFIG_GDB_SCRIPTS=y
        EOF
        ./scripts/kconfig/merge_config.sh .config .config-fragment
        

Check or change to your needs the configuration;

        $ make nconfig
        

Build Kernel and modules;

        $ make -j $(nproc)
        

Simple Init

Now that you have the kernel compiled you can create a simple init program, this program is called when kernel finish to load and setup its internals and is ready to launch first process. Init program should not exit or kernel will panic. Create init.S;

        .global _start
        _start:
            mov $1, %rax
            mov $1, %rdi
            mov $message, %rsi
            mov $message_len, %rdx
            syscall
            jmp .
            message: .ascii "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n"
            .equ message_len, . - message
        

Assemble, link and create simple initial ram disk;

        mkdir d
        as --64 -o init.o init.S # assemble
        ld -o d/init init.o      # link
        cd d
        find . | cpio -o -H newc | gzip > ../rootfs.cpio.gz
        

Clean temporary directory;

        cd ..
        rm -r d/
        

Can be used C to create init program;

        #include <stdio.h>
        #include <unistd.h>

        int main() {
            printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n");
            sleep(0xFFFFFFFF);
            return 0;
        }
        
        $ gcc -static init.c -o init
        

Start Debugging

Test qemu, kernel and simple init program, you should see "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR";

        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86_64/boot/bzImage \
        --initrd rootfs.cpio.gz
        

If everything goes well you can start qemu without starting the CPU (-S) and with gdb server on TCP port 1234 (-s).

        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86/boot/bzImage \
                --initrd rootfs.cpio.gz \
                -S -s
        

On another terminal start gdb;

        gdb \
            -ex "add-auto-load-safe-path $(pwd)" \
            -ex "file vmlinux" \
            -ex 'set arch i386:x86-64:intel' \
            -ex 'target remote localhost:1234' \
            -ex 'break start_kernel' \
            -ex 'continue' \
            -ex 'disconnect' \
            -ex 'set arch i386:x86-64' \
            -ex 'target remote localhost:1234'
        
	(gdb) info thread
	  Id   Target Id         Frame
	* 1    Thread 1 (CPU#0 [running]) start_kernel () at init/main.c:480
	
	(gdb) info frame
	Stack level 0, frame at 0xffffffff81e03f90:
	 rip = 0xffffffff81f4db2d in start_kernel (init/main.c:480); saved rip = 0xffffffff81f4d28e
	 called by frame at 0xffffffff81e03fa0
	 source language c.
	 Arglist at 0xffffffff81e03f80, args:
	 Locals at 0xffffffff81e03f80, Previous frame's sp is 0xffffffff81e03f90
	 Saved registers:
	  rip at 0xffffffff81e03f88
	
	(gdb) print $rip
	$2 = (void (*)()) 0xffffffff81f4db2d <start_kernel>
	(gdb)
	
C Index

This is part of the Tribu System Documentation. Copyright (C) 2020 Tribu Team. See the file Gnu Free Documentation License for copying conditions.