# 2019q1 Homework4 (smallsys) contributed by < `rebvivi` > ###### tags: `linux2019` * [F08: smallsys](https://hackmd.io/s/BkhVVlHu4) ## 作業要求 1. 回答「自我檢查清單」的所有問題,需要附上對應的參考資料和必要的程式碼,以第一手材料 (包含自己設計的實驗) 為佳 2. 透過 buildroot 工具產生針對 AArch64 的 linux kernel image (需要是 5.0 以上版本) 和 root file system,確保滿足以下要求: - 得以透過 qemu-system-aarch64 開機並正確地執行 userspace 套件 (如 Busybox) - 確認 qemu + gdb 能夠單步執行和設定中斷點 - 確保 VirtIO + 9P 得以運作 - 閱讀 Embedded Linux size reduction techniques,嘗試建構出更小但仍可符合上述需求的 Linux 核心 3. 參照 Linux 核心設計: 透過 eBPF 觀察作業系統行為,在上述 (2) 的環境開啟 eBPF 核心設定和準備必要的 userspace 開發工具,做對應的實驗 ## 自我檢查清單 1. 你是否詳閱 手機裡頭的 ARM 處理器:系列講座 呢?請紀錄學習過程中遇到的問題 2. 請解釋 AArch64 個別通用暫存器的作用,依據 Linux on AArch64 ARM 64-bit Architecture 的描述,搭配實際的程式碼說明。提示: 簡報第 19 頁附有參考資訊 3. AArch64 定義四種例外等級: EL0, EL1, EL2, EL3,請找出相關文件 (儘量是 Arm 公司的第一手材料) 並搭配 Linux 核心原始程式碼解說 4. linux-kernel-module-cheat 提及透過 GDB 對 QEMU 模擬的虛擬硬體之上的 Linux 核心進行追蹤和除錯,請解釋具體原理。提示: 應一併說明 GDB stub 和 QEMU/Debugging with QEMU 的運作機制 ## 透過 buildroot 工具產生針對 AArch64 的 linux kernel image (需要是 5.0 以上版本) 和 root file system - 首先我們要安裝`qemu-system-aarch64` ```cpp $ wget https://download.qemu.org/qemu-2.11.0.tar.xz $ tar xvJf qemu-2.11.0.tar.xz $ cd qemu-2.11.0 ``` - 要記得設定讓 VirtIO + 9P 得以運作,並且產生可以運行 aarch64 架構的 qemu ```cpp $ ./configure --enable-virtfs --target-list=aarch64-softmmu ``` - 如果出現以下錯誤訊息: ```cpp ERROR: VirtFS requires libcap devel and libattr devel ``` 就要安裝以下套件: ```cpp $ sudo apt-get install libcap-dev $ sudo apt-get install libattr1-dev ``` - 之後執行`$ make` - 如果出現以下錯誤訊息: ```cpp util/memfd.c:40:12: error: static declaration of ‘memfd_create’ follows non-static declaration static int memfd_create(const char *name, unsigned int flags) ^~~~~~~~~~~~ In file included from /usr/include/x86_64-linux-gnu/bits/mman-linux.h:115:0, from /usr/include/x86_64-linux-gnu/bits/mman.h:45, from /usr/include/x86_64-linux-gnu/sys/mman.h:41, from /home/peiwen/qemu-2.11.0/include/sysemu/os-posix.h:29, from /home/peiwen/qemu-2.11.0/include/qemu/osdep.h:104, from util/memfd.c:28: /usr/include/x86_64-linux-gnu/bits/mman-shared.h:46:5: note: previous declaration of ‘memfd_create’ was here int memfd_create (const char *__name, unsigned int __flags) __THROW; ^~~~~~~~~~~~ /home/peiwen/qemu-2.11.0/rules.mak:66: recipe for target 'util/memfd.o' failed make: *** [util/memfd.o] Error 1 ``` 就將檔案`util/memfd.c`中的所有 `memfd_create` 更改成 `tmp_memfd_create` - 下載 buildroot ```cpp $ git clone https://git.buildroot.net/buildroot $ cd buildroot ``` - 輸入`$ make menuconfig` - 找到`Target Options --->`中的`Target Architecture`,點進去之後,選擇` AArch64 (little endian)` - 找到`Toolchain --->`中的`Toolchain type`,選擇`External toolchain` - 找到`Target Packages --->`,勾選`Show packages that are also provided by busybox ` - 找到`Filesystem images --->`,勾選`cpio the root filesystem (for use as an initial RAM filesystem)` - 執行`$ make` - 下載 kernel ```cpp $ git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux $ cd linux ``` - 執行`$ ARCH=arm64 make defconfig` - 如果出現以下錯誤訊息: ```cpp /bin/sh: flex:命令找不到 scripts/Makefile.lib:194: recipe for target 'scripts/kconfig/lexer.lex.c' failed make[1]: *** [scripts/kconfig/lexer.lex.c] Error 127 Makefile:552: recipe for target 'defconfig' failed make: *** [defconfig] Error 2 ``` 就執行`$ sudo apt-get install flex` - 配置 kernel ```cpp $ CONFIG_INITRAMFS_SOURCE="/home/peiwen/buildroot/output/images/rootfs.cpio" $ CONFIG_NET_9P=y $ CONFIG_NET_9P_VIRTIO=y ``` - 編譯 kernel ```cpp $ CONFIG_CROSS_COMPILE="aarch64-linux-gnu-" $ ARCH=arm64 make -j 8 ``` - 如果遇到以下錯誤訊息: ```cpp gcc: error: unrecognized command line option ‘-mlittle-endian’; did you mean ‘-fconvert=little-endian’? scripts/Makefile.build:275: recipe for target 'scripts/mod/empty.o' failed make[1]: *** [scripts/mod/empty.o] Error 1 Makefile:1095: recipe for target 'prepare0' failed make: *** [prepare0] Error 2 ``` 執行以下指令: ```cpp $ export CROSS_COMPILE="aarch64-linux-gnu-" ``` - 接著 run kernel 的 Image >現在位置在`qemu-2.11.0`的目錄底下 ```cpp ./aarch64-softmmu/qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -nographic -smp 1 -m 2048 -kernel aarch64-image/Image --append "console=ttyAMA0" ``` - 輸入`root`登入 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up