@@ -11,13 +11,14 @@ performances.
11
11
QEMU's dynamic translation backend is called TCG, for "Tiny Code
12
12
Generator". For more information, please take a look at ``tcg/README ``.
13
13
14
- Some notable features of QEMU's dynamic translator are:
14
+ The following sections outline some notable features and implementation
15
+ details of QEMU's dynamic translator.
15
16
16
17
CPU state optimisations
17
18
-----------------------
18
19
19
- The target CPUs have many internal states which change the way it
20
- evaluates instructions. In order to achieve a good speed, the
20
+ The target CPUs have many internal states which change the way they
21
+ evaluate instructions. In order to achieve a good speed, the
21
22
translation phase considers that some state information of the virtual
22
23
CPU cannot change in it. The state is recorded in the Translation
23
24
Block (TB). If the state changes (e.g. privilege level), a new TB will
@@ -31,17 +32,95 @@ Direct block chaining
31
32
---------------------
32
33
33
34
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
+ Program Counter (PC) and other CPU state information (such as the CS
35
36
segment base value) to find the next basic block.
36
37
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
39
- next one.
40
-
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.
38
+ In its simplest, less optimized form, this is done by exiting from the
39
+ current TB, going through the TB epilogue, and then back to the
40
+ main loop. That’s where QEMU looks for the next TB to execute,
41
+ translating it from the guest architecture if it isn’t already available
42
+ in memory. Then QEMU proceeds to execute this next TB, starting at the
43
+ prologue and then moving on to the translated instructions.
44
+
45
+ Exiting from the TB this way will cause the ``cpu_exec_interrupt() ``
46
+ callback to be re-evaluated before executing additional instructions.
47
+ It is mandatory to exit this way after any CPU state changes that may
48
+ unmask interrupts.
49
+
50
+ In order to accelerate the cases where the TB for the new
51
+ simulated PC is already available, QEMU has mechanisms that allow
52
+ multiple TBs to be chained directly, without having to go back to the
53
+ main loop as described above. These mechanisms are:
54
+
55
+ ``lookup_and_goto_ptr ``
56
+ ^^^^^^^^^^^^^^^^^^^^^^^
57
+
58
+ Calling ``tcg_gen_lookup_and_goto_ptr() `` will emit a call to
59
+ ``helper_lookup_tb_ptr ``. This helper will look for an existing TB that
60
+ matches the current CPU state. If the destination TB is available its
61
+ code address is returned, otherwise the address of the JIT epilogue is
62
+ returned. The call to the helper is always followed by the tcg ``goto_ptr ``
63
+ opcode, which branches to the returned address. In this way, we either
64
+ branch to the next TB or return to the main loop.
65
+
66
+ ``goto_tb + exit_tb ``
67
+ ^^^^^^^^^^^^^^^^^^^^^
68
+
69
+ The translation code usually implements branching by performing the
70
+ following steps:
71
+
72
+ 1. Call ``tcg_gen_goto_tb() `` passing a jump slot index (either 0 or 1)
73
+ as a parameter.
74
+
75
+ 2. Emit TCG instructions to update the CPU state with any information
76
+ that has been assumed constant and is required by the main loop to
77
+ correctly locate and execute the next TB. For most guests, this is
78
+ just the PC of the branch destination, but others may store additional
79
+ data. The information updated in this step must be inferable from both
80
+ ``cpu_get_tb_cpu_state() `` and ``cpu_restore_state() ``.
81
+
82
+ 3. Call ``tcg_gen_exit_tb() `` passing the address of the current TB and
83
+ the jump slot index again.
84
+
85
+ Step 1, ``tcg_gen_goto_tb() ``, will emit a ``goto_tb `` TCG
86
+ instruction that later on gets translated to a jump to an address
87
+ associated with the specified jump slot. Initially, this is the address
88
+ of step 2's instructions, which update the CPU state information. Step 3,
89
+ ``tcg_gen_exit_tb() ``, exits from the current TB returning a tagged
90
+ pointer composed of the last executed TB’s address and the jump slot
91
+ index.
92
+
93
+ The first time this whole sequence is executed, step 1 simply jumps
94
+ to step 2. Then the CPU state information gets updated and we exit from
95
+ the current TB. As a result, the behavior is very similar to the less
96
+ optimized form described earlier in this section.
97
+
98
+ Next, the main loop looks for the next TB to execute using the
99
+ current CPU state information (creating the TB if it wasn’t already
100
+ available) and, before starting to execute the new TB’s instructions,
101
+ patches the previously executed TB by associating one of its jump
102
+ slots (the one specified in the call to ``tcg_gen_exit_tb() ``) with the
103
+ address of the new TB.
104
+
105
+ The next time this previous TB is executed and we get to that same
106
+ ``goto_tb `` step, it will already be patched (assuming the destination TB
107
+ is still in memory) and will jump directly to the first instruction of
108
+ the destination TB, without going back to the main loop.
109
+
110
+ For the ``goto_tb + exit_tb `` mechanism to be used, the following
111
+ conditions need to be satisfied:
112
+
113
+ * The change in CPU state must be constant, e.g., a direct branch and
114
+ not an indirect branch.
115
+
116
+ * The direct branch cannot cross a page boundary. Memory mappings
117
+ may change, causing the code at the destination address to change.
118
+
119
+ Note that, on step 3 (``tcg_gen_exit_tb() ``), in addition to the
120
+ jump slot index, the address of the TB just executed is also returned.
121
+ This address corresponds to the TB that will be patched; it may be
122
+ different than the one that was directly executed from the main loop
123
+ if the latter had already been chained to other TBs.
45
124
46
125
Self-modifying code and translated code invalidation
47
126
----------------------------------------------------
0 commit comments