基於Qemu在ubuntu上構建linux學習環境


 本文的主要內容:

  • Qemu及相關背景簡單介紹
  • 如何安裝Qemu
  • 如何獲取體系結構相關的datasheet
  • 如何使用Qemu啟動kernel

Qemu及相關背景簡單介紹

Qemu是ubuntu下的虛擬機,可以模擬ARM、X86、powerpc。與vmware workstaion是一類軟件。

為什么選擇Qemu而不是其他的虛擬機軟件呢?

  1. vmware只能模擬x86
  2. Qemu很流行,流行意味着資料多
  3. Qemu是開源的,這條對大部分用戶沒啥用,但是一旦想改點什么東西的話,這條就有用了
  4. Qemu可模擬多數的ARM開發板
  5. Qemu支持gdb,可以在執行第一條指令時hang住系統

4、5是選擇Qemu的關鍵原因,即Qemu可模擬ARM開發板

使用過 vmware 虛擬機的同學可能會覺得奇怪,這句是啥意思?vmware配置虛擬機時,只需要配置CPU、內存,硬盤大小等基本信息就行了,怎么還扯出來“開發板”這個詞了。

x86是兼容的,兼容簡單來說,就是任意一台機器,無論是物理機還是虛擬機,都可以直接安裝操作系統,比如windows ,ubuntu,甚至自己編譯的linux kernel,當然驅動程序可能會有問題。如果是虛擬機,那么虛擬機軟件會提供該虛擬機相應的"驅動程序",比如vmware提供windows、ubuntu下的vm tools,安裝好vm tool之后,就能設置正確的分辨率了。嚴格來說,vm tools提供的不單單是虛擬機內的“驅動程序”這么簡單,這里我們就不糾結了。

ARM、Powerpc體系結構不是“兼容”的,這里的不兼容一般是指soc(system on chip)資源不兼容,即“片上IP”可以由生產該CPU的vendor自行設計,比如有多少個ethernet設備,多少個串口,包括這些設備的控制寄存器在系統總線上的地址。模擬ARM、Powerpc展開來說,是指模擬某個ARM or Powerpc的開發板。

顯然,Qemu模擬所有的開發板是做不到的,對ARM體系結構來說,目前(2018.10)Qemu支持的ARM開發板如下。

⋊> ~ qemu-system-arm -M help                                                                                          14:19:52
Supported machines are:
akita                Sharp SL-C1000 (Akita) PDA (PXA270)
ast2500-evb          Aspeed AST2500 EVB (ARM1176)
borzoi               Sharp SL-C3100 (Borzoi) PDA (PXA270)
canon-a1100          Canon PowerShot A1100 IS
cheetah              Palm Tungsten|E aka. Cheetah PDA (OMAP310)
collie               Sharp SL-5500 (Collie) PDA (SA-1110)
connex               Gumstix Connex (PXA255)
cubieboard           cubietech cubieboard
highbank             Calxeda Highbank (ECX-1000)
imx25-pdk            ARM i.MX25 PDK board (ARM926)
integratorcp         ARM Integrator/CP (ARM926EJ-S)
kzm                  ARM KZM Emulation Baseboard (ARM1136)
lm3s6965evb          Stellaris LM3S6965EVB
lm3s811evb           Stellaris LM3S811EVB
mainstone            Mainstone II (PXA27x)
midway               Calxeda Midway (ECX-2000)
musicpal             Marvell 88w8618 / MusicPal (ARM926EJ-S)
n800                 Nokia N800 tablet aka. RX-34 (OMAP2420)
n810                 Nokia N810 tablet aka. RX-44 (OMAP2420)
netduino2            Netduino 2 Machine
none                 empty machine
nuri                 Samsung NURI board (Exynos4210)
palmetto-bmc         OpenPOWER Palmetto BMC (ARM926EJ-S)
raspi2               Raspberry Pi 2
realview-eb          ARM RealView Emulation Baseboard (ARM926EJ-S)
realview-eb-mpcore   ARM RealView Emulation Baseboard (ARM11MPCore)
realview-pb-a8       ARM RealView Platform Baseboard for Cortex-A8
realview-pbx-a9      ARM RealView Platform Baseboard Explore for Cortex-A9
sabrelite            Freescale i.MX6 Quad SABRE Lite Board (Cortex A9)
smdkc210             Samsung SMDKC210 board (Exynos4210)
spitz                Sharp SL-C3000 (Spitz) PDA (PXA270)
sx1                  Siemens SX1 (OMAP310) V2
sx1-v1               Siemens SX1 (OMAP310) V1
terrier              Sharp SL-C3200 (Terrier) PDA (PXA270)
tosa                 Sharp SL-6000 (Tosa) PDA (PXA255)
verdex               Gumstix Verdex (PXA270)
versatileab          ARM Versatile/AB (ARM926EJ-S)
versatilepb          ARM Versatile/PB (ARM926EJ-S)
vexpress-a15         ARM Versatile Express for Cortex-A15
vexpress-a9          ARM Versatile Express for Cortex-A9
virt-2.6             QEMU 2.6 ARM Virtual Machine
virt-2.7             QEMU 2.7 ARM Virtual Machine
virt                 QEMU 2.8 ARM Virtual Machine (alias of virt-2.8)
virt-2.8             QEMU 2.8 ARM Virtual Machine
xilinx-zynq-a9       Xilinx Zynq Platform Baseboard for Cortex-A9
z2                   Zipit Z2 (PXA27x)

 

如何安裝Qemu

網上資料很多,直接sudo apt-get install qemu 就可以了,也可以基於源代碼自己編譯。

 

如何獲取開發板Datasheet

我們以ARM公司的vexpress a9開發板為例,其他vendor的開發板資料獲取方法請自行搜索,我也沒下載過

http://infocenter.arm.com/ 是ARM公司的官方網站,所有的資料都可以在上面下載:

  • Development boards(中文網站顯示為Realview 開發板) 下是ARM開發板的Datasheet,vexpress A9系列涉及的文檔有:

ARM Motherboard Express uATX Technical Reference Manual

ARM CoreTile Express A9*4 Technical Reference Manual

  • CoreLink controllers and peripherals -> CoreLink peripherals 下面是串口等外設的Datasheet,具體涉及到的外設資料這里就不列舉了,后面分析具體驅動時再介紹

如何使用Qemu啟動kernel

首先要編譯 linux kernel+rootfs,關於 kernel、rootfs 的編譯又是一個復雜的話題,就不再本文中描述了。

https://github.com/redstar9451/learn-kernel 提供了kernel、rootfs的編譯方案,請安裝好工具鏈、clone此項目編譯內核、rootfs即可。

啟動虛擬機

$sudo qemu-system-arm -m 512 -M vexpress-a9 \
    -smp 4 \
    -s -serial stdio \
    -kernel $image \
    -initrd $rootfs \
    -dtb $dtb \
    -append "loglevel=0 root=/dev/mmcblk0 console=ttyAMA0"
  • -m 512 // 表示配置512M內存
  • -M vexpress-a9 // 表示模擬 vexpress-a9 開發板, 此處的M 表示machine
  • -smp 4 // enbables 4 CPU cores
  • -s // 開啟gdb server,端口號 1234
  • -serial stdio //使用標准輸入作為終端,使用這個參數可以用ctrl+c終止虛擬機的運行,沒別的作用
  • -kernel $image // zImage 的路徑
  • -initrd $rootfs // rootfs 的路徑
  • -dtb $dtb // dtb的路徑
  • -append xxx // 內核啟動參數

主要在兩個方面使用Qemu來學習kernel或uboot

1. 虛擬的開發板,便於測試與學習

修改內核、編寫kernel module,你可以隨便折騰,永遠不用擔心把設備弄成板磚,並且速度快

2. 使用gdb調試

使用gdb連接到qemu,隨時隨地調試uboot、調試kernel,使用 -S 參數,qemu啟動內核或boot時會hang住,方便調試內核、uboot的啟動過程

⋊> /V/S/learn-kernel on master ⨯ sudo apt install gdb-arm-none-eabi

⋊> /V/S/learn-kernel on master ⨯ arm-none-eabi-gdb GNU gdb (7.10-1ubuntu3+9) 7.10 Copyright (C) 2015 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "--host=x86_64-linux-gnu --target=arm-none-eabi". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word". (gdb) target remote localhost:1234 Remote debugging using localhost:1234 0x80118108 in ?? () (gdb) file .build/kernel/vmlinux A program is being debugged already. Are you sure you want to change the file? (y or n) y Reading symbols from .build/kernel/vmlinux...done. (gdb) bt #0 cpu_v7_do_idle () at /Volumes/Samsung_T5/learn-kernel/linux-kernel/arch/arm/mm/proc-v7.S:80 #1 0x801082f0 in arch_cpu_idle () at /Volumes/Samsung_T5/learn-kernel/linux-kernel/arch/arm/kernel/process.c:75 #2 0x801566e4 in cpuidle_idle_call () at /Volumes/Samsung_T5/learn-kernel/linux-kernel/kernel/sched/idle.c:156 #3 do_idle () at /Volumes/Samsung_T5/learn-kernel/linux-kernel/kernel/sched/idle.c:246 #4 0x801569fc in cpu_startup_entry (state=CPUHP_ONLINE) at /Volumes/Samsung_T5/learn-kernel/linux-kernel/kernel/sched/idle.c:351 #5 0x8066edbc in rest_init () at /Volumes/Samsung_T5/learn-kernel/linux-kernel/init/main.c:436 #6 0x80900c6c in start_kernel () at /Volumes/Samsung_T5/learn-kernel/linux-kernel/init/main.c:716 #7 0x00000000 in ?? () Backtrace stopped: previous frame identical to this frame (corrupt stack?) (gdb)

 

 

Qemu啟動boot的步驟大同小異,這里就不再贅述了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM