Statistics
| Branch: | Revision:

root / qemu-tech.texi @ aa82ba54

History | View | Annotate | Download (23.3 kB)

1 1f673135 bellard
\input texinfo @c -*- texinfo -*-
2 debc7065 bellard
@c %**start of header
3 debc7065 bellard
@setfilename qemu-tech.info
4 e080e785 Stefan Weil
5 e080e785 Stefan Weil
@documentlanguage en
6 e080e785 Stefan Weil
@documentencoding UTF-8
7 e080e785 Stefan Weil
8 debc7065 bellard
@settitle QEMU Internals
9 debc7065 bellard
@exampleindent 0
10 debc7065 bellard
@paragraphindent 0
11 debc7065 bellard
@c %**end of header
12 1f673135 bellard
13 a1a32b05 Stefan Weil
@ifinfo
14 a1a32b05 Stefan Weil
@direntry
15 a1a32b05 Stefan Weil
* QEMU Internals: (qemu-tech).   The QEMU Emulator Internals.
16 a1a32b05 Stefan Weil
@end direntry
17 a1a32b05 Stefan Weil
@end ifinfo
18 a1a32b05 Stefan Weil
19 1f673135 bellard
@iftex
20 1f673135 bellard
@titlepage
21 1f673135 bellard
@sp 7
22 1f673135 bellard
@center @titlefont{QEMU Internals}
23 1f673135 bellard
@sp 3
24 1f673135 bellard
@end titlepage
25 1f673135 bellard
@end iftex
26 1f673135 bellard
27 debc7065 bellard
@ifnottex
28 debc7065 bellard
@node Top
29 debc7065 bellard
@top
30 debc7065 bellard
31 debc7065 bellard
@menu
32 debc7065 bellard
* Introduction::
33 debc7065 bellard
* QEMU Internals::
34 debc7065 bellard
* Regression Tests::
35 debc7065 bellard
* Index::
36 debc7065 bellard
@end menu
37 debc7065 bellard
@end ifnottex
38 debc7065 bellard
39 debc7065 bellard
@contents
40 debc7065 bellard
41 debc7065 bellard
@node Introduction
42 1f673135 bellard
@chapter Introduction
43 1f673135 bellard
44 debc7065 bellard
@menu
45 3aeaea65 Max Filippov
* intro_features::         Features
46 3aeaea65 Max Filippov
* intro_x86_emulation::    x86 and x86-64 emulation
47 3aeaea65 Max Filippov
* intro_arm_emulation::    ARM emulation
48 3aeaea65 Max Filippov
* intro_mips_emulation::   MIPS emulation
49 3aeaea65 Max Filippov
* intro_ppc_emulation::    PowerPC emulation
50 3aeaea65 Max Filippov
* intro_sparc_emulation::  Sparc32 and Sparc64 emulation
51 3aeaea65 Max Filippov
* intro_xtensa_emulation:: Xtensa emulation
52 3aeaea65 Max Filippov
* intro_other_emulation::  Other CPU emulation
53 debc7065 bellard
@end menu
54 debc7065 bellard
55 debc7065 bellard
@node intro_features
56 1f673135 bellard
@section Features
57 1f673135 bellard
58 1f673135 bellard
QEMU is a FAST! processor emulator using a portable dynamic
59 1f673135 bellard
translator.
60 1f673135 bellard
61 1f673135 bellard
QEMU has two operating modes:
62 1f673135 bellard
63 1f673135 bellard
@itemize @minus
64 1f673135 bellard
65 5fafdf24 ths
@item
66 998a0501 blueswir1
Full system emulation. In this mode (full platform virtualization),
67 998a0501 blueswir1
QEMU emulates a full system (usually a PC), including a processor and
68 998a0501 blueswir1
various peripherals. It can be used to launch several different
69 998a0501 blueswir1
Operating Systems at once without rebooting the host machine or to
70 998a0501 blueswir1
debug system code.
71 1f673135 bellard
72 5fafdf24 ths
@item
73 998a0501 blueswir1
User mode emulation. In this mode (application level virtualization),
74 998a0501 blueswir1
QEMU can launch processes compiled for one CPU on another CPU, however
75 998a0501 blueswir1
the Operating Systems must match. This can be used for example to ease
76 998a0501 blueswir1
cross-compilation and cross-debugging.
77 1f673135 bellard
@end itemize
78 1f673135 bellard
79 1f673135 bellard
As QEMU requires no host kernel driver to run, it is very safe and
80 1f673135 bellard
easy to use.
81 1f673135 bellard
82 1f673135 bellard
QEMU generic features:
83 1f673135 bellard
84 5fafdf24 ths
@itemize
85 1f673135 bellard
86 1f673135 bellard
@item User space only or full system emulation.
87 1f673135 bellard
88 debc7065 bellard
@item Using dynamic translation to native code for reasonable speed.
89 1f673135 bellard
90 998a0501 blueswir1
@item
91 998a0501 blueswir1
Working on x86, x86_64 and PowerPC32/64 hosts. Being tested on ARM,
92 998a0501 blueswir1
HPPA, Sparc32 and Sparc64. Previous versions had some support for
93 998a0501 blueswir1
Alpha and S390 hosts, but TCG (see below) doesn't support those yet.
94 1f673135 bellard
95 1f673135 bellard
@item Self-modifying code support.
96 1f673135 bellard
97 1f673135 bellard
@item Precise exceptions support.
98 1f673135 bellard
99 5fafdf24 ths
@item The virtual CPU is a library (@code{libqemu}) which can be used
100 ad6a4837 bellard
in other projects (look at @file{qemu/tests/qruncom.c} to have an
101 ad6a4837 bellard
example of user mode @code{libqemu} usage).
102 1f673135 bellard
103 998a0501 blueswir1
@item
104 998a0501 blueswir1
Floating point library supporting both full software emulation and
105 998a0501 blueswir1
native host FPU instructions.
106 998a0501 blueswir1
107 1f673135 bellard
@end itemize
108 1f673135 bellard
109 1f673135 bellard
QEMU user mode emulation features:
110 5fafdf24 ths
@itemize
111 1f673135 bellard
@item Generic Linux system call converter, including most ioctls.
112 1f673135 bellard
113 1f673135 bellard
@item clone() emulation using native CPU clone() to use Linux scheduler for threads.
114 1f673135 bellard
115 5fafdf24 ths
@item Accurate signal handling by remapping host signals to target signals.
116 1f673135 bellard
@end itemize
117 1f673135 bellard
118 998a0501 blueswir1
Linux user emulator (Linux host only) can be used to launch the Wine
119 998a0501 blueswir1
Windows API emulator (@url{http://www.winehq.org}). A Darwin user
120 998a0501 blueswir1
emulator (Darwin hosts only) exists and a BSD user emulator for BSD
121 998a0501 blueswir1
hosts is under development. It would also be possible to develop a
122 998a0501 blueswir1
similar user emulator for Solaris.
123 998a0501 blueswir1
124 1f673135 bellard
QEMU full system emulation features:
125 5fafdf24 ths
@itemize
126 998a0501 blueswir1
@item
127 998a0501 blueswir1
QEMU uses a full software MMU for maximum portability.
128 998a0501 blueswir1
129 998a0501 blueswir1
@item
130 4a1418e0 Anthony Liguori
QEMU can optionally use an in-kernel accelerator, like kvm. The accelerators 
131 4a1418e0 Anthony Liguori
execute some of the guest code natively, while
132 998a0501 blueswir1
continuing to emulate the rest of the machine.
133 998a0501 blueswir1
134 998a0501 blueswir1
@item
135 998a0501 blueswir1
Various hardware devices can be emulated and in some cases, host
136 998a0501 blueswir1
devices (e.g. serial and parallel ports, USB, drives) can be used
137 998a0501 blueswir1
transparently by the guest Operating System. Host device passthrough
138 998a0501 blueswir1
can be used for talking to external physical peripherals (e.g. a
139 998a0501 blueswir1
webcam, modem or tape drive).
140 998a0501 blueswir1
141 998a0501 blueswir1
@item
142 998a0501 blueswir1
Symmetric multiprocessing (SMP) even on a host with a single CPU. On a
143 998a0501 blueswir1
SMP host system, QEMU can use only one CPU fully due to difficulty in
144 998a0501 blueswir1
implementing atomic memory accesses efficiently.
145 998a0501 blueswir1
146 1f673135 bellard
@end itemize
147 1f673135 bellard
148 debc7065 bellard
@node intro_x86_emulation
149 998a0501 blueswir1
@section x86 and x86-64 emulation
150 1f673135 bellard
151 1f673135 bellard
QEMU x86 target features:
152 1f673135 bellard
153 5fafdf24 ths
@itemize
154 1f673135 bellard
155 5fafdf24 ths
@item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation.
156 998a0501 blueswir1
LDT/GDT and IDT are emulated. VM86 mode is also supported to run
157 998a0501 blueswir1
DOSEMU. There is some support for MMX/3DNow!, SSE, SSE2, SSE3, SSSE3,
158 998a0501 blueswir1
and SSE4 as well as x86-64 SVM.
159 1f673135 bellard
160 1f673135 bellard
@item Support of host page sizes bigger than 4KB in user mode emulation.
161 1f673135 bellard
162 1f673135 bellard
@item QEMU can emulate itself on x86.
163 1f673135 bellard
164 5fafdf24 ths
@item An extensive Linux x86 CPU test program is included @file{tests/test-i386}.
165 1f673135 bellard
It can be used to test other x86 virtual CPUs.
166 1f673135 bellard
167 1f673135 bellard
@end itemize
168 1f673135 bellard
169 1f673135 bellard
Current QEMU limitations:
170 1f673135 bellard
171 5fafdf24 ths
@itemize
172 1f673135 bellard
173 998a0501 blueswir1
@item Limited x86-64 support.
174 1f673135 bellard
175 1f673135 bellard
@item IPC syscalls are missing.
176 1f673135 bellard
177 5fafdf24 ths
@item The x86 segment limits and access rights are not tested at every
178 1f673135 bellard
memory access (yet). Hopefully, very few OSes seem to rely on that for
179 1f673135 bellard
normal use.
180 1f673135 bellard
181 1f673135 bellard
@end itemize
182 1f673135 bellard
183 debc7065 bellard
@node intro_arm_emulation
184 1f673135 bellard
@section ARM emulation
185 1f673135 bellard
186 1f673135 bellard
@itemize
187 1f673135 bellard
188 1f673135 bellard
@item Full ARM 7 user emulation.
189 1f673135 bellard
190 1f673135 bellard
@item NWFPE FPU support included in user Linux emulation.
191 1f673135 bellard
192 1f673135 bellard
@item Can run most ARM Linux binaries.
193 1f673135 bellard
194 1f673135 bellard
@end itemize
195 1f673135 bellard
196 24d4de45 ths
@node intro_mips_emulation
197 24d4de45 ths
@section MIPS emulation
198 24d4de45 ths
199 24d4de45 ths
@itemize
200 24d4de45 ths
201 24d4de45 ths
@item The system emulation allows full MIPS32/MIPS64 Release 2 emulation,
202 24d4de45 ths
including privileged instructions, FPU and MMU, in both little and big
203 24d4de45 ths
endian modes.
204 24d4de45 ths
205 24d4de45 ths
@item The Linux userland emulation can run many 32 bit MIPS Linux binaries.
206 24d4de45 ths
207 24d4de45 ths
@end itemize
208 24d4de45 ths
209 24d4de45 ths
Current QEMU limitations:
210 24d4de45 ths
211 24d4de45 ths
@itemize
212 24d4de45 ths
213 24d4de45 ths
@item Self-modifying code is not always handled correctly.
214 24d4de45 ths
215 24d4de45 ths
@item 64 bit userland emulation is not implemented.
216 24d4de45 ths
217 24d4de45 ths
@item The system emulation is not complete enough to run real firmware.
218 24d4de45 ths
219 b1f45238 ths
@item The watchpoint debug facility is not implemented.
220 b1f45238 ths
221 24d4de45 ths
@end itemize
222 24d4de45 ths
223 debc7065 bellard
@node intro_ppc_emulation
224 1f673135 bellard
@section PowerPC emulation
225 1f673135 bellard
226 1f673135 bellard
@itemize
227 1f673135 bellard
228 5fafdf24 ths
@item Full PowerPC 32 bit emulation, including privileged instructions,
229 1f673135 bellard
FPU and MMU.
230 1f673135 bellard
231 1f673135 bellard
@item Can run most PowerPC Linux binaries.
232 1f673135 bellard
233 1f673135 bellard
@end itemize
234 1f673135 bellard
235 debc7065 bellard
@node intro_sparc_emulation
236 998a0501 blueswir1
@section Sparc32 and Sparc64 emulation
237 1f673135 bellard
238 1f673135 bellard
@itemize
239 1f673135 bellard
240 f6b647cd blueswir1
@item Full SPARC V8 emulation, including privileged
241 3475187d bellard
instructions, FPU and MMU. SPARC V9 emulation includes most privileged
242 a785e42e blueswir1
and VIS instructions, FPU and I/D MMU. Alignment is fully enforced.
243 1f673135 bellard
244 a785e42e blueswir1
@item Can run most 32-bit SPARC Linux binaries, SPARC32PLUS Linux binaries and
245 a785e42e blueswir1
some 64-bit SPARC Linux binaries.
246 3475187d bellard
247 3475187d bellard
@end itemize
248 3475187d bellard
249 3475187d bellard
Current QEMU limitations:
250 3475187d bellard
251 5fafdf24 ths
@itemize
252 3475187d bellard
253 3475187d bellard
@item IPC syscalls are missing.
254 3475187d bellard
255 1f587329 blueswir1
@item Floating point exception support is buggy.
256 3475187d bellard
257 3475187d bellard
@item Atomic instructions are not correctly implemented.
258 3475187d bellard
259 998a0501 blueswir1
@item There are still some problems with Sparc64 emulators.
260 998a0501 blueswir1
261 998a0501 blueswir1
@end itemize
262 998a0501 blueswir1
263 3aeaea65 Max Filippov
@node intro_xtensa_emulation
264 3aeaea65 Max Filippov
@section Xtensa emulation
265 3aeaea65 Max Filippov
266 3aeaea65 Max Filippov
@itemize
267 3aeaea65 Max Filippov
268 3aeaea65 Max Filippov
@item Core Xtensa ISA emulation, including most options: code density,
269 3aeaea65 Max Filippov
loop, extended L32R, 16- and 32-bit multiplication, 32-bit division,
270 3aeaea65 Max Filippov
MAC16, miscellaneous operations, boolean, multiprocessor synchronization,
271 3aeaea65 Max Filippov
conditional store, exceptions, relocatable vectors, unaligned exception,
272 3aeaea65 Max Filippov
interrupts (including high priority and timer), hardware alignment,
273 3aeaea65 Max Filippov
region protection, region translation, MMU, windowed registers, thread
274 3aeaea65 Max Filippov
pointer, processor ID.
275 3aeaea65 Max Filippov
276 3aeaea65 Max Filippov
@item Not implemented options: FP coprocessor, coprocessor context,
277 3aeaea65 Max Filippov
data/instruction cache (including cache prefetch and locking), XLMI,
278 3aeaea65 Max Filippov
processor interface, debug. Also options not covered by the core ISA
279 3aeaea65 Max Filippov
(e.g. FLIX, wide branches) are not implemented.
280 3aeaea65 Max Filippov
281 3aeaea65 Max Filippov
@item Can run most Xtensa Linux binaries.
282 3aeaea65 Max Filippov
283 3aeaea65 Max Filippov
@item New core configuration that requires no additional instructions
284 3aeaea65 Max Filippov
may be created from overlay with minimal amount of hand-written code.
285 3aeaea65 Max Filippov
286 3aeaea65 Max Filippov
@end itemize
287 3aeaea65 Max Filippov
288 998a0501 blueswir1
@node intro_other_emulation
289 998a0501 blueswir1
@section Other CPU emulation
290 1f673135 bellard
291 998a0501 blueswir1
In addition to the above, QEMU supports emulation of other CPUs with
292 998a0501 blueswir1
varying levels of success. These are:
293 998a0501 blueswir1
294 998a0501 blueswir1
@itemize
295 998a0501 blueswir1
296 998a0501 blueswir1
@item
297 998a0501 blueswir1
Alpha
298 998a0501 blueswir1
@item
299 998a0501 blueswir1
CRIS
300 998a0501 blueswir1
@item
301 998a0501 blueswir1
M68k
302 998a0501 blueswir1
@item
303 998a0501 blueswir1
SH4
304 1f673135 bellard
@end itemize
305 1f673135 bellard
306 debc7065 bellard
@node QEMU Internals
307 1f673135 bellard
@chapter QEMU Internals
308 1f673135 bellard
309 debc7065 bellard
@menu
310 debc7065 bellard
* QEMU compared to other emulators::
311 debc7065 bellard
* Portable dynamic translation::
312 debc7065 bellard
* Condition code optimisations::
313 debc7065 bellard
* CPU state optimisations::
314 debc7065 bellard
* Translation cache::
315 debc7065 bellard
* Direct block chaining::
316 debc7065 bellard
* Self-modifying code and translated code invalidation::
317 debc7065 bellard
* Exception support::
318 debc7065 bellard
* MMU emulation::
319 998a0501 blueswir1
* Device emulation::
320 debc7065 bellard
* Hardware interrupts::
321 debc7065 bellard
* User emulation specific details::
322 debc7065 bellard
* Bibliography::
323 debc7065 bellard
@end menu
324 debc7065 bellard
325 debc7065 bellard
@node QEMU compared to other emulators
326 1f673135 bellard
@section QEMU compared to other emulators
327 1f673135 bellard
328 1f673135 bellard
Like bochs [3], QEMU emulates an x86 CPU. But QEMU is much faster than
329 1f673135 bellard
bochs as it uses dynamic compilation. Bochs is closely tied to x86 PC
330 1f673135 bellard
emulation while QEMU can emulate several processors.
331 1f673135 bellard
332 1f673135 bellard
Like Valgrind [2], QEMU does user space emulation and dynamic
333 1f673135 bellard
translation. Valgrind is mainly a memory debugger while QEMU has no
334 1f673135 bellard
support for it (QEMU could be used to detect out of bound memory
335 1f673135 bellard
accesses as Valgrind, but it has no support to track uninitialised data
336 1f673135 bellard
as Valgrind does). The Valgrind dynamic translator generates better code
337 1f673135 bellard
than QEMU (in particular it does register allocation) but it is closely
338 1f673135 bellard
tied to an x86 host and target and has no support for precise exceptions
339 1f673135 bellard
and system emulation.
340 1f673135 bellard
341 1f673135 bellard
EM86 [4] is the closest project to user space QEMU (and QEMU still uses
342 1f673135 bellard
some of its code, in particular the ELF file loader). EM86 was limited
343 1f673135 bellard
to an alpha host and used a proprietary and slow interpreter (the
344 1f673135 bellard
interpreter part of the FX!32 Digital Win32 code translator [5]).
345 1f673135 bellard
346 1f673135 bellard
TWIN [6] is a Windows API emulator like Wine. It is less accurate than
347 1f673135 bellard
Wine but includes a protected mode x86 interpreter to launch x86 Windows
348 36d54d15 bellard
executables. Such an approach has greater potential because most of the
349 1f673135 bellard
Windows API is executed natively but it is far more difficult to develop
350 1f673135 bellard
because all the data structures and function parameters exchanged
351 1f673135 bellard
between the API and the x86 code must be converted.
352 1f673135 bellard
353 1f673135 bellard
User mode Linux [7] was the only solution before QEMU to launch a
354 1f673135 bellard
Linux kernel as a process while not needing any host kernel
355 1f673135 bellard
patches. However, user mode Linux requires heavy kernel patches while
356 1f673135 bellard
QEMU accepts unpatched Linux kernels. The price to pay is that QEMU is
357 1f673135 bellard
slower.
358 1f673135 bellard
359 998a0501 blueswir1
The Plex86 [8] PC virtualizer is done in the same spirit as the now
360 998a0501 blueswir1
obsolete qemu-fast system emulator. It requires a patched Linux kernel
361 998a0501 blueswir1
to work (you cannot launch the same kernel on your PC), but the
362 998a0501 blueswir1
patches are really small. As it is a PC virtualizer (no emulation is
363 998a0501 blueswir1
done except for some privileged instructions), it has the potential of
364 998a0501 blueswir1
being faster than QEMU. The downside is that a complicated (and
365 998a0501 blueswir1
potentially unsafe) host kernel patch is needed.
366 1f673135 bellard
367 1f673135 bellard
The commercial PC Virtualizers (VMWare [9], VirtualPC [10], TwoOStwo
368 1f673135 bellard
[11]) are faster than QEMU, but they all need specific, proprietary
369 1f673135 bellard
and potentially unsafe host drivers. Moreover, they are unable to
370 1f673135 bellard
provide cycle exact simulation as an emulator can.
371 1f673135 bellard
372 998a0501 blueswir1
VirtualBox [12], Xen [13] and KVM [14] are based on QEMU. QEMU-SystemC
373 998a0501 blueswir1
[15] uses QEMU to simulate a system where some hardware devices are
374 998a0501 blueswir1
developed in SystemC.
375 998a0501 blueswir1
376 debc7065 bellard
@node Portable dynamic translation
377 1f673135 bellard
@section Portable dynamic translation
378 1f673135 bellard
379 1f673135 bellard
QEMU is a dynamic translator. When it first encounters a piece of code,
380 1f673135 bellard
it converts it to the host instruction set. Usually dynamic translators
381 1f673135 bellard
are very complicated and highly CPU dependent. QEMU uses some tricks
382 1f673135 bellard
which make it relatively easily portable and simple while achieving good
383 1f673135 bellard
performances.
384 1f673135 bellard
385 998a0501 blueswir1
After the release of version 0.9.1, QEMU switched to a new method of
386 998a0501 blueswir1
generating code, Tiny Code Generator or TCG. TCG relaxes the
387 998a0501 blueswir1
dependency on the exact version of the compiler used. The basic idea
388 998a0501 blueswir1
is to split every target instruction into a couple of RISC-like TCG
389 998a0501 blueswir1
ops (see @code{target-i386/translate.c}). Some optimizations can be
390 998a0501 blueswir1
performed at this stage, including liveness analysis and trivial
391 998a0501 blueswir1
constant expression evaluation. TCG ops are then implemented in the
392 998a0501 blueswir1
host CPU back end, also known as TCG target (see
393 998a0501 blueswir1
@code{tcg/i386/tcg-target.c}). For more information, please take a
394 998a0501 blueswir1
look at @code{tcg/README}.
395 1f673135 bellard
396 debc7065 bellard
@node Condition code optimisations
397 1f673135 bellard
@section Condition code optimisations
398 1f673135 bellard
399 998a0501 blueswir1
Lazy evaluation of CPU condition codes (@code{EFLAGS} register on x86)
400 998a0501 blueswir1
is important for CPUs where every instruction sets the condition
401 998a0501 blueswir1
codes. It tends to be less important on conventional RISC systems
402 f0f26a06 Blue Swirl
where condition codes are only updated when explicitly requested. On
403 f0f26a06 Blue Swirl
Sparc64, costly update of both 32 and 64 bit condition codes can be
404 f0f26a06 Blue Swirl
avoided with lazy evaluation.
405 998a0501 blueswir1
406 998a0501 blueswir1
Instead of computing the condition codes after each x86 instruction,
407 998a0501 blueswir1
QEMU just stores one operand (called @code{CC_SRC}), the result
408 998a0501 blueswir1
(called @code{CC_DST}) and the type of operation (called
409 998a0501 blueswir1
@code{CC_OP}). When the condition codes are needed, the condition
410 998a0501 blueswir1
codes can be calculated using this information. In addition, an
411 998a0501 blueswir1
optimized calculation can be performed for some instruction types like
412 998a0501 blueswir1
conditional branches.
413 1f673135 bellard
414 1235fc06 ths
@code{CC_OP} is almost never explicitly set in the generated code
415 1f673135 bellard
because it is known at translation time.
416 1f673135 bellard
417 f0f26a06 Blue Swirl
The lazy condition code evaluation is used on x86, m68k, cris and
418 f0f26a06 Blue Swirl
Sparc. ARM uses a simplified variant for the N and Z flags.
419 1f673135 bellard
420 debc7065 bellard
@node CPU state optimisations
421 1f673135 bellard
@section CPU state optimisations
422 1f673135 bellard
423 998a0501 blueswir1
The target CPUs have many internal states which change the way it
424 998a0501 blueswir1
evaluates instructions. In order to achieve a good speed, the
425 998a0501 blueswir1
translation phase considers that some state information of the virtual
426 998a0501 blueswir1
CPU cannot change in it. The state is recorded in the Translation
427 998a0501 blueswir1
Block (TB). If the state changes (e.g. privilege level), a new TB will
428 998a0501 blueswir1
be generated and the previous TB won't be used anymore until the state
429 998a0501 blueswir1
matches the state recorded in the previous TB. For example, if the SS,
430 998a0501 blueswir1
DS and ES segments have a zero base, then the translator does not even
431 998a0501 blueswir1
generate an addition for the segment base.
432 1f673135 bellard
433 1f673135 bellard
[The FPU stack pointer register is not handled that way yet].
434 1f673135 bellard
435 debc7065 bellard
@node Translation cache
436 1f673135 bellard
@section Translation cache
437 1f673135 bellard
438 15a34c63 bellard
A 16 MByte cache holds the most recently used translations. For
439 1f673135 bellard
simplicity, it is completely flushed when it is full. A translation unit
440 1f673135 bellard
contains just a single basic block (a block of x86 instructions
441 1f673135 bellard
terminated by a jump or by a virtual CPU state change which the
442 1f673135 bellard
translator cannot deduce statically).
443 1f673135 bellard
444 debc7065 bellard
@node Direct block chaining
445 1f673135 bellard
@section Direct block chaining
446 1f673135 bellard
447 1f673135 bellard
After each translated basic block is executed, QEMU uses the simulated
448 1f673135 bellard
Program Counter (PC) and other cpu state informations (such as the CS
449 1f673135 bellard
segment base value) to find the next basic block.
450 1f673135 bellard
451 1f673135 bellard
In order to accelerate the most common cases where the new simulated PC
452 1f673135 bellard
is known, QEMU can patch a basic block so that it jumps directly to the
453 1f673135 bellard
next one.
454 1f673135 bellard
455 1f673135 bellard
The most portable code uses an indirect jump. An indirect jump makes
456 1f673135 bellard
it easier to make the jump target modification atomic. On some host
457 1f673135 bellard
architectures (such as x86 or PowerPC), the @code{JUMP} opcode is
458 1f673135 bellard
directly patched so that the block chaining has no overhead.
459 1f673135 bellard
460 debc7065 bellard
@node Self-modifying code and translated code invalidation
461 1f673135 bellard
@section Self-modifying code and translated code invalidation
462 1f673135 bellard
463 1f673135 bellard
Self-modifying code is a special challenge in x86 emulation because no
464 1f673135 bellard
instruction cache invalidation is signaled by the application when code
465 1f673135 bellard
is modified.
466 1f673135 bellard
467 1f673135 bellard
When translated code is generated for a basic block, the corresponding
468 998a0501 blueswir1
host page is write protected if it is not already read-only. Then, if
469 998a0501 blueswir1
a write access is done to the page, Linux raises a SEGV signal. QEMU
470 998a0501 blueswir1
then invalidates all the translated code in the page and enables write
471 998a0501 blueswir1
accesses to the page.
472 1f673135 bellard
473 1f673135 bellard
Correct translated code invalidation is done efficiently by maintaining
474 1f673135 bellard
a linked list of every translated block contained in a given page. Other
475 5fafdf24 ths
linked lists are also maintained to undo direct block chaining.
476 1f673135 bellard
477 998a0501 blueswir1
On RISC targets, correctly written software uses memory barriers and
478 998a0501 blueswir1
cache flushes, so some of the protection above would not be
479 998a0501 blueswir1
necessary. However, QEMU still requires that the generated code always
480 998a0501 blueswir1
matches the target instructions in memory in order to handle
481 998a0501 blueswir1
exceptions correctly.
482 1f673135 bellard
483 debc7065 bellard
@node Exception support
484 1f673135 bellard
@section Exception support
485 1f673135 bellard
486 1f673135 bellard
longjmp() is used when an exception such as division by zero is
487 5fafdf24 ths
encountered.
488 1f673135 bellard
489 1f673135 bellard
The host SIGSEGV and SIGBUS signal handlers are used to get invalid
490 998a0501 blueswir1
memory accesses. The simulated program counter is found by
491 998a0501 blueswir1
retranslating the corresponding basic block and by looking where the
492 998a0501 blueswir1
host program counter was at the exception point.
493 1f673135 bellard
494 1f673135 bellard
The virtual CPU cannot retrieve the exact @code{EFLAGS} register because
495 1f673135 bellard
in some cases it is not computed because of condition code
496 1f673135 bellard
optimisations. It is not a big concern because the emulated code can
497 1f673135 bellard
still be restarted in any cases.
498 1f673135 bellard
499 debc7065 bellard
@node MMU emulation
500 1f673135 bellard
@section MMU emulation
501 1f673135 bellard
502 998a0501 blueswir1
For system emulation QEMU supports a soft MMU. In that mode, the MMU
503 998a0501 blueswir1
virtual to physical address translation is done at every memory
504 998a0501 blueswir1
access. QEMU uses an address translation cache to speed up the
505 998a0501 blueswir1
translation.
506 1f673135 bellard
507 1f673135 bellard
In order to avoid flushing the translated code each time the MMU
508 1f673135 bellard
mappings change, QEMU uses a physically indexed translation cache. It
509 5fafdf24 ths
means that each basic block is indexed with its physical address.
510 1f673135 bellard
511 1f673135 bellard
When MMU mappings change, only the chaining of the basic blocks is
512 1f673135 bellard
reset (i.e. a basic block can no longer jump directly to another one).
513 1f673135 bellard
514 998a0501 blueswir1
@node Device emulation
515 998a0501 blueswir1
@section Device emulation
516 998a0501 blueswir1
517 998a0501 blueswir1
Systems emulated by QEMU are organized by boards. At initialization
518 998a0501 blueswir1
phase, each board instantiates a number of CPUs, devices, RAM and
519 998a0501 blueswir1
ROM. Each device in turn can assign I/O ports or memory areas (for
520 998a0501 blueswir1
MMIO) to its handlers. When the emulation starts, an access to the
521 998a0501 blueswir1
ports or MMIO memory areas assigned to the device causes the
522 998a0501 blueswir1
corresponding handler to be called.
523 998a0501 blueswir1
524 998a0501 blueswir1
RAM and ROM are handled more optimally, only the offset to the host
525 998a0501 blueswir1
memory needs to be added to the guest address.
526 998a0501 blueswir1
527 998a0501 blueswir1
The video RAM of VGA and other display cards is special: it can be
528 998a0501 blueswir1
read or written directly like RAM, but write accesses cause the memory
529 998a0501 blueswir1
to be marked with VGA_DIRTY flag as well.
530 998a0501 blueswir1
531 998a0501 blueswir1
QEMU supports some device classes like serial and parallel ports, USB,
532 998a0501 blueswir1
drives and network devices, by providing APIs for easier connection to
533 998a0501 blueswir1
the generic, higher level implementations. The API hides the
534 998a0501 blueswir1
implementation details from the devices, like native device use or
535 998a0501 blueswir1
advanced block device formats like QCOW.
536 998a0501 blueswir1
537 998a0501 blueswir1
Usually the devices implement a reset method and register support for
538 998a0501 blueswir1
saving and loading of the device state. The devices can also use
539 998a0501 blueswir1
timers, especially together with the use of bottom halves (BHs).
540 998a0501 blueswir1
541 debc7065 bellard
@node Hardware interrupts
542 1f673135 bellard
@section Hardware interrupts
543 1f673135 bellard
544 1f673135 bellard
In order to be faster, QEMU does not check at every basic block if an
545 e8dc0938 Stefan Weil
hardware interrupt is pending. Instead, the user must asynchronously
546 1f673135 bellard
call a specific function to tell that an interrupt is pending. This
547 1f673135 bellard
function resets the chaining of the currently executing basic
548 1f673135 bellard
block. It ensures that the execution will return soon in the main loop
549 1f673135 bellard
of the CPU emulator. Then the main loop can test if the interrupt is
550 1f673135 bellard
pending and handle it.
551 1f673135 bellard
552 debc7065 bellard
@node User emulation specific details
553 1f673135 bellard
@section User emulation specific details
554 1f673135 bellard
555 1f673135 bellard
@subsection Linux system call translation
556 1f673135 bellard
557 1f673135 bellard
QEMU includes a generic system call translator for Linux. It means that
558 1f673135 bellard
the parameters of the system calls can be converted to fix the
559 1f673135 bellard
endianness and 32/64 bit issues. The IOCTLs are converted with a generic
560 1f673135 bellard
type description system (see @file{ioctls.h} and @file{thunk.c}).
561 1f673135 bellard
562 1f673135 bellard
QEMU supports host CPUs which have pages bigger than 4KB. It records all
563 1f673135 bellard
the mappings the process does and try to emulated the @code{mmap()}
564 1f673135 bellard
system calls in cases where the host @code{mmap()} call would fail
565 1f673135 bellard
because of bad page alignment.
566 1f673135 bellard
567 1f673135 bellard
@subsection Linux signals
568 1f673135 bellard
569 1f673135 bellard
Normal and real-time signals are queued along with their information
570 1f673135 bellard
(@code{siginfo_t}) as it is done in the Linux kernel. Then an interrupt
571 1f673135 bellard
request is done to the virtual CPU. When it is interrupted, one queued
572 1f673135 bellard
signal is handled by generating a stack frame in the virtual CPU as the
573 1f673135 bellard
Linux kernel does. The @code{sigreturn()} system call is emulated to return
574 1f673135 bellard
from the virtual signal handler.
575 1f673135 bellard
576 1f673135 bellard
Some signals (such as SIGALRM) directly come from the host. Other
577 e8dc0938 Stefan Weil
signals are synthesized from the virtual CPU exceptions such as SIGFPE
578 1f673135 bellard
when a division by zero is done (see @code{main.c:cpu_loop()}).
579 1f673135 bellard
580 1f673135 bellard
The blocked signal mask is still handled by the host Linux kernel so
581 1f673135 bellard
that most signal system calls can be redirected directly to the host
582 1f673135 bellard
Linux kernel. Only the @code{sigaction()} and @code{sigreturn()} system
583 1f673135 bellard
calls need to be fully emulated (see @file{signal.c}).
584 1f673135 bellard
585 1f673135 bellard
@subsection clone() system call and threads
586 1f673135 bellard
587 1f673135 bellard
The Linux clone() system call is usually used to create a thread. QEMU
588 1f673135 bellard
uses the host clone() system call so that real host threads are created
589 1f673135 bellard
for each emulated thread. One virtual CPU instance is created for each
590 1f673135 bellard
thread.
591 1f673135 bellard
592 1f673135 bellard
The virtual x86 CPU atomic operations are emulated with a global lock so
593 1f673135 bellard
that their semantic is preserved.
594 1f673135 bellard
595 1f673135 bellard
Note that currently there are still some locking issues in QEMU. In
596 1f673135 bellard
particular, the translated cache flush is not protected yet against
597 1f673135 bellard
reentrancy.
598 1f673135 bellard
599 1f673135 bellard
@subsection Self-virtualization
600 1f673135 bellard
601 1f673135 bellard
QEMU was conceived so that ultimately it can emulate itself. Although
602 1f673135 bellard
it is not very useful, it is an important test to show the power of the
603 1f673135 bellard
emulator.
604 1f673135 bellard
605 1f673135 bellard
Achieving self-virtualization is not easy because there may be address
606 998a0501 blueswir1
space conflicts. QEMU user emulators solve this problem by being an
607 998a0501 blueswir1
executable ELF shared object as the ld-linux.so ELF interpreter. That
608 998a0501 blueswir1
way, it can be relocated at load time.
609 1f673135 bellard
610 debc7065 bellard
@node Bibliography
611 1f673135 bellard
@section Bibliography
612 1f673135 bellard
613 1f673135 bellard
@table @asis
614 1f673135 bellard
615 5fafdf24 ths
@item [1]
616 1f673135 bellard
@url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing
617 1f673135 bellard
direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio
618 1f673135 bellard
Riccardi.
619 1f673135 bellard
620 1f673135 bellard
@item [2]
621 1f673135 bellard
@url{http://developer.kde.org/~sewardj/}, Valgrind, an open-source
622 1f673135 bellard
memory debugger for x86-GNU/Linux, by Julian Seward.
623 1f673135 bellard
624 1f673135 bellard
@item [3]
625 1f673135 bellard
@url{http://bochs.sourceforge.net/}, the Bochs IA-32 Emulator Project,
626 1f673135 bellard
by Kevin Lawton et al.
627 1f673135 bellard
628 1f673135 bellard
@item [4]
629 1f673135 bellard
@url{http://www.cs.rose-hulman.edu/~donaldlf/em86/index.html}, the EM86
630 1f673135 bellard
x86 emulator on Alpha-Linux.
631 1f673135 bellard
632 1f673135 bellard
@item [5]
633 debc7065 bellard
@url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/@/full_papers/chernoff/chernoff.pdf},
634 1f673135 bellard
DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton
635 1f673135 bellard
Chernoff and Ray Hookway.
636 1f673135 bellard
637 1f673135 bellard
@item [6]
638 1f673135 bellard
@url{http://www.willows.com/}, Windows API library emulation from
639 1f673135 bellard
Willows Software.
640 1f673135 bellard
641 1f673135 bellard
@item [7]
642 5fafdf24 ths
@url{http://user-mode-linux.sourceforge.net/},
643 1f673135 bellard
The User-mode Linux Kernel.
644 1f673135 bellard
645 1f673135 bellard
@item [8]
646 5fafdf24 ths
@url{http://www.plex86.org/},
647 1f673135 bellard
The new Plex86 project.
648 1f673135 bellard
649 1f673135 bellard
@item [9]
650 5fafdf24 ths
@url{http://www.vmware.com/},
651 1f673135 bellard
The VMWare PC virtualizer.
652 1f673135 bellard
653 1f673135 bellard
@item [10]
654 5fafdf24 ths
@url{http://www.microsoft.com/windowsxp/virtualpc/},
655 1f673135 bellard
The VirtualPC PC virtualizer.
656 1f673135 bellard
657 1f673135 bellard
@item [11]
658 5fafdf24 ths
@url{http://www.twoostwo.org/},
659 1f673135 bellard
The TwoOStwo PC virtualizer.
660 1f673135 bellard
661 998a0501 blueswir1
@item [12]
662 998a0501 blueswir1
@url{http://virtualbox.org/},
663 998a0501 blueswir1
The VirtualBox PC virtualizer.
664 998a0501 blueswir1
665 998a0501 blueswir1
@item [13]
666 998a0501 blueswir1
@url{http://www.xen.org/},
667 998a0501 blueswir1
The Xen hypervisor.
668 998a0501 blueswir1
669 998a0501 blueswir1
@item [14]
670 998a0501 blueswir1
@url{http://kvm.qumranet.com/kvmwiki/Front_Page},
671 998a0501 blueswir1
Kernel Based Virtual Machine (KVM).
672 998a0501 blueswir1
673 998a0501 blueswir1
@item [15]
674 998a0501 blueswir1
@url{http://www.greensocs.com/projects/QEMUSystemC},
675 998a0501 blueswir1
QEMU-SystemC, a hardware co-simulator.
676 998a0501 blueswir1
677 1f673135 bellard
@end table
678 1f673135 bellard
679 debc7065 bellard
@node Regression Tests
680 1f673135 bellard
@chapter Regression Tests
681 1f673135 bellard
682 1f673135 bellard
In the directory @file{tests/}, various interesting testing programs
683 b1f45238 ths
are available. They are used for regression testing.
684 1f673135 bellard
685 debc7065 bellard
@menu
686 debc7065 bellard
* test-i386::
687 debc7065 bellard
* linux-test::
688 debc7065 bellard
* qruncom.c::
689 debc7065 bellard
@end menu
690 debc7065 bellard
691 debc7065 bellard
@node test-i386
692 1f673135 bellard
@section @file{test-i386}
693 1f673135 bellard
694 1f673135 bellard
This program executes most of the 16 bit and 32 bit x86 instructions and
695 1f673135 bellard
generates a text output. It can be compared with the output obtained with
696 1f673135 bellard
a real CPU or another emulator. The target @code{make test} runs this
697 1f673135 bellard
program and a @code{diff} on the generated output.
698 1f673135 bellard
699 1f673135 bellard
The Linux system call @code{modify_ldt()} is used to create x86 selectors
700 1f673135 bellard
to test some 16 bit addressing and 32 bit with segmentation cases.
701 1f673135 bellard
702 1f673135 bellard
The Linux system call @code{vm86()} is used to test vm86 emulation.
703 1f673135 bellard
704 1f673135 bellard
Various exceptions are raised to test most of the x86 user space
705 1f673135 bellard
exception reporting.
706 1f673135 bellard
707 debc7065 bellard
@node linux-test
708 1f673135 bellard
@section @file{linux-test}
709 1f673135 bellard
710 1f673135 bellard
This program tests various Linux system calls. It is used to verify
711 1f673135 bellard
that the system call parameters are correctly converted between target
712 1f673135 bellard
and host CPUs.
713 1f673135 bellard
714 debc7065 bellard
@node qruncom.c
715 15a34c63 bellard
@section @file{qruncom.c}
716 1f673135 bellard
717 15a34c63 bellard
Example of usage of @code{libqemu} to emulate a user mode i386 CPU.
718 debc7065 bellard
719 debc7065 bellard
@node Index
720 debc7065 bellard
@chapter Index
721 debc7065 bellard
@printindex cp
722 debc7065 bellard
723 debc7065 bellard
@bye