5 QEMU is a dynamic translator. When it first encounters a piece of code,
6 it converts it to the host instruction set. Usually dynamic translators
7 are very complicated and highly CPU dependent. QEMU uses some tricks
8 which make it relatively easily portable and simple while achieving good
11 QEMU's dynamic translation backend is called TCG, for "Tiny Code
12 Generator". For more information, please take a look at ``tcg/README``.
14 Some notable features of QEMU's dynamic translator are:
16 CPU state optimisations
17 -----------------------
19 The target CPUs have many internal states which change the way it
20 evaluates instructions. In order to achieve a good speed, the
21 translation phase considers that some state information of the virtual
22 CPU cannot change in it. The state is recorded in the Translation
23 Block (TB). If the state changes (e.g. privilege level), a new TB will
24 be generated and the previous TB won't be used anymore until the state
25 matches the state recorded in the previous TB. The same idea can be applied
26 to other aspects of the CPU state. For example, on x86, if the SS,
27 DS and ES segments have a zero base, then the translator does not even
28 generate an addition for the segment base.
33 After each translated basic block is executed, QEMU uses the simulated
34 Program Counter (PC) and other cpu state information (such as the CS
35 segment base value) to find the next basic block.
37 In order to accelerate the most common cases where the new simulated PC
38 is known, QEMU can patch a basic block so that it jumps directly to the
41 The most portable code uses an indirect jump. An indirect jump makes
42 it easier to make the jump target modification atomic. On some host
43 architectures (such as x86 or PowerPC), the ``JUMP`` opcode is
44 directly patched so that the block chaining has no overhead.
46 Self-modifying code and translated code invalidation
47 ----------------------------------------------------
49 Self-modifying code is a special challenge in x86 emulation because no
50 instruction cache invalidation is signaled by the application when code
53 User-mode emulation marks a host page as write-protected (if it is
54 not already read-only) every time translated code is generated for a
55 basic block. Then, if a write access is done to the page, Linux raises
56 a SEGV signal. QEMU then invalidates all the translated code in the page
57 and enables write accesses to the page. For system emulation, write
58 protection is achieved through the software MMU.
60 Correct translated code invalidation is done efficiently by maintaining
61 a linked list of every translated block contained in a given page. Other
62 linked lists are also maintained to undo direct block chaining.
64 On RISC targets, correctly written software uses memory barriers and
65 cache flushes, so some of the protection above would not be
66 necessary. However, QEMU still requires that the generated code always
67 matches the target instructions in memory in order to handle
73 longjmp() is used when an exception such as division by zero is
76 The host SIGSEGV and SIGBUS signal handlers are used to get invalid
77 memory accesses. QEMU keeps a map from host program counter to
78 target program counter, and looks up where the exception happened
79 based on the host program counter at the exception point.
81 On some targets, some bits of the virtual CPU's state are not flushed to the
82 memory until the end of the translation block. This is done for internal
83 emulation state that is rarely accessed directly by the program and/or changes
84 very often throughout the execution of a translation block---this includes
85 condition codes on x86, delay slots on SPARC, conditional execution on
86 ARM, and so on. This state is stored for each target instruction, and
87 looked up on exceptions.
92 For system emulation QEMU uses a software MMU. In that mode, the MMU
93 virtual to physical address translation is done at every memory
96 QEMU uses an address translation cache (TLB) to speed up the translation.
97 In order to avoid flushing the translated code each time the MMU
98 mappings change, all caches in QEMU are physically indexed. This
99 means that each basic block is indexed with its physical address.
101 In order to avoid invalidating the basic block chain when MMU mappings
102 change, chaining is only performed when the destination of the jump
103 shares a page with the basic block that is performing the jump.
105 The MMU can also distinguish RAM and ROM memory areas from MMIO memory
106 areas. Access is faster for RAM and ROM because the translation cache also
107 hosts the offset between guest address and host memory. Accessing MMIO
108 memory areas instead calls out to C code for device emulation.
109 Finally, the MMU helps tracking dirty pages and pages pointed to by