Qemu和Gem5两项仿真工具的编译安装

本文中我们会介绍如何准备Qemu和Gem5的环境并进行安装。

Qemu安装

这里我们使用的是8.2.10版本,对应的API是v1,选择插件版本需要留意

环境依赖

首先需要安装必须的包

sudo apt-get install git libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev ninja-build

包名是Debian的(括号内是Red Hat/Fedora)。 git-email,用于发送补丁 libsdl2-dev(libsdl2-devel),用于基于SDL的图形用户界面 libgtk-3-dev(gtk3-devel),用于一个简单的界面,代替VNC libvte-dev(VTE291-DEVEL),通过GTK接口访问QEMU显示器和串口/控制台设备 libcapstone-dev(capstone-devel),用于拆解 CPU 指令 上述列表远非完整。为了最大化代码覆盖,应尽可能启用 QEMU 功能。 运行配置时,你应该会看到很多“是”的行,只有少数几行是“否”。

对于Ubuntu(以及可能的其他基于Debian的发行版), 大多数推荐的额外软件包以实现最大代码覆盖,可以安装如下:

sudo apt-get install git-email
sudo apt-get install libaio-dev libbluetooth-dev libcapstone-dev libbrlapi-dev libbz2-dev
sudo apt-get install libcap-ng-dev libcurl4-gnutls-dev libgtk-3-dev
sudo apt-get install libibverbs-dev libjpeg8-dev libncurses5-dev libnuma-dev
sudo apt-get install librbd-dev librdmacm-dev
sudo apt-get install libsasl2-dev libsdl2-dev libseccomp-dev libsnappy-dev libssh-dev
sudo apt-get install libvde-dev libvdeplug-dev libvte-2.91-dev libxen-dev liblzo2-dev
sudo apt-get install valgrind xfslibs-dev 

然后执行

make build && cd build
../configure --prefix=/usr/local/qemu --enable-slirp
make
sudo make install
sudo ln -s /usr/local/qemu/bin/* /usr/local/bin/

问题解决

遭遇问题:

Executing subproject keycodemapdb 

keycodemapdb| Project name: keycodemapdb
keycodemapdb| Project version: undefined
keycodemapdb| Program tools/keymap-gen found: YES (/home/volcanobin/Downloads/qemu-stable-8.2/subprojects/keycodemapdb/tools/keymap-gen)
keycodemapdb| Build targets in project: 275
keycodemapdb| Subproject keycodemapdb finished.

Program scripts/decodetree.py found: YES (/home/volcanobin/Downloads/qemu-stable-8.2/build/pyvenv/bin/python3 /home/volcanobin/Downloads/qemu-stable-8.2/scripts/decodetree.py)
Program flex found: NO

../target/hexagon/meson.build:180:8: ERROR: Program 'flex' not found or not executable

A full log can be found at /home/volcanobin/Downloads/qemu-stable-8.2/build/meson-logs/meson-log.txt

ERROR: meson setup failed

解决:

sudo apt install -y flex bison

gem5 安装

准备

环境依赖安装

sudo apt install build-essential git m4 scons zlib1g zlib1g-dev \
    libprotobuf-dev protobuf-compiler libprotoc-dev libgoogle-perftools-dev \
    python3-dev python-is-python3 libboost-all-dev pkg-config gcc-10 g++-10 \
    python3-tk clang-format-18

从github上下载gem5

git clone https://github.com/gem5/gem5.git

在v23+版本开始,就放弃了se.py个fs.py,改为了使用配置文件的方法,如果希望使用旧版方法请下载22.1以前的版本。我这里使用v22.1.0.0

编译

执行

scons build/{ISA}/gem5.{variant} -j {cpus}

有效的ISA有:

ALL - 包含了gem5起的所有指令集和Ruby协议

  • ARM
  • NULL
  • MIPS
  • POWER
  • RISCV
  • SPARC
  • X86

有效的构建变体有

  • debug 关闭了优化。这确保变量不会 优化后,功能不会意外地内联化,控制流程也会 不要以令人惊讶的方式行事。这让这个版本更容易作 类似GDB的工具,但没有优化,这个版本明显慢很多 比其他人更重要。使用像 gdb 和 valgrind 这样的工具时,应该选择它 而且不希望细节被掩盖,但除此之外,优化更好的版本是 推荐。
  • opt 开启了优化功能和调试功能,比如断言 以及留下的 DPRINTF。这在速度之间取得了良好的平衡 模拟和洞察,以防出现问题。就是这样 大多数情况下版本是最好的。
  • fast 已开启优化功能并编译调试功能 出去。这在性能上发挥了所有作用,但代价是 运行时错误检查以及启用调试输出的能力。就是这样 如果你非常确定一切正常,推荐使用这个版本 并且想从模拟器中获得最佳性能。

编译后的help:

Usage
=====
  gem5.opt [gem5 options] script.py [script options]

gem5 is copyrighted software; use the --copyright option for details.

Options
=======
--help, -h              show this help message and exit
--build-info, -B        Show build information
--copyright, -C         Show full copyright information
--readme, -R            Show the readme
--outdir=DIR, -d DIR    Set the output directory to DIR [Default: m5out]
--redirect-stdout, -r   Redirect stdout (& stderr, without -e) to file
--redirect-stderr, -e   Redirect stderr to file
--silent-redirect       Suppress printing a message when redirecting stdout or
                        stderr
--stdout-file=FILE      Filename for -r redirection [Default: simout.txt]
--stderr-file=FILE      Filename for -e redirection [Default: simerr.txt]
--listener-mode={on,off,auto}
                        Port (e.g., gdb) listener mode (auto: Enable if
                        running interactively) [Default: auto]
--allow-remote-connections
                        Port listeners will accept connections from anywhere
                        (0.0.0.0). Default is only localhost.
--interactive, -i       Invoke the interactive interpreter after running the
                        script
--pdb                   Invoke the python debugger before running the script
--path=PATH[:PATH], -p PATH[:PATH]
                        Prepend PATH to the system path when invoking the
                        script
--quiet, -q             Reduce verbosity
--verbose, -v           Increase verbosity
-m mod                  run library module as a script (terminates option
                        list)
-c cmd                  program passed in as string (terminates option list)
-P                      Don't prepend the script directory to the system path.
                        Mimics Python 3's `-P` option.
-s                      IGNORED, only for compatibility with python. don'tadd
                        user site directory to sys.path; also PYTHONNOUSERSITE

Statistics Options
------------------
--stats-file=FILE       Sets the output file for statistics [Default:
                        stats.txt]
--stats-help            Display documentation for available stat visitors

Configuration Options
---------------------
--dump-config=FILE      Dump configuration output file [Default: config.ini]
--json-config=FILE      Create JSON output of the configuration [Default:
                        config.json]
--dot-config=FILE       Create DOT & pdf outputs of the configuration
                        [Default: config.dot]
--dot-dvfs-config=FILE  Create DOT & pdf outputs of the DVFS configuration
                        [Default: none]

Debugging Options
-----------------
--debug-break=TICK[,TICK]
                        Create breakpoint(s) at TICK(s) (kills process if no
                        debugger attached)
--debug-help            Print help on debug flags
--debug-flags=FLAG[,FLAG]
                        Sets the flags for debug output (-FLAG disables a
                        flag)
--debug-start=TICK      Start debug output at TICK
--debug-end=TICK        End debug output at TICK
--debug-file=FILE       Sets the output file for debug. Append '.gz' to the
                        name for it to be compressed automatically [Default:
                        cout]
--debug-activate=EXPR[,EXPR]
                        Activate EXPR sim objects
--debug-ignore=EXPR     Ignore EXPR sim objects
--remote-gdb-port=REMOTE_GDB_PORT
                        Remote gdb base port (set to 0 to disable listening)

Help Options
------------
--list-sim-objects      List all built-in SimObjects, their params and default
                        values

gem5测试

可以使用下面命令行测试:

./build/ARM/gem5.opt configs/example/se.py -c tests/test-progs/hello/bin/arm/linux/hello

这个时候其实还会报警告提示你有pydot等没安装,生成不了一些相关文件,可以使用下面命令补上

sudo apt install -y graphviz
pip3 install pydot

最后修改于 2026-01-28