QEMU运行ARM64 Linux内核

QEMU运行ARM64 Linux内核

安装必要工具

主机环境:Ubuntu 20.04.6 x86_64

sudo apt install gcc-arm-linux-gnueabi gcc-aarch64-linux-gnu gdb-multiarch
sudo apt install qemu qemu-system-arm qemu-user-static
sudo apt install libncurses5-dev build-essential bison flex libssl-dev

获取BusyBox和Kernel源码

目标版本:busybox 1.36 + linux kernel 5.15

# BusyBox
wget https://www.busybox.net/downloads/busybox-1.36.1.tar.bz2 && tar xf busybox-1.36.1.tar.bz2
# Linux Kernel
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.151.tar.xz && tar xf linux-5.15.151.tar.xz

构建最小根文件系统

$ cd busybox
$ export ARCH=arm64
$ export CROSS_COMPILE=aarch64-linux-gnu-
$ make menuconfig   # Settings -> Build static binary(no shared libs)
$ make install

编译完成后,在busybox根目录下会有一个_install目录,该目录是最小文件系统需要的一些命令集合。

Tips:这里需要确认编译出的busybox是ARM架构的,否则后边Qemu会启动失败。

$ file _install/bin/busybox
_install/bin/busybox: ELF 64-bit LSB executable, ARM aarch64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=ab2d6ba8b52797249281a738c86354532e7b38aa, for GNU/Linux 3.7.0, stripped

但仅有这些还不够,还需要创建Linux启动所需的一些关键文件/目录。

# 把`_install`目录移动到Kernel根目录下
$ mv /path/to/busybox/_install /path/to/linux-5.15.151/
$ cd linux-5.15.151/_install/

$ mkdir -p etc/init.d dev mnt
$ touch etc/init.d/rcS
$ cat <<EOF > etc/init.d/rcS
mkdir -p /proc
mkdir -p /tmp
mkdir -p /sys
mkdir -p /mnt
/bin/mount -a
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
EOF
$ chmod +x etc/init.d/rcS

$ touch etc/fstab
$ cat <<EOF > etc/fstab
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /dev tmpfs defaults 0 0
debugfs /sys/kernel/debug debugfs defaults 0 0
EOF

$ touch etc/inittab
$ cat <<EOF > etc/inittab
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::askfirst:-/bin/sh
::ctrlaltdel:/bin/umount -a -r
EOF

$ sudo mknod dev/null c 1 3
$ sudo mknod dev/console c 5 1

至此,最小根文件系统就制作好啦。

编译Kernel

$ cd /path/to/linux-5.15.151/
$ export ARCH=arm64
$ export CROSS_COMPILE=aarch64-linux-gnu-

# 配置内核
$ make defconfig
$ make menuconfig
# General Setup -> Initramfs source file(s) 填入 '_install'
# 在 make menuconfig结束,可以查看.config文件,确认包含下面配置项:
#   CONFIG_INITRAMFS_SOURCE="_install"
#   CONFIG_CMDLINE=""

# 编译内核
$ make -j4

Kernel编译完成后,会生成arch/arm64/boot/Image文件。

运行QEMU

运行QEMU来模拟2核Cortex-A57开发平台

$ cd /path/to/linux-5.15.151/
$ qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -nographic -m 2048 -smp 2 -kernel arch/arm64/boot/Image --append "rdinit=/linuxrc console=ttyAMA0" 

运行日志如下:

[    0.000000] Booting Linux on physical CPU 0x0000000000 [0x411fd070]
[    0.000000] Linux version 5.15.151 (it@it-ThinkPad) (aarch64-linux-gnu-gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0, GNU ld (GNU Binutils for Ubuntu) 2.34) #1 SMP PREEMPT Mon Aug 26 19:12:26 CST 2024
[    0.000000] Machine model: linux,dummy-virt
[    0.000000] efi: UEFI not found.
[    0.000000] NUMA: No NUMA configuration found
[    0.000000] NUMA: Faking a node at [mem 0x0000000040000000-0x00000000bfffffff]
[    0.000000] NUMA: NODE_DATA [mem 0xbfbf2c00-0xbfbf4fff]
[    0.000000] Zone ranges:
[    0.000000]   DMA      [mem 0x0000000040000000-0x00000000bfffffff]
[    0.000000]   DMA32    empty
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000040000000-0x00000000bfffffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000040000000-0x00000000bfffffff]
[    0.000000] cma: Reserved 32 MiB at 0x00000000bb800000
[    0.000000] psci: probing for conduit method from DT.
[    0.000000] psci: PSCIv0.2 detected in firmware.
[    0.000000] psci: Using standard PSCI v0.2 function IDs
[    0.000000] psci: Trusted OS migration not required
[    0.000000] percpu: Embedded 20 pages/cpu s41560 r8192 d32168 u81920
[    0.000000] Detected PIPT I-cache on CPU0
[    0.000000] CPU features: detected: Spectre-v2
[    0.000000] CPU features: detected: Spectre-v3a
[    0.000000] CPU features: detected: Spectre-v4
[    0.000000] CPU features: detected: Spectre-BHB
[    0.000000] CPU features: detected: ARM erratum 834220
[    0.000000] CPU features: detected: ARM erratum 1742098
[    0.000000] CPU features: detected: ARM erratum 832075
[    0.000000] CPU features: detected: ARM errata 1165522, 1319367, or 1530923
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 516096
[    0.000000] Policy zone: DMA
[    0.000000] Kernel command line: rdinit=/linuxrc console=ttyAMA0
[    0.000000] Dentry cache hash table entries: 262144 (order: 9, 2097152 bytes, linear)
[    0.000000] Inode-cache hash table entries: 131072 (order: 8, 1048576 bytes, linear)
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] Memory: 1988784K/2097152K available (14976K kernel code, 3090K rwdata, 8332K rodata, 7360K init, 497K bss, 75600K reserved, 32768K cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=2, Nodes=1
[    0.000000] rcu: Preemptible hierarchical RCU implementation.
[    0.000000] rcu:     RCU event tracing is enabled.
[    0.000000] rcu:     RCU restricting CPUs from NR_CPUS=256 to nr_cpu_ids=2.
[    0.000000]     Trampoline variant of Tasks RCU enabled.
[    0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 25 jiffies.
[    0.000000] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=2
[    0.000000] NR_IRQS: 64, nr_irqs: 64, preallocated irqs: 0
[    0.000000] Root IRQ handler: gic_handle_irq
[    0.000000] GICv2m: range[mem 0x08020000-0x08020fff], SPI[80:143]
[    0.000000] arch_timer: cp15 timer(s) running at 62.50MHz (virt).
[    0.000000] clocksource: arch_sys_counter: mask: 0xffffffffffffff max_cycles: 0x1cd42e208c, max_idle_ns: 881590405314 ns
[    0.000056] sched_clock: 56 bits at 62MHz, resolution 16ns, wraps every 4398046511096ns
[    0.005893] Console: colour dummy device 80x25
[    0.007394] Calibrating delay loop (skipped), value calculated using timer frequency.. 125.00 BogoMIPS (lpj=250000)
[    0.007486] pid_max: default: 32768 minimum: 301
[    0.007950] LSM: Security Framework initializing
[    0.009908] Mount-cache hash table entries: 4096 (order: 3, 32768 bytes, linear)
[    0.009955] Mountpoint-cache hash table entries: 4096 (order: 3, 32768 bytes, linear)
[    0.032130] rcu: Hierarchical SRCU implementation.
[    0.034639] EFI services will not be available.
[    0.035309] smp: Bringing up secondary CPUs ...
[    0.037873] Detected PIPT I-cache on CPU1
[    0.038360] CPU1: Booted secondary processor 0x0000000001 [0x411fd070]
[    0.040894] smp: Brought up 1 node, 2 CPUs
[    0.040928] SMP: Total of 2 processors activated.
[    0.040973] CPU features: detected: 32-bit EL0 Support
[    0.040992] CPU features: detected: 32-bit EL1 Support
[    0.041036] CPU features: detected: CRC32 instructions
[    0.071389] CPU: All CPU(s) started at EL1
[    0.071692] alternatives: patching kernel code
[    0.085496] devtmpfs: initialized
[    0.091449] KASLR disabled due to lack of seed
[    0.092305] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 7645041785100000 ns
[    0.092409] futex hash table entries: 512 (order: 3, 32768 bytes, linear)
[    0.095749] pinctrl core: initialized pinctrl subsystem
[    0.103293] DMI not present or invalid.
[    0.108502] NET: Registered PF_NETLINK/PF_ROUTE protocol family
[    0.116264] DMA: preallocated 256 KiB GFP_KERNEL pool for atomic allocations
[    0.116709] DMA: preallocated 256 KiB GFP_KERNEL|GFP_DMA pool for atomic allocations
[    0.117028] DMA: preallocated 256 KiB GFP_KERNEL|GFP_DMA32 pool for atomic allocations
[    0.117194] audit: initializing netlink subsys (disabled)
[    0.118695] audit: type=2000 audit(0.112:1): state=initialized audit_enabled=0 res=1
[    0.122180] thermal_sys: Registered thermal governor 'step_wise'
[    0.122232] thermal_sys: Registered thermal governor 'power_allocator'
[    0.122735] cpuidle: using governor menu
[    0.123406] hw-breakpoint: found 6 breakpoint and 4 watchpoint registers.
[    0.123849] ASID allocator initialised with 65536 entries
[    0.127702] Serial: AMBA PL011 UART driver
[    0.155046] 9000000.pl011: ttyAMA0 at MMIO 0x9000000 (irq = 47, base_baud = 0) is a PL011 rev1
[    0.162855] printk: console [ttyAMA0] enabled
[    0.187874] HugeTLB registered 1.00 GiB page size, pre-allocated 0 pages
[    0.188023] HugeTLB registered 32.0 MiB page size, pre-allocated 0 pages
[    0.188151] HugeTLB registered 2.00 MiB page size, pre-allocated 0 pages
[    0.188250] HugeTLB registered 64.0 KiB page size, pre-allocated 0 pages
[    0.196419] cryptd: max_cpu_qlen set to 1000
[    0.203953] ACPI: Interpreter disabled.
[    0.207745] iommu: Default domain type: Translated 
[    0.207848] iommu: DMA domain TLB invalidation policy: strict mode 
[    0.208621] vgaarb: loaded
[    0.209560] SCSI subsystem initialized
[    0.211403] usbcore: registered new interface driver usbfs
[    0.211658] usbcore: registered new interface driver hub
[    0.211844] usbcore: registered new device driver usb
[    0.213213] pps_core: LinuxPPS API ver. 1 registered
[    0.213298] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
[    0.213489] PTP clock support registered
[    0.213888] EDAC MC: Ver: 3.0.0
[    0.217534] FPGA manager framework
[    0.218083] Advanced Linux Sound Architecture Driver Initialized.
[    0.225708] clocksource: Switched to clocksource arch_sys_counter
[    0.227361] VFS: Disk quotas dquot_6.6.0
[    0.227558] VFS: Dquot-cache hash table entries: 512 (order 0, 4096 bytes)
[    0.228638] pnp: PnP ACPI: disabled
[    0.246272] NET: Registered PF_INET protocol family
[    0.247364] IP idents hash table entries: 32768 (order: 6, 262144 bytes, linear)
[    0.251248] tcp_listen_portaddr_hash hash table entries: 1024 (order: 2, 16384 bytes, linear)
[    0.251547] Table-perturb hash table entries: 65536 (order: 6, 262144 bytes, linear)
[    0.251709] TCP established hash table entries: 16384 (order: 5, 131072 bytes, linear)
[    0.252037] TCP bind hash table entries: 16384 (order: 6, 262144 bytes, linear)
[    0.252350] TCP: Hash tables configured (established 16384 bind 16384)
[    0.253194] UDP hash table entries: 1024 (order: 3, 32768 bytes, linear)
[    0.253449] UDP-Lite hash table entries: 1024 (order: 3, 32768 bytes, linear)
[    0.254365] NET: Registered PF_UNIX/PF_LOCAL protocol family
[    0.256394] RPC: Registered named UNIX socket transport module.
[    0.256522] RPC: Registered udp transport module.
[    0.256599] RPC: Registered tcp transport module.
[    0.256670] RPC: Registered tcp NFSv4.1 backchannel transport module.
[    0.256827] PCI: CLS 0 bytes, default 64
[    0.265994] hw perfevents: enabled with armv8_pmuv3 PMU driver, 5 counters available
[    0.266560] kvm [1]: HYP mode not available
[    0.288853] Initialise system trusted keyrings
[    0.290086] workingset: timestamp_bits=42 max_order=19 bucket_order=0
[    0.298211] squashfs: version 4.0 (2009/01/31) Phillip Lougher
[    0.302470] NFS: Registering the id_resolver key type
[    0.302801] Key type id_resolver registered
[    0.302894] Key type id_legacy registered
[    0.303214] nfs4filelayout_init: NFSv4 File Layout Driver Registering...
[    0.303383] nfs4flexfilelayout_init: NFSv4 Flexfile Layout Driver Registering...
[    0.303938] 9p: Installing v9fs 9p2000 file system support
[    0.324340] Key type asymmetric registered
[    0.324445] Asymmetric key parser 'x509' registered
[    0.324720] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 245)
[    0.324907] io scheduler mq-deadline registered
[    0.325032] io scheduler kyber registered
[    0.338480] pl061_gpio 9030000.pl061: PL061 GPIO chip registered
[    0.342314] pci-host-generic 4010000000.pcie: host bridge /pcie@10000000 ranges:
[    0.342832] pci-host-generic 4010000000.pcie:       IO 0x003eff0000..0x003effffff -> 0x0000000000
[    0.343261] pci-host-generic 4010000000.pcie:      MEM 0x0010000000..0x003efeffff -> 0x0010000000
[    0.343443] pci-host-generic 4010000000.pcie:      MEM 0x8000000000..0xffffffffff -> 0x8000000000
[    0.343873] pci-host-generic 4010000000.pcie: Memory resource size exceeds max for 32 bits
[    0.344262] pci-host-generic 4010000000.pcie: ECAM at [mem 0x4010000000-0x401fffffff] for [bus 00-ff]
[    0.345023] pci-host-generic 4010000000.pcie: PCI host bridge to bus 0000:00
[    0.345267] pci_bus 0000:00: root bus resource [bus 00-ff]
[    0.345402] pci_bus 0000:00: root bus resource [io  0x0000-0xffff]
[    0.345505] pci_bus 0000:00: root bus resource [mem 0x10000000-0x3efeffff]
[    0.345679] pci_bus 0000:00: root bus resource [mem 0x8000000000-0xffffffffff]
[    0.346828] pci 0000:00:00.0: [1b36:0008] type 00 class 0x060000
[    0.349296] pci 0000:00:01.0: [1af4:1000] type 00 class 0x020000
[    0.349550] pci 0000:00:01.0: reg 0x10: [io  0x0000-0x001f]
[    0.349750] pci 0000:00:01.0: reg 0x14: [mem 0x00000000-0x00000fff]
[    0.349901] pci 0000:00:01.0: reg 0x20: [mem 0x00000000-0x00003fff 64bit pref]
[    0.350037] pci 0000:00:01.0: reg 0x30: [mem 0x00000000-0x0007ffff pref]
[    0.351751] pci 0000:00:01.0: BAR 6: assigned [mem 0x10000000-0x1007ffff pref]
[    0.352051] pci 0000:00:01.0: BAR 4: assigned [mem 0x8000000000-0x8000003fff 64bit pref]
[    0.352241] pci 0000:00:01.0: BAR 1: assigned [mem 0x10080000-0x10080fff]
[    0.352365] pci 0000:00:01.0: BAR 0: assigned [io  0x1000-0x101f]
[    0.355960] EINJ: ACPI disabled.
[    0.373053] virtio-pci 0000:00:01.0: enabling device (0000 -> 0003)
[    0.382171] Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled
[    0.385491] SuperH (H)SCI(F) driver initialized
[    0.386356] msm_serial: driver initialized
[    0.388719] cacheinfo: Unable to detect cache hierarchy for CPU 0
[    0.401843] loop: module loaded
[    0.403596] megasas: 07.717.02.00-rc1
[    0.407962] physmap-flash 0.flash: physmap platform flash device: [mem 0x00000000-0x03ffffff]
[    0.409021] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[    0.409437] Intel/Sharp Extended Query Table at 0x0031
[    0.410073] Using buffer write method
[    0.410502] physmap-flash 0.flash: physmap platform flash device: [mem 0x04000000-0x07ffffff]
[    0.410911] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[    0.411106] Intel/Sharp Extended Query Table at 0x0031
[    0.411473] Using buffer write method
[    0.411613] Concatenating MTD devices:
[    0.411688] (0): "0.flash"
[    0.411741] (1): "0.flash"
[    0.411790] into device "0.flash"
[    0.453258] tun: Universal TUN/TAP device driver, 1.6
[    0.461810] thunder_xcv, ver 1.0
[    0.461951] thunder_bgx, ver 1.0
[    0.462059] nicpf, ver 1.0
[    0.463927] hclge is initializing
[    0.464094] hns3: Hisilicon Ethernet Network Driver for Hip08 Family - version
[    0.464186] hns3: Copyright (c) 2017 Huawei Corporation.
[    0.464419] e1000: Intel(R) PRO/1000 Network Driver
[    0.464487] e1000: Copyright (c) 1999-2006 Intel Corporation.
[    0.464628] e1000e: Intel(R) PRO/1000 Network Driver
[    0.464710] e1000e: Copyright(c) 1999 - 2015 Intel Corporation.
[    0.464864] igb: Intel(R) Gigabit Ethernet Network Driver
[    0.464976] igb: Copyright (c) 2007-2014 Intel Corporation.
[    0.465172] igbvf: Intel(R) Gigabit Virtual Function Network Driver
[    0.465267] igbvf: Copyright (c) 2009 - 2012 Intel Corporation.
[    0.465897] sky2: driver version 1.30
[    0.467525] VFIO - User Level meta-driver version: 0.3
[    0.471004] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[    0.471174] ehci-pci: EHCI PCI platform driver
[    0.471388] ehci-platform: EHCI generic platform driver
[    0.471622] ehci-orion: EHCI orion driver
[    0.471815] ehci-exynos: EHCI Exynos driver
[    0.472014] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
[    0.472165] ohci-pci: OHCI PCI platform driver
[    0.472369] ohci-platform: OHCI generic platform driver
[    0.472593] ohci-exynos: OHCI Exynos driver
[    0.473540] usbcore: registered new interface driver usb-storage
[    0.479246] rtc-pl031 9010000.pl031: registered as rtc0
[    0.479677] rtc-pl031 9010000.pl031: setting system clock to 2024-08-26T11:32:29 UTC (1724671949)
[    0.480838] i2c_dev: i2c /dev entries driver
[    0.488811] sdhci: Secure Digital Host Controller Interface driver
[    0.488944] sdhci: Copyright(c) Pierre Ossman
[    0.490012] Synopsys Designware Multimedia Card Interface Driver
[    0.491280] sdhci-pltfm: SDHCI platform and OF driver helper
[    0.493532] ledtrig-cpu: registered to indicate activity on CPUs
[    0.496529] usbcore: registered new interface driver usbhid
[    0.496629] usbhid: USB HID core driver
[    0.504398] NET: Registered PF_PACKET protocol family
[    0.505238] 9pnet: Installing 9P2000 support
[    0.505524] Key type dns_resolver registered
[    0.506421] Loading compiled-in X.509 certificates
[    0.512967] input: gpio-keys as /devices/platform/gpio-keys/input/input0
[    0.518520] ALSA device list:
[    0.518639]   No soundcards found.
[    0.520914] uart-pl011 9000000.pl011: no DMA platform data
[    0.549126] Freeing unused kernel memory: 7360K
[    0.549744] Run /linuxrc as init process
/etc/init.d/rcS: line 8: can't create /proc/sys/kernel/hotplug: nonexistent directory

Please press Enter to activate this console. 
~ # ls
bin      etc      mnt      sbin     tmp
dev      linuxrc  proc     sys      usr
~ # cat /proc/cpuinfo 
processor    : 0
BogoMIPS    : 125.00
Features    : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer    : 0x41
CPU architecture: 8
CPU variant    : 0x1
CPU part    : 0xd07
CPU revision    : 0

processor    : 1
BogoMIPS    : 125.00
Features    : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
CPU implementer    : 0x41
CPU architecture: 8
CPU variant    : 0x1
CPU part    : 0xd07
CPU revision    : 0

~ # 

在Ubuntu另一个终端中输入killall qemu-system-aarch64即可关闭QEMU平台。

参考:

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值