zoukankan      html  css  js  c++  java
  • 基于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的步骤大同小异,这里就不再赘述了

  • 相关阅读:
    pytest文档55-plugins插件开发
    pytest文档54-Hooks函数terminal打印测试结果(pytest_report_teststatus)
    Linux内存占用过高排查过程
    系统日志:/var/log/messages
    win10 安装oracle 11gR2_database(内附下载地址)
    CentOS 7 下 Docker 的离线安装方法
    docker 常用命令
    【docker】如何将服务器加入集群,成为子节点
    Docker管理面板系列——Portainer
    基于Docker的Consul服务发现集群搭建
  • 原文地址:https://www.cnblogs.com/redstar9451/p/9755415.html
Copyright © 2011-2022 走看看