arrow-left

All pages
gitbookPowered by GitBook
1 of 3

Loading...

Loading...

Loading...

Qemu emulation

Run arm32 UEFI in a virtual machine.

Emulating a arm32 UEFI device is useful for developing Linux and debugging it.

In the GDB Debugging page you can find instructions on how to compile Linux for this virtual machine.

circle-info

A premade ZIP with all required files can be found at the bottom.

hashtag
Compile OVMF for qemu

hashtag
Install required packages

Run the following commands to install the required packages.

You will need other stuff too, but that is probably already installed. (e.g. git)

hashtag
Download source

You need the source code of edk2 and acpica.

hashtag
Compile OVMF

Go to your source directory and run the following commands.

Your output OVMF firmware file for qemu is$WORKSPACE/Build/ArmVirtQemu-ARM/RELEASEGCC5/FV/QEMU_EFI.fd

hashtag
Setup qemu files and run it

Create a directory, where you want your files to be in. Put your QEMU_EFI.fd firmware file in this directory, compiled in the previous section. Now run the following commands to create some disk images:

Now create a directory named boot. This will be your EFI partition. You can now easily place your EFI files in there.

hashtag
Run qemu

To start your virtual machine run the following command, and make sure qemu-system-arm is installed.

This will run qemu with 4 virtual CPU cores. They are Coretx-A15 cores. Used because it works.

hashtag
Premade files

The following ZIP includes all files setup in their proper location. In addition its EFI partition folder has a UEFI shell in it. To run it either execute the run.sh file or enter the command described in .

hashtag
References

Links where the above compiling information is from:

Run qemu
file-archive
2MB
arm32-uefi-qemu-setup.zip
archive
arrow-up-right-from-squareOpen
Premade qemu setup
https://developer.arm.com/tools-and-software/open-source-software/firmware/edkii-uefi-firmware/building-edkii-uefi-firmware-for-arm-platformsarrow-up-right
https://designprincipia.com/virtualize-uefi-on-arm-using-qemu/arrow-up-right
sudo apt install python python3 python3-distutils
sudo apt install gcc-arm-none-eabi
sudo apt install uuid-dev
sudo apt install build-essential
sudo apt install bison
sudo apt install flex
# Go to the directory you want to work in
export WORKSPACE=$PWD

# Download edk2 and acpica
git clone https://github.com/acpica/acpica.git
git clone https://github.com/tianocore/edk2.git

# Download submodules
cd edk2
git submodule update --init
cd ..
# Compile acpica tools
make -C $WORKSPACE/acpica -j$(nproc)

# Set environment variables
export GCC5_ARM_PREFIX=arm-none-eabi-
export IASL_PREFIX=$WORKSPACE/acpica/generate/unix/bin/
export PYTHON_COMMAND=/usr/bin/python3

# Configure the edk2 environment
source edk2/edksetup.sh

# Compile edk2 BaseTools
make -C edk2/BaseTools -j$(nproc)

# Compile OVMF
build -a ARM -t GCC5 -p ArmVirtPkg/ArmVirtQemu.dsc -b RELEASE -j$(nproc)
rm -f flash0.img flash1.img
dd if=/dev/zero bs=1M count=64 of=flash0.img
dd if=/dev/zero bs=1M count=64 of=flash1.img
dd if=QEMU_EFI.fd bs=1M of=flash0.img conv=notrunc
qemu-system-arm \ 
    -m 1024 \
    -cpu cortex-a15 \
    -M virt \
    -pflash flash0.img \
    -pflash flash1.img \
    -nographic \
    -drive \
    file=fat:rw:boot/ \
    -smp '4'

GDB Debugging

Debug Linux kernel with GDB

hashtag
Compile a correct Linux kernel

Clone a Linux tree and run make ARCH=arm defconfig to make a generic kernel configuration suited for qemu. Now edit the kernel configuration (.config) and add the following lines at the bottom:

Now run make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j$(nproc) to compile the kernel. If you get asked about anything, just press enter to use the standard value.

Copy the output zImage (arch/arm/boot/zImage) to efi/boot/bootarm.efi on your EFI partition folder in your qemu directory.

hashtag
Prepare GDB for debugging

hashtag
Install

Run sudo apt-get install gdb-multiarch to install GDB on Ubuntu. gdb-mutliarch is required because normal gdb package doesn't have support for ARM.

hashtag
Run

Open up the terminal you want GDB to run in, and change directory to your Linux compilation directory. Then run gdb-multiarch vmlinux., it will open GDB you and you can now connect to a target with target remote localhost:1234. At this point GDB will wait for qemu to start. After that you can now debug with qemu, there are tutorials online to show you how to do this.

hashtag
Run qemu

Go to the directory where your qemu files are located, start qemu as described in , only change is that you need to add a -s parameter, this lets qemu know that it starts a GDB server.

VSCode integration

Debug Linux kernel within Visual Studio Code

hashtag
Create configuration files

The following steps have to be performed in your Linux source directory.

hashtag
Create tasks.json

Create a file called tasks.json in the directory .vscode and paste the following contents into it:

circle-info

You may want to change line 26 and 34, as they point to the directory where your qemu files are located.

See for the documentation about the tasks.json format

hashtag
Create launch.json

Create a file called launch.json in the directory .vscode and paste the following contents into it: Use IntelliSense to learn about possible attributes. Hover to view descriptions of existing attributes. For more information, visit:

hashtag
Create c_cpp_properties.json

Create a file called c_cpp_properties.json in the directory .vscode and paste the following contents into it:

hashtag
Debug

Press F5 to start debugging. The following steps will be performed:

  • Compile Kernel

  • Copy zImage to qemu EFI partition

  • Launch qemu

The following keys are important:

  • F9 for creating a breakpoint

  • F10 for going a step forward

  • F11 for stepping into a function

CONFIG_DEBUG_INFO=y
CONFIG_GDB_SCRIPTS=y
Qemu emulation
Start GDB debugging

F12 to step out of a function

https://go.microsoft.com/fwlink/?LinkId=733558arrow-up-right
https://go.microsoft.com/fwlink/?linkid=830387arrow-up-right
tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build Kernel for ARM",
      "type": "shell",
      "command": "make",
      "args": [
          "ARCH=arm",
          "CROSS_COMPILE=arm-linux-gnueabihf-",
          "-j6"
      ],
      "group": {
          "kind": "build",
          "isDefault": true
      },
      "problemMatcher": [
          "$gcc"
      ]
    },
    {
      "label": "Copy zImage",
      "command": "cp",
      "args": [
        "arch/arm/boot/zImage",
        "../emulation/boot/efi/boot/bootarm.efi"
      ],
      "dependsOn":["Build Kernel for ARM"]
    },
    {
      "label": "Run qemu",
      "command": "/usr/bin/qemu-system-arm -m 1024 -cpu cortex-a15 -M virt -pflash flash0.img -pflash flash1.img -nographic -drive file=fat:rw:boot/ -smp '4' -s",
      "options": {
        "cwd": "${workspaceFolder}/../emulation"
      },
      "type": "shell",
      "isBackground": true,
      "problemMatcher": [
        {
          "pattern": [
            {
              "regexp": ".",
              "file": 1,
              "location": 2,
              "message": 3
          }
          ],
          "background": {
            "activeOnStart": true,
            "beginsPattern": ".",
            "endsPattern": ".",
          }
        }
      ],
      "dependsOn":["Copy zImage"]
    },
    {
      "label": "Terminate All Tasks",
      "command": "echo ${input:terminate}",
      "type": "shell",
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "terminate",
      "type": "command",
      "command": "workbench.action.tasks.terminate",
      "args": "terminateAll"
    }
  ]
} 
launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Run",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/vmlinux",
            "miDebuggerServerAddress": "localhost:1234",
            "miDebuggerPath": "/usr/bin/gdb-multiarch",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "targetArchitecture": "arm",
            "preLaunchTask": "Run qemu",
            "postDebugTask": "Terminate All Tasks",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}",
                "${workspaceFolder}/include",
                "${workspaceFolder}/include/uapi",
                "${workspaceFolder}/include/generated",
                "${workspaceFolder}/arch/arm/include",
                "${workspaceFolder}/arch/arm/include/uapi",
                "${workspaceFolder}/arch/arm/include/generated"
            ],
            "defines": [
                "__KERNEL__"
            ],
            "compilerPath": "/usr/bin/arm-linux-gnueabihf-gcc",
            "cStandard": "gnu17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-arm",
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "${workspaceFolder}/include",
                    "${workspaceFolder}/mm",
                    "${workspaceFolder}/fs",
                    "${workspaceFolder}/kernel"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}