2019年6月23日 星期日

Build the Linux Kernel and Busybox for ARM and run them on QEMU



安裝corss-compiler
brook@vista:~/qemu$ wget http://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/arm-linux-gnueabihf/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz 
brook@vista:~/qemu$ tar xvf gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz
brook@vista:~/qemu$ sudo mv gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf /opt/

編譯kernel
brook@vista:~/qemu$ cd linux-arm
brook@vista:~/qemu/linux-arm$ export ARCH=arm
brook@vista:~/qemu/linux-arm$ export CROSS_COMPILE=arm-linux-gnueabihf-
brook@vista:~/qemu/linux-arm$ export PATH=/opt/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf/bin:$PATH
brook@vista:~/qemu/linux-arm$ make vexpress_defconfig
brook@vista:~/qemu/linux-arm$ make -j8

編譯busybox
brook@vista:~/qemu/busybox$ export ARCH=arm
brook@vista:~/qemu/busybox$ export CROSS_COMPILE=arm-linux-gnueabihf-
brook@vista:~/qemu/busybox$ make O=/home/brook/qemu/busybox-build menuconfig
--> change to static busybox

brook@vista:~/qemu/busybox$ cd ../busybox-build
brook@vista:~/qemu/busybox-build$ make -j8
brook@vista:~/qemu/busybox-build$ cp busybox ../initrd-arm/bin/busybox

generate initrd-image
brook@vista:~/qemu$ ./linux-arm/usr/gen_initramfs_list.sh -d initrd-arm > /tmp/brook_initramfs_list
brook@vista:~/qemu$ ./linux-arm/usr/gen_init_cpio /tmp/brook_initramfs_list > initrd-arm.img


執行QEMU
brook@vista:~/qemu$ /usr/local/bin/qemu-system-arm -M vexpress-a9 -m 512M -kernel ./linux-arm/arch/arm/boot/zImage -dtb ./linux-arm/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -initrd ./initrd-arm.img -nographic -append "console=ttyAMA0"



    參考資料:
  1. http://albert-oma.blogspot.com/2017/12/qemu-arm-linux.html, [QEMU] 模擬一個實際的 ARM Linux 嵌入式系統
  2. https://www.centennialsoftwaresolutions.com/blog/build-the-linux-kernel-and-busybox-for-arm-and-run-them-on-qemu, Build the Linux Kernel and Busybox for ARM and run them on QEMU





6 則留言:

  1. brook@vista:~/qemu$ sudo tunctl -u jpr -t tap0
    Set 'tap0' persistent and owned by uid 1000

    brook@vista:~/qemu$ sudo ifconfig tap0 192.168.1.1

    brook@vista:~/qemu$ /usr/local/bin/qemu-system-arm -M vexpress-a9 -m 512M -kernel ./linux-arm/arch/arm/boot/zImage -dtb ./linux-arm/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -initrd ./initrd-arm.img -nographic -append "console=ttyAMA0" -nic tap,ifname=tap0

    ----- boot message -----

    Please press Enter to activate this console. / #
    / # ifconfig lo up

    / # ifconfig eth0 192.168.1.2 up
    Generic PHY 4e000000.ethernet-ffffffff:01: attached PHY driver [Generic PHY] (mii_bus:phy_addr=4e000000.ethernet-ffffffff:01, irq=POLL)
    smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xa08d0000, IRQ: 22

    / # ping 192.168.1.1 -c 1
    PING 192.168.1.1 (192.168.1.1): 56 data bytes
    64 bytes from 192.168.1.1: seq=0 ttl=64 time=86.415 ms

    --- 192.168.1.1 ping statistics ---
    1 packets transmitted, 1 packets received, 0% packet loss
    round-trip min/avg/max = 86.415/86.415/86.415 ms

    回覆刪除
  2. Kernel hacking --->
    [*] Kernel debugging
    Compile-time checks and compiler options --->
    [*] Compile the kernel with debug info
    [*] Provide GDB scripts for kernel debugging

    回覆刪除
  3. https://lwn.net/Articles/753639/
    GCC optimization for better debug experience (-Og)
    Then introduce new config CC_OPTIMIZE_FOR_DEBUGGING which apply '-Og'

    回覆刪除
  4. for v4.19-rc8
    https://github.com/0day-ci/linux/commits/changbin-du-intel-com/kernel-hacking-GCC-optimization-for-better-debug-experience
    -Og/20180606-001415
    ---
    Makefile | 4 ++++
    arch/arm/mm/mmu.c | 2 +-
    include/linux/compiler-gcc.h | 2 +-
    include/linux/compiler.h | 2 +-
    lib/Kconfig.debug | 21 +++++++++++++++++++++
    5 files changed, 28 insertions(+), 3 deletions(-)

    diff --git a/Makefile b/Makefile
    index bf3786e..70e059b 100644
    --- a/Makefile
    +++ b/Makefile
    @@ -657,6 +657,9 @@ KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
    KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
    KBUILD_CFLAGS += $(call cc-disable-warning, int-in-bool-context)

    +ifdef CONFIG_DEBUG_EXPERIENCE
    +KBUILD_CFLAGS += $(call cc-option, -Og)
    +else
    ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
    KBUILD_CFLAGS += $(call cc-option,-Oz,-Os)
    KBUILD_CFLAGS += $(call cc-disable-warning,maybe-uninitialized,)
    @@ -667,6 +670,7 @@ else
    KBUILD_CFLAGS += -O2
    endif
    endif
    +endif

    KBUILD_CFLAGS += $(call cc-ifversion, -lt, 0409, \
    $(call cc-disable-warning,maybe-uninitialized,))
    diff --git a/arch/arm/mm/mmu.c b/arch/arm/mm/mmu.c
    index e46a6a4..c08d74e 100644
    --- a/arch/arm/mm/mmu.c
    +++ b/arch/arm/mm/mmu.c
    @@ -1599,7 +1599,7 @@ static void __init early_fixmap_shutdown(void)
    pte_t *pte;
    struct map_desc map;

    - map.virtual = fix_to_virt(i);
    + map.virtual = __fix_to_virt(i);
    pte = pte_offset_early_fixmap(pmd_off_k(map.virtual), map.virtual);

    /* Only i/o device mappings are supported ATM */
    diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
    index 4d36b27..ea46678 100644
    --- a/include/linux/compiler-gcc.h
    +++ b/include/linux/compiler-gcc.h
    @@ -85,7 +85,7 @@

    #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)

    -#ifndef __CHECKER__
    +#if !defined(__CHECKER__) && !defined(CONFIG_DEBUG_EXPERIENCE)
    #define __compiletime_warning(message) __attribute__((warning(message)))
    #define __compiletime_error(message) __attribute__((error(message)))

    diff --git a/include/linux/compiler.h b/include/linux/compiler.h
    index 681d866..a237661 100644
    --- a/include/linux/compiler.h
    +++ b/include/linux/compiler.h
    @@ -320,7 +320,7 @@ static inline void *offset_to_ptr(const int *off)
    * sparse see a constant array size without breaking compiletime_assert on old
    * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
    */
    -# ifndef __CHECKER__
    +# if !defined(__CHECKER__) && !defined(CONFIG_DEBUG_EXPERIENCE)
    # define __compiletime_error_fallback(condition) \
    do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
    # endif
    diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
    index 4966c4f..fe09872 100644
    --- a/lib/Kconfig.debug
    +++ b/lib/Kconfig.debug
    @@ -2033,6 +2033,27 @@ config IO_STRICT_DEVMEM
    if the driver using a given range cannot be disabled.

    If in doubt, say Y.
    +config DEBUG_EXPERIENCE
    + bool "Optimize for better debugging experience (-Og)"
    + default n
    + select NO_AUTO_INLINE
    + depends on !CC_OPTIMIZE_FOR_SIZE
    + help
    + This will apply GCC '-Og' optimization level get supported from
    + GCC 4.8. This optimization level offers a reasonable level of
    + optimization while maintaining fast compilation and a good
    + debugging experience. It is similar to '-O1' while perfer keeping
    + debug ability over runtime speed. The overall performance will
    + drop a bit.
    +
    + If enabling this option break your kernel, you should either
    + disable this or find a fix (mostly in the arch code). Currently
    + this option has only be tested in qemu x86_64 guest.
    +
    + Use only if you want to debug the kernel, especially if you want
    + to have better kernel debugging experience with gdb facilities
    + like kgdb and qemu.
    +

    source "arch/$(SRCARCH)/Kconfig.debug"

    回覆刪除
  5. https://t.codebug.vip/questions-1106650.htm

    使用-nographic執行qemu時如何將Ctrl-C傳遞给guest虛擬機器?
    你可以尝試使用 sendkey 用 -monitor stdio命令 .虽然顯然是 -display none -serial mon:stdio 選項應该將Ctrl+c傳遞给訪客。

    您還可以建立一个监视器套接字,如下所示:

    qemu -display none -monitor unix:/tmp/qemu-monitor,server,nowait
    然後,連線到监视器以使用sendkey和 socat 像這樣:

    socat - UNIX-CONNECT:/tmp/qemu-monitor
    您現在可以發送密钥,如下所示:

    (qemu) sendkey ctrl-c
    sendkey ctrl-c

    回覆刪除
  6. qemu-system-arm -M vexpress-a9 -m 256M -nographic -kernel u-boot
    或者
    qemu-system-arm -M vexpress-a9 -m 256M -serial stdio -kernel u-boot
    发现,如果没有指定-nographics, 则必须要加-serial stdio才会有打印。

    回覆刪除

熱門文章