Top Banner
QEMU Code Overview Architecture & internals tour Stefan Hajnoczi <[email protected]>
23

QEMU Code Overview -

Feb 11, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: QEMU Code Overview -

QEMU Code OverviewArchitecture & internals tour

Stefan Hajnoczi <[email protected]>

Page 2: QEMU Code Overview -

Covered topics

Enough details about QEMU to:● Understand how components fit together● Build and start contributing● Debug and troubleshoot

Too little time to step through source code,follow code references if you want to know more

Page 3: QEMU Code Overview -

What is QEMU?

Emulates x86, ARM, PowerPC, and other machines

Used for virtualization with KVM and Xen

Written in C, runs on POSIX and Windows hosts

Code at qemu-project.org under GPLv2

Page 4: QEMU Code Overview -

External interfacesInteracting with the outside world

Page 5: QEMU Code Overview -

Command-line options

Guest is defined on command-line:qemu -m 1024 \ -machine accel=kvm \ -hda web-server.img

man qemu for most options

See qemu-options.hx and vl.c:main() for implementation

Page 6: QEMU Code Overview -

QMP monitor

JSON RPC-like API for managing QEMU:● Hotplug devices● Stop/continue guest● Query device information● etc

Write custom scripts with QMP/qmp.py

See qapi-schema.json and QMP/

Page 7: QEMU Code Overview -

HMP monitor

Text-based interface for managing QEMU

Superseded by QMP but handy for interactive sessions

See hmp-commands.hx

Page 8: QEMU Code Overview -

User interfaces

Remote UIs include VNC and SPICE

Local UIs include GTK and SDL

See ui/

Page 9: QEMU Code Overview -

Logging

Errors and warnings go to the monitor, if currently running a command

Otherwise they are printed to stderr

Page 10: QEMU Code Overview -

ArchitectureHow it fits together

Page 11: QEMU Code Overview -

QEMU process model

Host Kernel

Guest RAM

QEMU

QEMU is a userspace process

QEMU owns guest RAM

Each KVM vCPU is a thread

Host kernel scheduler decides when QEMU and vCPUs run

Can use ps(1), nice(1), cgroups

Page 12: QEMU Code Overview -

Main loop

QEMU is event-driven, has async APIs for:● File descriptor is readable or writeable● Timer expiration● Deferred workGlobal mutex protects QEMU code● No need to synchronize explicitly● Gradually being removed to improve

scalability

See include/qemu/main-loop.h

Page 13: QEMU Code Overview -

Architecture summary

Main loop● Monitor● UI● Host I/O

completion● Deferred

work● Timers

Host kernelKVM, host I/O, scheduling, resource limits

vCPU #0● Run guest

code● Device

emulation

vCPU #1● Run guest

code● Device

emulation

Page 14: QEMU Code Overview -

Device emulationImplementing guest hardware

Page 15: QEMU Code Overview -

Hardware emulation model

Accelerators run guest code:● KVM uses hardware assist (VMX/SVM)● TCG does binary translation

Devices implement guest hardware:● See hw/ for code● List available devices: qemu -device \?

Page 16: QEMU Code Overview -

KVM accelerator pseudo-codeopen("/dev/kvm")ioctl(KVM_CREATE_VM)

ioctl(KVM_CREATE_VCPU)for (;;) { ioctl(KVM_RUN) switch (exit_reason) { case KVM_EXIT_IO: /* ... */ case KVM_EXIT_HLT: /* ... */ }}

Page 17: QEMU Code Overview -

Guest/host device split

Guest devices simulate real hardware● Net example: e1000 PCI adapter● Disk example: virtio-blk device

Host devices implement I/O● Net example: tap device● Disk example: GlusterFS backend

This allows flexible guest/host device pairing

Page 18: QEMU Code Overview -

Guest device emulation

Devices have memory or I/O regionsMust implement read/write handler functions

Devices can raise interrupts to notify guest

Inspect devices using info qtree

Inspect memory regions using info mtree

Page 19: QEMU Code Overview -

DevelopmentContributing to QEMU

Page 20: QEMU Code Overview -

git clone git://git.qemu-project.org/qemu.git

Page 21: QEMU Code Overview -

Build process

./configure shell script detects library dependencies

Check ./configure output to confirm optional features are enabled

Only build x86_64 guest support with --target-list=x86_64-softmmu

Page 22: QEMU Code Overview -

Contributing

Specifications and documentation, see docs/

Read CODING_STYLE and HACKING

Use scripts/checkpatch.pl to scan your patches

More info:http://qemu-project.org/Contribute/SubmitAPatch

Page 23: QEMU Code Overview -

Where to find out more

More QEMU architecture overview on my blog:http://goo.gl/sdaVV

Read the code, documentation is sparse

Mailing list: [email protected]: #qemu on irc.oftc.net