qemu-system启动linux内核

6 篇文章 1 订阅

0. 平台

 cat /proc/version
Linux version 4.4.0-210-generic (buildd@lgw01-amd64-009) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) ) #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021

1. 安装qemu模拟器

sudo apt-get install -y qemu-system

2. 内核文件

内核配置需要设置:CONFIG_BLK_DEV_RAM=y
编译内核后有个/arch/arm/boot/zImage(这里举例,实际看SDK的路径)

file zImage
zImage: Linux kernel ARM boot executable zImage (little-endian)

具体看编译的内核版本,这里是stm32mp157的zImage

3. 文件系统

init程序

#include <stdio.h>
#include <unistd.h>

int main(int argc,char *argv[])
{
        printf("this is the init program !\n");
        while(1){
                sleep(1);
        }

        return 0;
}

编译init

arm-none-linux-gnueabihf-gcc -o init init.c -static
chmod a+x init

make_initramfs.sh

#! /bin/sh

if [ $# -ne 2 ]
then
    echo "usage: make_initramfs directory imagename.cpio.gz"
    exit 1
fi

if [ -d "$1" ]
then
    echo "creating $2 from $1"
    (cd "$1"; find . | cpio -o -H newc | gzip) > "$2"
else
    echo "First argument must be a directory"
    exit 1
fi

制作最小文件系统

不一定要制作最小文件系统,可以使用其他SDK自带的文件系统或自己做的文件系统,
这里仅为测试模拟器启动内核使用。

mkdir rootfs
cp init rootfs
chmod a+x make_initramfs.sh
./make_initramfs.sh rootfs initramfs.cpio.gz

输出:

./make_initramfs.sh rootfs initramfs.cpio.gz
creating initramfs.cpio.gz from rootfs
8004

4. 启动脚本launch.sh

#! /bin/bash

qemu-system-arm \
-machine virt \
-nographic \
-smp 4 \
-m 2048 \
-kernel zImage \
-append "root=/dev/ram0 rootfstype=ramfs rw init=/init" \
-initrd initramfs.cpio.gz

qemu-system-arm是32位的模拟器,qemu-system-aarch64是64位的模拟器

5. 启动

chmod a+x launch.sh
./launch.sh

现象:

 ./launch.sh
[    0.000000] Booting Linux on physical CPU 0x0
[    0.000000] Linux version 5.4.31-g15a9baf-dirty (z@z-server) (gcc version 9.2.1 20191025 (GNU Toolchain for the A-profile Architecture 9.2-2019.12 (arm-9.10))) #17 SMP PREEMPT Tue Feb 28 23:07:01 CST 2023
[    0.000000] CPU: ARMv7 Processor [412fc0f1] revision 1 (ARMv7), cr=10c5387d
[    0.000000] CPU: div instructions available: patching division code
[    0.000000] CPU: PIPT / VIPT nonaliasing data cache, PIPT instruction cache
[    0.000000] OF: fdt: Machine model: linux,dummy-virt
[    0.000000] Memory policy: Data cache writealloc
[    0.000000] cma: Reserved 128 MiB at 0xb8000000
[    0.000000] ------------[ cut here ]------------
[    0.000000] WARNING: CPU: 0 PID: 0 at arch/arm/kernel/devtree.c:148 arm_dt_init_cpu_maps+0x1c0/0x2b8
[    0.000000] DT /cpu 3 nodes greater than max cores 2, capping them
[    0.000000] Modules linked in:
[    0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 5.4.31-g15a9baf-dirty #17
[    0.000000] Hardware name: Generic DT based system
[    0.000000] [<c0112518>] (unwind_backtrace) from [<c010d7a0>] (show_stack+0x10/0x14)
[    0.000000] [<c010d7a0>] (show_stack) from [<c0caffd4>] (dump_stack+0xb0/0xc4)
[    0.000000] [<c0caffd4>] (dump_stack) from [<c0125a8c>] (__warn+0xd0/0xf8)
[    0.000000] [<c0125a8c>] (__warn) from [<c0125e68>] (warn_slowpath_fmt+0x98/0xc4)
[    0.000000] [<c0125e68>] (warn_slowpath_fmt) from [<c11053e0>] (arm_dt_init_cpu_maps+0x1c0/0x2b8)
[    0.000000] [<c11053e0>] (arm_dt_init_cpu_maps) from [<c1104a18>] (setup_arch+0x398/0x5f4)
[    0.000000] [<c1104a18>] (setup_arch) from [<c1100ac4>] (start_kernel+0x70/0x4f4)
[    0.000000] [<c1100ac4>] (start_kernel) from [<00000000>] (0x0)
[    0.000000] random: get_random_bytes called from print_oops_end_marker+0x24/0x4c with crng_init=0
[    0.000000] ---[ end trace 0000000000000000 ]---
[    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 s50124 r8192 d23604 u81920
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 522560
[    0.000000] Kernel command line: root=/dev/ram0 rootfstype=ramfs rw init=/init
[    0.000000] Dentry cache hash table entries: 131072 (order: 7, 524288 bytes, linear)
[    0.000000] Inode-cache hash table entries: 65536 (order: 6, 262144 bytes, linear)
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] Memory: 1926280K/2097152K available (12288K kernel code, 991K rwdata, 3720K rodata, 1024K init, 230K bss, 39800K reserved, 131072K cma-reserved, 1179648K highmem)
[    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]  Tasks RCU enabled.
[    0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 10 jiffies.
[    0.000000] NR_IRQS: 16, nr_irqs: 16, preallocated irqs: 16
[    0.000000] arch_timer: WARNING: Invalid trigger for IRQ18, assuming level low
[    0.000000] arch_timer: WARNING: Please fix your firmware
[    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.000115] sched_clock: 56 bits at 62MHz, resolution 16ns, wraps every 4398046511096ns
[    0.000246] Switching to timer-based delay loop, resolution 16ns
[    0.002956] Console: colour dummy device 80x30
[    0.005429] printk: console [tty0] enabled
[    0.006322] Calibrating delay loop (skipped), value calculated using timer frequency.. 125.00 BogoMIPS (lpj=625000)
[    0.006510] pid_max: default: 32768 minimum: 301
[    0.007511] Mount-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
[    0.007628] Mountpoint-cache hash table entries: 2048 (order: 1, 8192 bytes, linear)
[    0.022366] CPU: Testing write buffer coherency: ok
[    0.023191] CPU0: Spectre v2: firmware did not set auxiliary control register IBE bit, system vulnerable
[    0.030746] /cpus/cpu@0 missing clock-frequency property
[    0.030894] /cpus/cpu@1 missing clock-frequency property
[    0.031946] CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
[    0.094502] Setting up static identity map for 0x40100000 - 0x40100060
[    0.114051] rcu: Hierarchical SRCU implementation.
[    0.165573] smp: Bringing up secondary CPUs ...
[    0.228010] CPU1: thread -1, cpu 1, socket 0, mpidr 80000001
[    0.228063] CPU1: Spectre v2: firmware did not set auxiliary control register IBE bit, system vulnerable
[    0.230552] smp: Brought up 1 node, 2 CPUs
[    0.230654] SMP: Total of 2 processors activated (250.00 BogoMIPS).
[    0.230730] CPU: All CPU(s) started in SVC mode.
[    0.241460] devtmpfs: initialized
[    0.264139] VFP support v0.3: implementor 41 architecture 4 part 30 variant f rev 0
[    0.282654] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
[    0.283102] futex hash table entries: 512 (order: 3, 32768 bytes, linear)
[    0.293780] pinctrl core: initialized pinctrl subsystem
[    0.313256] NET: Registered protocol family 16
[    0.340541] DMA: preallocated 256 KiB pool for atomic coherent allocations
[    0.345046] hw-breakpoint: CPU 0 debug is powered down!
[    0.346788] Serial: AMBA PL011 UART driver
[    0.371666] 9000000.pl011: ttyAMA0 at MMIO 0x9000000 (irq = 53, base_baud = 0) is a PL011 rev1
[    0.389310] printk: console [ttyAMA0] enabled
[    0.397314] irq: type mismatch, failed to map hwirq-27 for intc!
[    0.524816] SCSI subsystem initialized
......
[    9.838098] rtc-pl031 9010000.pl031: setting system clock to 2023-03-01T13:39:38 UTC (1677677978)
[    9.839363] cfg80211: Loading compiled-in X.509 certificates for regulatory database
[    9.843574] cfg80211: Loaded X.509 cert 'sforshee: 00b28ddf47aef9cea7'
[    9.845237] platform regulatory.0: Direct firmware load for regulatory.db failed with error -2
[    9.845726] cfg80211: failed to load regulatory.db
[    9.846588] ALSA device list:
[    9.846798]   No soundcards found.
[    9.849699] uart-pl011 9000000.pl011: no DMA platform data
[   10.040005] Freeing unused kernel memory: 1024K
[   10.135443] Run /init as init process
this is the init program !

qemu-system-arm参数

-machine

 qemu-system-arm -machine help
Supported machines are:
akita                Sharp SL-C1000 (Akita) PDA (PXA270)
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)
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
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                 ARM Virtual Machine
xilinx-zynq-a9       Xilinx Zynq Platform Baseboard for Cortex-A9
z2                   Zipit Z2 (PXA27x)

-smp

qemu-system-arm -smp help
qemu-system-arm: -smp help: Parameter 'cpus' expects a number

-m

qemu-system-arm -m help
qemu-system-arm: -m help: Parameter 'size' expects a size
You may use k, M, G or T suffixes for kilobytes, megabytes, gigabytes and terabytes.

-kernel

 -kernel bzImage
           Use bzImage as kernel image. The kernel can be either a Linux kernel or in multiboot format.

-initrd

 -initrd file
           Use file as initial ram disk.

-append

-append cmdline
           Use cmdline as kernel command line

-S -s

 -S  Do not start CPU at startup (you must type 'c' in the monitor).
 -s  Shorthand for -gdb tcp::1234, i.e. open a gdbserver on TCP port 1234.

更多参数查看

man qemu-system-arm 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QEmu虚拟机 做嵌入式开发的朋友试试 调试内核很方便 QEMU emulator version 0.13.0, Copyright (c) 2003-2008 Fabrice Bellard usage: qemu [options] [disk_image] 'disk_image' is a raw hard disk image for IDE hard disk 0 Standard options: -h or -help display this help and exit -version display version information and exit -M machine select emulated machine (-M ? for list) -cpu cpu select CPU (-cpu ? for list) -smp n[,maxcpus=cpus][,cores=cores][,threads=threads][,sockets=sockets] set the number of CPUs to 'n' [default=1] maxcpus= maximum number of total cpus, including offline CPUs for hotplug, etc cores= number of CPU cores on one socket threads= number of threads on one CPU core sockets= number of discrete sockets in the system -numa node[,mem=size][,cpus=cpu[-cpu]][,nodeid=node] -fda/-fdb file use 'file' as floppy disk 0/1 image -hda/-hdb file use 'file' as IDE hard disk 0/1 image -hdc/-hdd file use 'file' as IDE hard disk 2/3 image -cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master) -drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i] [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off] [,cache=writethrough|writeback|none|unsafe][,format=f] [,serial=s][,addr=A][,id=name][,aio=threads|native] [,readonly=on|off] use 'file' as a drive image -set group.id.arg=value set parameter for item of type i.e. -set drive.$id.file=/path/to/image -global driver.property=value set a global default for a driver property -mtdblock file use 'file' as on-board Flash memory image -sd file use 'file' as SecureDigital card image -pflash file use 'file' as a parallel flash image -boot [order=drives][,once=drives][,menu=on|off] 'drives': floppy (a), hard disk (c), CD-ROM (d), network (n) -snapshot write to temporary files

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值