QEMU仿真aarch64处理器运行Linux kernel

编译ARM64仿真器QEMU

下载qemu源码

$ wget https://download.qemu.org/qemu-6.0.0.tar.xz

解压tar包

$ tar xvf qemu-6.0.0.tar.xz

配置qemu

$ cd qemu-6.0.0.tar.xz
$ ./configure --prefix=~/opt/qemu --target-list="arm-softmmu aarch64-softmmu i386-softmmu x86_64-softmmu arm-linux-user aarch64-linux-user i386-linux-user x86_64-linux-user riscv64-softmmu" --enable-sdl --enable-debug

编译qemu

$ make -j8

安装qemu

$ make install

编译Linux内核

下载Linux kernel源码

$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.13.8.tar.xz

解压Linux kernel源码tar包

$ tar xvf linux-5.13.8.tar.xz

配置Linux kernel

$ make ARCH=arm64 CROSS_COMPILE=aarch64-none-linux-gnu- defconfig

编译Linux kernel

$ make ARCH=arm64 CROSS_COMPILE=aarch64-none-linux-gnu- Image -j8

制作根文件系统

基于busybox制作根文件系统

下载busybox源码

$ wget https://busybox.net/downloads/busybox-1.33.1.tar.bz2

解压busybox源码tar包

$ tar jxvf busybox-1.33.1.tar.bz2

配置busybox

$ make ARCH=arm64 CROSS_COMPILE=aarch64-none-linux-gnu- menuconfig

其中如下配置项要选上
QEMU仿真aarch64处理器运行Linux kernel_第1张图片

编译busybox源码

$ make ARCH=arm64 CROSS_COMPILE=aarch64-none-linux-gnu- 

安装busybox

$ make ARCH=arm64 CROSS_COMPILE=aarch64-none-linux-gnu- install

执行如下脚本内容,制作基于编译的busybox的根文件系统

rm -rfv rootfs
mkdir rootfs
#mkdir -p rootfs/lib
#copy dynamic library for rootfs, because we compile the busybox dynamically 
#cp -pvf /usr/aarch64-linux-gnu/lib/* rootfs/lib
#cp -rpvf /opt/arm64/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/lib/* rootfs/lib
cp -rfv busybox-1.33.1/_install/* rootfs/

cd rootfs
mkdir dev etc proc

cd dev
#create the communication console
sudo mknod console c 5 1
sudo mknod null c 1 3
sudo mknod tty1 c 4 1
sudo mknod tty2 c 4 2
sudo mknod tty3 c 4 3
sudo mknod tty4 c 4 4

cd ../etc
echo "::sysinit:/etc/init.d/rcS" > inittab
echo "::respawn:-/bin/sh" >> inittab
echo "::askfirst:-/bin/sh" >> inittab
echo "::ctrlaltdel:/bin/umount -a -r " >> inittab
mkdir init.d

cd init.d
echo "#!/bin/sh" > rcS 
echo "mount -t proc proc /proc" >> rcS
chmod +x ./rcS
cd ../..
#find . | cpio -o -H newc > rootfs.cpio
#gzip -c rootfs.cpio > ../rootfs.cpio.gz
find . | cpio -o -H newc | gzip -c > ../rootfs.cpio.gz

基于QEMU运行Linux kernel

执行如下命令运行Linux kernel

qemu-system-aarch64 \
        -machine virt,virtualization=true,gic-version=3 \
        -nographic \
        -m size=1024M \
        -cpu cortex-a72 \
        -smp 4 \
        -kernel linux-5.13.8/arch/arm64/boot/Image \
        -initrd busybox-1.33.1/rootfs.cpio.gz \
        --append "console=ttyAMA0 rdinit=/linuxrc"

执行结果如下

[    0.000000] Booting Linux on physical CPU 0x0000000000 [0x410fd083]
[    0.000000] Linux version 5.13.8 (wxer@wxer-PC) (aarch64-none-linux-gnu-gcc (GNU Toolchain for the A-profile Architecture 10.2-2020.11 (arm-10.16)) 10.2.1 20201103, GNU ld (GNU Toolchain for the A-profile Architecture 10.2-2020.11 (arm-10.16)) 2.35.1.20201028) #1 SMP PREEMPT Sat Aug 7 19:05:03 CST 2021
[    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-0x000000007fffffff]
[    0.000000] NUMA: NODE_DATA [mem 0x7fdf4c00-0x7fdf6fff]
[    0.000000] Zone ranges:
[    0.000000]   DMA      [mem 0x0000000040000000-0x000000007fffffff]
[    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-0x000000007fffffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000040000000-0x000000007fffffff]
[    0.000000] cma: Reserved 32 MiB at 0x000000007cc00000
[    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 23 pages/cpu s56920 r8192 d29096 u94208
[    0.000000] Detected PIPT I-cache on CPU0
[    0.000000] CPU features: detected: GIC system register CPU interface
[    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: kernel page table isolation forced ON by KASLR
[    0.000000] CPU features: detected: Kernel page table isolation (KPTI)
[    0.000000] CPU features: detected: ARM errata 1165522, 1319367, or 1530923
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 258048
[    0.000000] Policy zone: DMA
[    0.000000] Kernel command line: console=ttyAMA0 rdinit=/linuxrc
[    0.000000] Dentry cache hash table entries: 131072 (order: 8, 1048576 bytes, linear)
[    0.000000] Inode-cache hash table entries: 65536 (order: 7, 524288 bytes, linear)
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] Memory: 960556K/1048576K available (15040K kernel code, 3038K rwdata, 7972K rodata, 6080K init, 500K bss, 55252K reserved, 32768K cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, 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=4.
[    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=4
[    0.000000] NR_IRQS: 64, nr_irqs: 64, preallocated irqs: 0
[    0.000000] GICv3: GIC: Using split EOI/Deactivate mode
[    0.000000] GICv3: 224 SPIs implemented
[    0.000000] GICv3: 0 Extended SPIs implemented
[    0.000000] GICv3: Distributor has no Range Selector support
[    0.000000] Root IRQ handler: gic_handle_irq
[    0.000000] GICv3: 16 PPIs implemented
[    0.000000] GICv3: CPU0: found redistributor 0 region 0:0x00000000080a0000
[    0.000000] random: get_random_bytes called from start_kernel+0x33c/0x528 with crng_init=0
[    0.000000] arch_timer: cp15 timer(s) running at 62.50MHz (phys).
[    0.000000] clocksource: arch_sys_counter: mask: 0xffffffffffffff max_cycles: 0x1cd42e208c, max_idle_ns: 881590405314 ns
[    0.000450] sched_clock: 56 bits at 62MHz, resolution 16ns, wraps every 4398046511096ns
[    0.040101] Console: colour dummy device 80x25
[    0.056038] Calibrating delay loop (skipped), value calculated using timer frequency.. 125.00 BogoMIPS (lpj=250000)
[    0.056766] pid_max: default: 32768 minimum: 301
[    0.064225] LSM: Security Framework initializing
[    0.070758] Mount-cache hash table entries: 2048 (order: 2, 16384 bytes, linear)
[    0.071044] Mountpoint-cache hash table entries: 2048 (order: 2, 16384 bytes, linear)
[    0.282766] rcu: Hierarchical SRCU implementation.
[    0.327626] EFI services will not be available.
[    0.334913] smp: Bringing up secondary CPUs ...
[    0.351930] Detected PIPT I-cache on CPU1
[    0.355194] GICv3: CPU1: found redistributor 1 region 0:0x00000000080c0000
[    0.356265] CPU1: Booted secondary processor 0x0000000001 [0x410fd083]
[    0.379016] Detected PIPT I-cache on CPU2
[    0.379426] GICv3: CPU2: found redistributor 2 region 0:0x00000000080e0000
[    0.379955] CPU2: Booted secondary processor 0x0000000002 [0x410fd083]
[    0.390380] Detected PIPT I-cache on CPU3
[    0.390798] GICv3: CPU3: found redistributor 3 region 0:0x0000000008100000
[    0.391325] CPU3: Booted secondary processor 0x0000000003 [0x410fd083]
[    0.394859] smp: Brought up 1 node, 4 CPUs
[    0.395376] SMP: Total of 4 processors activated.
[    0.395649] CPU features: detected: 32-bit EL0 Support
[    0.395761] CPU features: detected: 32-bit EL1 Support
[    0.396216] CPU features: detected: CRC32 instructions
[    0.683570] CPU: All CPU(s) started at EL2
[    0.686043] alternatives: patching kernel code
[    0.777761] devtmpfs: initialized
[    0.852671] KASLR enabled
[    0.860514] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 7645041785100000 ns
[    0.861342] futex hash table entries: 1024 (order: 4, 65536 bytes, linear)
[    0.891588] pinctrl core: initialized pinctrl subsystem
[    0.961147] DMI not present or invalid.
[    1.011108] NET: Registered protocol family 16
[    1.090254] DMA: preallocated 128 KiB GFP_KERNEL pool for atomic allocations
[    1.094684] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA pool for atomic allocations
[    1.097778] DMA: preallocated 128 KiB GFP_KERNEL|GFP_DMA32 pool for atomic allocations
[    1.099477] audit: initializing netlink subsys (disabled)
[    1.110092] audit: type=2000 audit(1.000:1): state=initialized audit_enabled=0 res=1
[    1.144168] thermal_sys: Registered thermal governor 'step_wise'
[    1.144487] thermal_sys: Registered thermal governor 'power_allocator'
[    1.149150] cpuidle: using governor menu
[    1.155518] hw-breakpoint: found 6 breakpoint and 4 watchpoint registers.
[    1.159551] ASID allocator initialised with 32768 entries
[    1.199593] Serial: AMBA PL011 UART driver
[    1.557598] 9000000.pl011: ttyAMA0 at MMIO 0x9000000 (irq = 48, base_baud = 0) is a PL011 rev1
[    1.603511] printk: console [ttyAMA0] enabled
[    1.881030] HugeTLB registered 1.00 GiB page size, pre-allocated 0 pages
[    1.881755] HugeTLB registered 32.0 MiB page size, pre-allocated 0 pages
[    1.882482] HugeTLB registered 2.00 MiB page size, pre-allocated 0 pages
[    1.883002] HugeTLB registered 64.0 KiB page size, pre-allocated 0 pages
[    1.941526] cryptd: max_cpu_qlen set to 1000
[    2.010906] ACPI: Interpreter disabled.
[    2.059036] iommu: Default domain type: Translated 
[    2.065907] vgaarb: loaded
[    2.076064] SCSI subsystem initialized
[    2.090593] usbcore: registered new interface driver usbfs
[    2.093097] usbcore: registered new interface driver hub
[    2.094479] usbcore: registered new device driver usb
[    2.114261] pps_core: LinuxPPS API ver. 1 registered
[    2.114812] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <[email protected]>
[    2.115926] PTP clock support registered
[    2.120032] EDAC MC: Ver: 3.0.0
[    2.158848] FPGA manager framework
[    2.164359] Advanced Linux Sound Architecture Driver Initialized.
[    2.236227] clocksource: Switched to clocksource arch_sys_counter
[    2.252339] VFS: Disk quotas dquot_6.6.0
[    2.253868] VFS: Dquot-cache hash table entries: 512 (order 0, 4096 bytes)
[    2.264071] pnp: PnP ACPI: disabled
[    2.412967] NET: Registered protocol family 2
[    2.420270] IP idents hash table entries: 16384 (order: 5, 131072 bytes, linear)
[    2.449357] tcp_listen_portaddr_hash hash table entries: 512 (order: 1, 8192 bytes, linear)
[    2.450495] TCP established hash table entries: 8192 (order: 4, 65536 bytes, linear)
[    2.452174] TCP bind hash table entries: 8192 (order: 5, 131072 bytes, linear)
[    2.453413] TCP: Hash tables configured (established 8192 bind 8192)
[    2.463177] UDP hash table entries: 512 (order: 2, 16384 bytes, linear)
[    2.464775] UDP-Lite hash table entries: 512 (order: 2, 16384 bytes, linear)
[    2.476331] NET: Registered protocol family 1
[    2.494090] RPC: Registered named UNIX socket transport module.
[    2.495148] RPC: Registered udp transport module.
[    2.495635] RPC: Registered tcp transport module.
[    2.496014] RPC: Registered tcp NFSv4.1 backchannel transport module.
[    2.496994] PCI: CLS 0 bytes, default 64
[    2.522399] Unpacking initramfs...
[    2.528429] hw perfevents: enabled with armv8_pmuv3 PMU driver, 5 counters available
[    2.533230] kvm [1]: IPA Size Limit: 44 bits
[    2.576787] kvm [1]: GICv3: no GICV resource entry
[    2.578298] kvm [1]: disabling GICv2 emulation
[    2.579854] kvm [1]: GIC system register CPU interface enabled
[    2.583364] kvm [1]: vgic interrupt IRQ9
[    2.628611] kvm [1]: Hyp mode initialized successfully
[    2.723999] Initialise system trusted keyrings
[    2.737268] workingset: timestamp_bits=42 max_order=18 bucket_order=0
[    2.879685] squashfs: version 4.0 (2009/01/31) Phillip Lougher
[    2.898165] NFS: Registering the id_resolver key type
[    2.900842] Key type id_resolver registered
[    2.901427] Key type id_legacy registered
[    2.905210] nfs4filelayout_init: NFSv4 File Layout Driver Registering...
[    2.906105] nfs4flexfilelayout_init: NFSv4 Flexfile Layout Driver Registering...
[    2.911816] 9p: Installing v9fs 9p2000 file system support
[    2.967171] Freeing initrd memory: 1080K
[    2.972997] Key type asymmetric registered
[    2.973858] Asymmetric key parser 'x509' registered
[    2.975877] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 245)
[    2.976966] io scheduler mq-deadline registered
[    2.977710] io scheduler kyber registered
[    3.167082] pl061_gpio 9030000.pl061: PL061 GPIO chip registered
[    3.213453] pci-host-generic 4010000000.pcie: host bridge /pcie@10000000 ranges:
[    3.219095] pci-host-generic 4010000000.pcie:       IO 0x003eff0000..0x003effffff -> 0x0000000000
[    3.222607] pci-host-generic 4010000000.pcie:      MEM 0x0010000000..0x003efeffff -> 0x0010000000
[    3.223755] pci-host-generic 4010000000.pcie:      MEM 0x8000000000..0xffffffffff -> 0x8000000000
[    3.227547] pci-host-generic 4010000000.pcie: Memory resource size exceeds max for 32 bits
[    3.231149] pci-host-generic 4010000000.pcie: ECAM at [mem 0x4010000000-0x401fffffff] for [bus 00-ff]
[    3.240557] pci-host-generic 4010000000.pcie: PCI host bridge to bus 0000:00
[    3.242029] pci_bus 0000:00: root bus resource [bus 00-ff]
[    3.243096] pci_bus 0000:00: root bus resource [io  0x0000-0xffff]
[    3.243908] pci_bus 0000:00: root bus resource [mem 0x10000000-0x3efeffff]
[    3.244507] pci_bus 0000:00: root bus resource [mem 0x8000000000-0xffffffffff]
[    3.253104] pci 0000:00:00.0: [1b36:0008] type 00 class 0x060000
[    3.277592] pci 0000:00:01.0: [1af4:1000] type 00 class 0x020000
[    3.279678] pci 0000:00:01.0: reg 0x10: [io  0x0000-0x001f]
[    3.280721] pci 0000:00:01.0: reg 0x20: [mem 0x00000000-0x00003fff 64bit pref]
[    3.281809] pci 0000:00:01.0: reg 0x30: [mem 0x00000000-0x0003ffff pref]
[    3.304458] pci 0000:00:01.0: BAR 6: assigned [mem 0x10000000-0x1003ffff pref]
[    3.306907] pci 0000:00:01.0: BAR 4: assigned [mem 0x8000000000-0x8000003fff 64bit pref]
[    3.308308] pci 0000:00:01.0: BAR 0: assigned [io  0x1000-0x101f]
[    3.352299] EINJ: ACPI disabled.
[    3.615108] virtio-pci 0000:00:01.0: enabling device (0000 -> 0003)
[    3.744242] Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled
[    3.795459] SuperH (H)SCI(F) driver initialized
[    3.808420] msm_serial: driver initialized
[    3.842400] cacheinfo: Unable to detect cache hierarchy for CPU 0
[    4.033600] loop: module loaded
[    4.061159] megasas: 07.714.04.00-rc1
[    4.118496] physmap-flash 0.flash: physmap platform flash device: [mem 0x00000000-0x03ffffff]
[    4.127325] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[    4.130464] Intel/Sharp Extended Query Table at 0x0031
[    4.133759] Using buffer write method
[    4.137647] physmap-flash 0.flash: physmap platform flash device: [mem 0x04000000-0x07ffffff]
[    4.140287] 0.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[    4.141206] Intel/Sharp Extended Query Table at 0x0031
[    4.143360] Using buffer write method
[    4.144379] Concatenating MTD devices:
[    4.144799] (0): "0.flash"
[    4.145109] (1): "0.flash"
[    4.145409] into device "0.flash"
[    4.591976] libphy: Fixed MDIO Bus: probed
[    4.623425] tun: Universal TUN/TAP device driver, 1.6
[    4.690063] thunder_xcv, ver 1.0
[    4.691648] thunder_bgx, ver 1.0
[    4.692721] nicpf, ver 1.0
[    4.735190] hclge is initializing
[    4.736705] hns3: Hisilicon Ethernet Network Driver for Hip08 Family - version
[    4.737319] hns3: Copyright (c) 2017 Huawei Corporation.
[    4.740213] e1000: Intel(R) PRO/1000 Network Driver
[    4.740713] e1000: Copyright (c) 1999-2006 Intel Corporation.
[    4.742254] e1000e: Intel(R) PRO/1000 Network Driver
[    4.743079] e1000e: Copyright(c) 1999 - 2015 Intel Corporation.
[    4.744535] igb: Intel(R) Gigabit Ethernet Network Driver
[    4.745018] igb: Copyright (c) 2007-2014 Intel Corporation.
[    4.746247] igbvf: Intel(R) Gigabit Virtual Function Network Driver
[    4.747065] igbvf: Copyright (c) 2009 - 2012 Intel Corporation.
[    4.756605] sky2: driver version 1.30
[    4.783907] VFIO - User Level meta-driver version: 0.3
[    4.832060] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[    4.833267] ehci-pci: EHCI PCI platform driver
[    4.835212] ehci-platform: EHCI generic platform driver
[    4.838338] ehci-orion: EHCI orion driver
[    4.841160] ehci-exynos: EHCI Exynos driver
[    4.844136] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
[    4.845485] ohci-pci: OHCI PCI platform driver
[    4.847509] ohci-platform: OHCI generic platform driver
[    4.850525] ohci-exynos: OHCI Exynos driver
[    4.867311] usbcore: registered new interface driver usb-storage
[    4.949074] rtc-pl031 9010000.pl031: registered as rtc0
[    4.953123] rtc-pl031 9010000.pl031: setting system clock to 2021-08-07T12:16:59 UTC (1628338619)
[    4.976246] i2c /dev entries driver
[    5.117690] sdhci: Secure Digital Host Controller Interface driver
[    5.118287] sdhci: Copyright(c) Pierre Ossman
[    5.131369] Synopsys Designware Multimedia Card Interface Driver
[    5.156285] sdhci-pltfm: SDHCI platform and OF driver helper
[    5.197590] ledtrig-cpu: registered to indicate activity on CPUs
[    5.232996] usbcore: registered new interface driver usbhid
[    5.233637] usbhid: USB HID core driver
[    5.360546] NET: Registered protocol family 17
[    5.369138] 9pnet: Installing 9P2000 support
[    5.371499] Key type dns_resolver registered
[    5.377380] registered taskstats version 1
[    5.378045] Loading compiled-in X.509 certificates
[    5.436756] input: gpio-keys as /devices/platform/gpio-keys/input/input0
[    5.466318] ALSA device list:
[    5.467529]   No soundcards found.
[    5.488315] uart-pl011 9000000.pl011: no DMA platform data
[    5.636826] Freeing unused kernel memory: 6080K
[    5.642169] Run /linuxrc as init process

Please press Enter to activate this console. 
/ # ls -al
total 4
drwxrwxr-x    9 1000     1000           220 Aug  7 12:17 .
drwxrwxr-x    9 1000     1000           220 Aug  7 12:17 ..
-rw-------    1 0        0                7 Aug  7 12:17 .ash_history
drwxrwxr-x    2 1000     1000          1920 Aug  7 11:43 bin
drwxrwxr-x    2 1000     1000           160 Aug  7 11:43 dev
drwxrwxr-x    3 1000     1000            80 Aug  7 11:43 etc
lrwxrwxrwx    1 1000     1000            11 Aug  7 11:43 linuxrc -> bin/busybox
dr-xr-xr-x  116 0        0                0 Aug  7 12:17 proc
drwx------    2 0        0               40 Aug  7 11:05 root
drwxrwxr-x    2 1000     1000          1480 Aug  7 11:43 sbin
drwxrwxr-x    4 1000     1000            80 Aug  7 11:43 usr
/ # 

你可能感兴趣的:(Linux,Kernel)