QEMU (Quick Emulator) is a free and open-source emulator. It emulates a computer's processor through dynamic binary translation and provides a set of different hardware and device models for the machine, enabling it to run a variety of guest operating systems. It can interoperate with Kernel-based Virtual Machine (KVM) to run virtual machines at near-native speed. QEMU can also do emulation for user-level processes, allowing applications compiled for one processor architecture to run on another.
A guest operating system running in the emulated computer accesses these devices, and runs as if it were running on real hardware. For instance, you can pass an ISO image as a parameter to QEMU, and the OS running in the emulated computer will see a real CD-ROM inserted into a CD drive.
QEMU can emulate a great variety of hardware from ARM to Sparc, but Proxmox VE is only concerned with 32 and 64 bits PC clone emulation, since it represents the overwhelming majority of server hardware. The emulation of PC clones is also one of the fastest due to the availability of processor extensions which greatly speed up QEMU when the emulated architecture is the same as the host architecture.
QEMU supports the emulation of various architectures, including x86, ARM, PowerPC, RISC-V, and others.
QEMU has multiple operating modes
User-mode emulation : In this mode QEMU runs single Linux or Darwin/macOS programs that were compiled for a different instruction set. System calls are thunked for endianness and for 32/64 bit mismatches. Fast cross-compilation and cross-debugging are the main targets for user-mode emulation.
System emulation: In this mode QEMU emulates a full computer system, including peripherals. It can be used to provide virtual hosting of several virtual computers on a single computer. QEMU can boot many guest operating systems, including Linux, Solaris, Microsoft Windows, DOS, and BSD; it supports emulating several instruction sets, including x86, MIPS, 32-bit ARMv7, ARMv8, PowerPC, RISC-V, SPARC, ETRAX CRIS and MicroBlaze.
Hypervisor Support : In this mode QEMU either acts as a Virtual Machine Manager (VMM) or as a device emulation back-end for virtual machines running under a hypervisor. The most common is Linux's KVM but the project supports a number of hypervisors including Xen, Apple's HVF, Windows' WHPX and NetBSD's nvmm.
KVM-QEMU is a virtualization that combines QEMU with KVM in a QEMU style that will connect with the KVM module to become a form of Full virtualization.
The combination of KVM and QEMU provides a form of full virtualization. This means that the guest operating system running inside the VM is unaware that it's running in a virtualized environment. It behaves as if it were running directly on the physical hardware.
So, while QEMU plays a crucial role in the overall virtualization process, KVM is the primary component responsible for the full virtualization capabilities.
Include:
From the above picture we can understand: For each type of virtualization such as Kvm, Xen, .. there will be a Libvirt process running to control the virtualization types and provide APIs for tools such as virsh, virt-manager, Openstack , ovirt can communicate with KVM-Qemu through livbirt.
Libvirt is a library, allowing you to use python and other programming languages to configure virtual machines. Virsh is a toolkit which works in terminal to monitor and configure virtual machine settings. Virt-manager is VMware player like GUI as an alternative to virsh and it uses libvirt.
The answer of Kaustubh Pradhan for the question How do KVM and QEMU work together?
KVM - resides in the Linux kernel as a loadable module. Once loaded, KVM converts the Linux kernel into a type-1 hypervisor aka bare-metal hypervisor. KVM virtualization uses the Linux kernel as its hypervisor (VM is essentially a process). However, it depends on the Intel-VT and AMD-V virtualization extensions on Intel and AMD respectively for hardware assists to enable robust virtualization. Working in concert with these extensions, KVM helps deliver a better virtualization experience with higher throughput of almost near-zero latency. Thus all the VMs (read process) can run without any performance or compatibility hit, as if it was running natively on a dedicated CPU. Also, because of the the aforementioned extensions the VMs have a greater awareness of the capabilities of the underlying hardware platform. Therefore, is fair to say that KVM offers hardware virtualization in its sincerest and best form.
QEMU - On the other hand resides in the user space and provides system emulation including the processor and various peripherals. Typically, QEMU is deployed along with KVM as an in-kernel accelerator where KVM executes most of the guest code natively, while QEMU emulates the rest of the machine (peripherals) needed by the guest. In places where the VM has to talk to external devices, QEMU uses passthrough.
When used together, KVM provides the low-level virtualization capabilities, while QEMU handles the high-level emulation and management of the virtual machines. The combination of KVM and QEMU allows for efficient, hardware-accelerated virtualization on Linux systems, with QEMU providing the user-facing tools and customization options for the virtual machines.
The typical workflow is:
This combination of KVM and QEMU allows for a flexible and performant virtualization solution on Linux, making it a popular choice for server virtualization, cloud computing, and other use cases where efficient resource utilization and isolation are important.
Map the virtual machine image to memory. This process is like booting physical virtual machine
Create vCPU and allocate space for vCPU
In this loop
hello.c
Check what are installed
Keep in mind that the naming convention of qemu user mode command is qemu-arctecture
where architecture can be arm
(ARM 32bit version), aarch64
(ARM 64bit), i386
(x86 32bit), x86_64
(x86 64 bit), etc. In next section, we will introduction qemu system mode. The naming convention is qemu-system-architecture
. You can see what the difference is.
Let's see how we can execute the hello programs on qemu.
To run a virtual machine in QEMU system mode, you indeed require an OS image
.
Choosing Linux 0.11 as your OS image for learning purposes is a great choice. It's relatively simple, allowing you to grasp the core concepts of an operating system without getting overwhelmed by complexities.
Check Old Linux and following the Linux Ancient Resources to download qemu-images/Linux 0.11 on qemu-12.5.i386.zip
Once downloaded and unziped, run command
Then enter key 1
to boot from the first device.
Now Linux 0.11 is running on qemu-system-i386. You may find many commands not found; however, there are some available commands.
Following the source code in Qemu v1.6.1 or QEMU v1.6.1 source
main()
function resides in vl.c
. It's like the grand conductor, orchestrating the entire show.main()
function gets everything ready./cpus.c
, /exec-all.c
, /exec.c
, and /cpu-exec.c
). These files handle the nitty-gritty of CPU execution, dynamic translation, and other magic.main()
function is the architect laying the foundation for your virtual world.QEMU is like a symphony—a harmonious blend of orchestration, translation, and emulation. So, next time you fire up QEMU, tip your hat to the main()
function—it's the maestro behind the scenes! 🎩🌟
(1) Qemu source code flow - Stack Overflow.
(2) on the Virtualization Stack.
(3) The QEMU build system architecture — QEMU documentation
Multithreading Magic (MTTCG):
qemu_tcg_cpu_thread_fn
function is like the director backstage, coordinating the actors (threads) for a grand performance.How It Works:
Enabling MTTCG:
TARGET_SUPPORTS_MTTCG
.TCG_TARGET_DEFAULT_MO
) supports it.Porting a Guest Architecture:
tcg_gen_mb
) gracefully.Testing and Further Work:
People Behind the Curtain:
Reading Material:
So, next time you see MTTCG in action, give a nod to the qemu_tcg_cpu_thread_fn
—it's the conductor orchestrating the multithreaded symphony! 🎭🌟
(1) Features/tcg-multithread - QEMU.
(2) TCG Intermediate Representation — QEMU documentation
(3) Cache Modelling TCG Plugin - QEMU.
(4) Cross-ISA Machine Emulation for Multicores
TCG (Tiny Code Generator) Overview:
Purpose of tcg_exec_all
:
tcg_exec_all
function plays a pivotal role in executing translated code. Let's break it down:
tcg_exec_all
shines:
tcg_exec_all
ensures that interrupt handling (like masked interrupts) happens correctly. If CPU state changes (e.g., privilege level), it exits the TB execution loop to re-evaluate interrupt conditions.Direct Block Chaining Mechanisms:
lookup_and_goto_ptr
:
helper_lookup_tb_ptr
, which searches for an existing TB matching the current CPU state.goto_tb + exit_tb
:
Why Is This Important?
Fun Fact:
TL;DR:
tcg_exec_all
orchestrates TB execution, chaining them efficiently.If QEMU were a theater, tcg_exec_all
would be the stage manager, making sure the actors (TBs) hit their cues!* 🎭🌟
(1) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
(2) Translator Internals — QEMU documentation.
(3) Qemu source code flow - Stack Overflow.
(4) Features/tcg-multithread - QEMU.
(5) Features/TCG - QEMU.
(6) Multi-threaded TCG — QEMU documentation.
The Dynamic Translation Dance (TCG):
tcg_cpu_exec
Takes the Stage:
tcg_cpu_exec
function orchestrates this translation ballet. It's like the choreographer ensuring the steps flow seamlessly:
tcg_cpu_exec
directs the vCPU through this TB, executing the translated dance moves.CPU State Optimizations:
Direct Block Chaining (Fancy Footwork):
lookup_and_goto_ptr
:
goto_tb + exit_tb
:
Why Is This Important?
Fun Fact:
You imagine it as the lead dancer—guiding the vCPUs through their translated routines! 💃🌟
(1) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
(2) Documentation/TCG - QEMU.
(3) Emulation — QEMU documentation.
(4) Translator Internals — QEMU documentation.
(5) A deep dive into QEMU: The Tiny Code Generator (TCG), part 1.
(6) Features/tcg-multithread - QEMU.
(7) Documentation/TCG/frontend-ops - QEMU.
The CPU and Execution:
cpu_exec
Takes the Stage:
cpu_exec
function:
cpu_exec
function:
Direct Block Chaining (Efficient Moves):
lookup_and_goto_ptr
:
goto_tb + exit_tb
:
Why Is This Important?
Fun Fact:
You imagine it as the lead dancer, guiding the vCPU through its translated routines! 💃🌟
(1) The CPU and the fetch-execute cycle What is the purpose of the CPU? - BBC.
(2) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
(3) QEMU/cpu-exec.c at master · mp-lee/QEMU · GitHub.
(4) How Does a Computer Execute Code? - MUO.
Translation Blocks (TBs) and Their Importance:
The Role of tb_find_fast
:
tb_find_fast
function is like a backstage guide. When a vCPU needs to execute an instruction, it checks whether a corresponding TB is already in the cache.tb_find_fast
uses a hash function value to find the index of the TB in the cache.Fallback to tb_find_slow
:
tb_find_fast
gracefully falls back to tb_find_slow
.tb_find_slow
performs a sequential search through the cache to locate the correct TB.Why Is This Important?
You imagine it as the backstage coordinator, swiftly guiding the vCPUs to their cached routines! 🎭🌟
(1) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
(2) Translator Internals — QEMU documentation.
(3) TCG Intermediate Representation — QEMU documentation.
Translation Blocks (TBs) and Their Importance:
The Role of tb_find_slow
:
tb_find_slow
function is like a backstage guide. When a vCPU needs to execute an instruction, it checks whether a corresponding TB is already in the cache.tb_find_fast
uses a hash function value to find the index of the TB in the cache.tb_find_slow
steps in.tb_find_slow
gracefully falls back to a sequential search.Why Is This Important?
tb_find_slow
ensures we don't miss our cues.You imagine it as the backstage coordinator, meticulously searching for the right costumes in the cache! 🎭🌟
(1) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
(2) Translator Internals — QEMU documentation.
(3) arm - How to use QEMU's simple trace backend? - Stack Overflow.
Translation Blocks (TBs) and Their Importance:
The Role of cpu_tb_exec
:
cpu_tb_exec
function is like the lead dancer. When a vCPU needs to execute an instruction, it steps onto the stage (the current TB).cpu_tb_exec
directs the vCPU through this TB, executing the translated dance moves.tb_find_fast
: Tries a fast lookup using a hash function value. If the TB is valid, it's a quick win—the vCPU jumps directly to that TB.tb_find_slow
: If the fast search fails (maybe due to state changes), it gracefully falls back to a sequential search. It meticulously examines each dressing room (TB) until it finds the right costume for the current scene (instruction).Why Is This Important?
You imagine it as the lead dancer, guiding the vCPU through its translated routines! 💃🌟
(1) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
(2) Translator Internals — QEMU documentation.
(3) TCG Intermediate Representation — QEMU documentation.
Translation Blocks (TBs) Recap:
The Role of tb_gen_code
:
tb_gen_code
function is like the costume designer. When QEMU encounters a new guest instruction (one not already translated), it creates a fresh TB.tb_gen_code
function:
TB Size and Hashing:
tb_find_fast
) uses a hash function value to find the index of the TB in the cache.tb_find_slow
performs a sequential search to locate the correct TB.Why Is This Important?
You imagine it as the costume designer, tailoring unique outfits for each guest instruction! 🎭🌟
(1) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
(2) qemu_blog/exec.md at main · airbus-seclab/qemu_blog · GitHub.
(3) Features/TCGCodeQuality - QEMU.
(4) Translator Internals — QEMU documentation.
Translation Blocks (TBs) and TCG:
The Role of cpu_gen_code
:
cpu_gen_code
function is like the master tailor. When QEMU encounters a new guest instruction (one not already translated), it creates a fresh TB.cpu_gen_code
:
TB Size and Hashing:
tb_find_fast
) uses a hash function value to find the index of the TB in the cache.tb_find_slow
performs a sequential search to locate the correct TB.Why Is This Important?
You imagine it as the master tailor, crafting unique outfits for each guest instruction! 🎭🌟
(1) Translator Internals — QEMU documentation.
(2) A deep dive into QEMU: The Tiny Code Generator (TCG), part 1.
(3) Features/TCGCodeQuality - QEMU.
(4) TCG Intermediate Representation — QEMU documentation.
Intermediate Code Generation:
The Role of gen_intermediate_code
:
gen_intermediate_code
function is like the playwright. When QEMU encounters a new guest instruction (one not already translated), it creates an intermediate representation (IR) of that instruction.gen_intermediate_code
:
Why Is This Important?
You imagine it as the playwright, crafting the script that guides the translation process! 🎭🌟
(1) TCG Intermediate Representation — QEMU documentation.
(2) Compiler - Intermediate Code Generation - Online Tutorials Library.
(3) Intermediate Code Generation in Compiler Design - GeeksforGeeks.
(4) Chapter Intermediate-Code Generation.
(5) Can I examine Qemu tcg ir code? If so how? - Stack Overflow.
Intermediate Representation (IR) and TCG:
The Role of gen_intermediate_code_internal
:
gen_intermediate_code_internal
function is like the playwright. When QEMU encounters a new guest instruction (one not already translated), it creates an intermediate representation (IR) of that instruction.gen_intermediate_code_internal
:
Why Is This Important?
You imagine it as the playwright, crafting the script that guides the translation process! 🎭🌟
(1) TCG Intermediate Representation — QEMU documentation.
(2) Translator Internals — QEMU documentation.
(3) Target Code Generation - University of Washington.
(4) Can I examine Qemu tcg ir code? If so how? - Stack Overflow.
Dissecting disas_insn
:
disas_insn
function is like a backstage spotlight operator. Its job? Illuminate the guest instructions and reveal their secrets.Guest Instructions Meet the Spotlight:
disas_insn
steps in:
Why Is This Important?
disas_insn
provides this insight.Fun Fact:
-d op
option in QEMU enables debug printing of TCG ops (the internal intermediate code).in_asm
and out_asm
.-D file
option dumps the tracing to a file for detailed analysis.You imagine it as the spotlight operator, revealing the intricacies of guest instructions! 🎭🌟
(1) Can I examine Qemu tcg ir code? If so how? - Stack Overflow.
(2) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
(3) Translator Internals — QEMU documentation.
TCG (Tiny Code Generator) and Intermediate Representation (IR):
The Role of tcg_gen_code
:
tcg_gen_code
function is like the conductor. When QEMU encounters a new guest instruction (one not already translated), it orchestrates the creation of an IR.tcg_gen_code
:
Why Is This Important?
You imagine it as the conductor, orchestrating the creation of the intermediate dance steps! 🎭🌟
(1) TCG Intermediate Representation — QEMU documentation.
(2) Documentation/TCG/frontend-ops - QEMU.
(3) Features/TCG - QEMU.
(4) Features/tcg-multithread - QEMU.
TCG (Tiny Code Generator) and Intermediate Representation (IR):
The Role of tcg_gen_code_common
:
tcg_gen_code_common
function is like the conductor. When QEMU encounters a new guest instruction (one not already translated), it orchestrates the creation of an IR.tcg_gen_code_common
:
Why Is This Important?
You imagine it as the conductor, orchestrating the creation of the intermediate dance steps! 🎭🌟
(1) TCG Intermediate Representation — QEMU documentation.
(2) Multi-threaded TCG — QEMU documentation.
(3) Features/TCG - QEMU.
(4) Documentation/TCG/frontend-ops - QEMU.
TCG and Intermediate Representation (IR) Recap:
The Role of tcg_out_op
:
tcg_out_op
function is like the scriptwriter. When TCG translates guest instructions to IR, it generates TCG ops (operations) that represent these instructions.tcg_out_op
:
Why Is This Important?
You imagine it as the scriptwriter, crafting the instructions for the TCG performance! 🎭🌟
(1) TCG Intermediate Representation — QEMU documentation.
(2) A deep dive into QEMU: The Tiny Code Generator (TCG), part 1.
(3) Documentation/TCG/frontend-ops - QEMU.
(4) Documentation/TCG - QEMU.
(5) Can I examine Qemu tcg ir code? If so how? - Stack Overflow.
TCG and Intermediate Representation (IR) Recap:
The Role of tcg_out_op_YYY
:
tcg_out_op_YYY
function is like the scriptwriter. When TCG translates guest instructions to IR, it generates TCG ops (operations) that represent these instructions.tcg_out_op_YYY
:
Why Is This Important?
(1) TCG Intermediate Representation — QEMU documentation.
(2) Documentation/TCG/frontend-ops - QEMU.
(3) QEMU - Code Flow [ Instruction cache and TCG] - Stack Overflow.
A virtual machine (VM) is an operating system (OS) or application environment installed on software that imitates dedicated hardware. The end user's experience when using a VM is equivalent to that of using dedicated hardware.
A VM provides an isolated environment for running its own OS and applications, independent from the underlying host system or other VMs on that host. A VM's OS, commonly referred to as the guest OS, can be the same as or different from the host OS and the OSes of other VMs on the host.
A single computer can host multiple VMs running different OSes and applications without affecting or interfering with each other. Although the VM is still dependent on the host's physical resources, those resources are virtualized and distributed across the VMs and can be reassigned as necessary. This makes it possible to run different environments simultaneously and accommodate fluctuating workloads.
Hosting VMs on a computer requires a specialized type of software called a hypervisor, which manages resources and allocates them to VMs. The hypervisor also schedules and adjusts how resources are distributed based on the configuration of the hypervisor and VMs, including reallocating resources as demands fluctuate.
The hypervisor emulates the computer's CPU, memory, hard disk, network and other hardware resources, creating a pool of resources to allocate to individual VMs according to their specific requirements. The hypervisor can support multiple virtual hardware platforms that are isolated from each other, enabling VMs to run Linux and Windows Server OSes on the same physical host.
Most hypervisors do not require special hardware components. However, the computer that runs the hypervisor must have the resources necessary to support VMs, the hypervisor's operations and the host's own operations.
A hypervisor is a software that you can use to run multiple virtual machines on a single physical machine. Every virtual machine has its own operating system and applications. The hypervisor allocates the underlying physical computing resources such as CPU and memory to individual virtual machines as required.
Developed by IBM in the 1960s to enable partitioning—and more efficient use—of resources within its mainframe computers, hypervisor technology matured and became a key element of the hardware virtualization that was added to PCs and servers. Hypervisors enabled Linux and Unix systems to expand hardware capabilities, improve reliability, and manage costs. Today’s hypervisors are available in two primary types.
Also called bare-metal hypervisors, run directly on the computer’s hardware, or bare metal, without any operating systems or other underlying software. They require a separate management machine to administer and control the virtual environment. Type 1 hypervisors are highly secure because they have direct access to the physical hardware with nothing in between that could be compromised in an attack. They allow for more resources to be assigned to virtual machines than are available, and since only the necessary resources are consumed by the instance, they are also highly efficient. These two important features make Type 1 hypervisors a central element in enterprise datacenters.
In addition to server operating systems, Type 1 hypervisors can also virtualize desktop operating systems. This is the foundation of virtual desktop infrastructure (VDI), which allows users to access desktop environments such as Windows or Linux that are running inside virtual machines on a central server. Through a connection broker, the hypervisor assigns a virtual desktop from a pool to a single user who accesses it over the network, enabling remote work from any device. Citrix VDI solutions deliver this functionality from both on-premises servers and via the cloud.
Also called hosted hypervisors, run as an application in an operating system. They require the host operating system to perform their function like any other application, and the guest operating system runs as a process on the host while the hypervisor isolates the guest from the host. Multiple Type 2 hypervisors can be run on top of a single host operating system, and each hypervisor may itself have multiple operating systems. Type 2 hypervisors are simple to set up and enable quick access between applications running on the guest and host operating systems, but are not capable of running the complex workloads that Type 1 hypervisors run.
KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko.
Kernel-based Virtual Machine (KVM) is a software feature that you can install on physical Linux machines to create virtual machines. A virtual machine is a software application that acts as an independent computer within another physical computer. It shares resources like CPU cycles, network bandwidth, and memory with the physical machine. KVM is a Linux operating system component that provides native support for virtual machines on Linux. It has been available in Linux distributions since 2007.
Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.
KVM is open source software. The kernel component of KVM is included in mainline Linux, as of 2.6.20. The userspace component of KVM is included in mainline QEMU, as of 1.3.
The hardware or software of a hypervisor undergoes optimization of capacity to be distributed among the virtual machines in a balanced manner. KVM converts Linux into a type-1 (bare-metal) hypervisor. All hypervisors need some operating system-level components—such as a memory manager, process scheduler, input/output (I/O) stack, device drivers, security manager, a network stack, and more—to run VMs. KVM has all these components because it’s part of the Linux kernel. Every VM is implemented as a regular Linux process, scheduled by the standard Linux scheduler, with dedicated virtual hardware like a network card, graphics adapter, CPU(s), memory, and disks.
Type 1 directly uses the hardware of the system to create virtual machines, whereas Type 2 hypervisor uses the software of the host operating system for a virtual division.
Type 2 hypervisors need a medium to make virtual machines. Here comes the Linux kernel, KVM, that makes any Linux OS software suitable for divisions into small units of virtual machines. It converts a Linux system into a type 2 hypervisor to allow the division of cloud hardware to each of these units for them to work independently.
Above is the KVM Stack including 4 layers:
Virtual Machine - VM
What is a hypervisor
What is a hypervisor?
Are hardware drivers needed to be installed on the management OS of a type 1 hypervisor?
Linux-kvm ~ main page
What is KVM
What is Kernel-based Virtual Machines (KVM)?
KVM basic
KVM intro
QEMU
KVM - QEMU
Qemu/KVM Virtual Machines
Understanding Hypervisors: Exploring Type-1 vs Type-2 and Full vs Para Virtualization
Management Tools
Difference between KVM and QEMU
How do KVM and QEMU work together?