blob: fc72ff1b697436ec51fdd96cef32cab93c25c5c6 [file] [log] [blame]
bellard1f673132004-04-04 15:21:17 +00001\input texinfo @c -*- texinfo -*-
bellarddebc7062006-04-30 21:58:41 +00002@c %**start of header
3@setfilename qemu-tech.info
4@settitle QEMU Internals
5@exampleindent 0
6@paragraphindent 0
7@c %**end of header
bellard1f673132004-04-04 15:21:17 +00008
9@iftex
bellard1f673132004-04-04 15:21:17 +000010@titlepage
11@sp 7
12@center @titlefont{QEMU Internals}
13@sp 3
14@end titlepage
15@end iftex
16
bellarddebc7062006-04-30 21:58:41 +000017@ifnottex
18@node Top
19@top
20
21@menu
22* Introduction::
23* QEMU Internals::
24* Regression Tests::
25* Index::
26@end menu
27@end ifnottex
28
29@contents
30
31@node Introduction
bellard1f673132004-04-04 15:21:17 +000032@chapter Introduction
33
bellarddebc7062006-04-30 21:58:41 +000034@menu
35* intro_features:: Features
36* intro_x86_emulation:: x86 emulation
37* intro_arm_emulation:: ARM emulation
38* intro_ppc_emulation:: PowerPC emulation
39* intro_sparc_emulation:: SPARC emulation
40@end menu
41
42@node intro_features
bellard1f673132004-04-04 15:21:17 +000043@section Features
44
45QEMU is a FAST! processor emulator using a portable dynamic
46translator.
47
48QEMU has two operating modes:
49
50@itemize @minus
51
52@item
53Full system emulation. In this mode, QEMU emulates a full system
bellardb671f9e2005-04-30 15:08:33 +000054(usually a PC), including a processor and various peripherals. It can
bellard1f673132004-04-04 15:21:17 +000055be used to launch an different Operating System without rebooting the
56PC or to debug system code.
57
58@item
59User mode emulation (Linux host only). In this mode, QEMU can launch
60Linux processes compiled for one CPU on another CPU. It can be used to
61launch the Wine Windows API emulator (@url{http://www.winehq.org}) or
62to ease cross-compilation and cross-debugging.
63
64@end itemize
65
66As QEMU requires no host kernel driver to run, it is very safe and
67easy to use.
68
69QEMU generic features:
70
71@itemize
72
73@item User space only or full system emulation.
74
bellarddebc7062006-04-30 21:58:41 +000075@item Using dynamic translation to native code for reasonable speed.
bellard1f673132004-04-04 15:21:17 +000076
77@item Working on x86 and PowerPC hosts. Being tested on ARM, Sparc32, Alpha and S390.
78
79@item Self-modifying code support.
80
81@item Precise exceptions support.
82
83@item The virtual CPU is a library (@code{libqemu}) which can be used
bellardad6a4832004-04-24 00:16:28 +000084in other projects (look at @file{qemu/tests/qruncom.c} to have an
85example of user mode @code{libqemu} usage).
bellard1f673132004-04-04 15:21:17 +000086
87@end itemize
88
89QEMU user mode emulation features:
90@itemize
91@item Generic Linux system call converter, including most ioctls.
92
93@item clone() emulation using native CPU clone() to use Linux scheduler for threads.
94
95@item Accurate signal handling by remapping host signals to target signals.
96@end itemize
bellard1f673132004-04-04 15:21:17 +000097
98QEMU full system emulation features:
99@itemize
100@item QEMU can either use a full software MMU for maximum portability or use the host system call mmap() to simulate the target MMU.
101@end itemize
102
bellarddebc7062006-04-30 21:58:41 +0000103@node intro_x86_emulation
bellard1f673132004-04-04 15:21:17 +0000104@section x86 emulation
105
106QEMU x86 target features:
107
108@itemize
109
110@item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation.
111LDT/GDT and IDT are emulated. VM86 mode is also supported to run DOSEMU.
112
113@item Support of host page sizes bigger than 4KB in user mode emulation.
114
115@item QEMU can emulate itself on x86.
116
117@item An extensive Linux x86 CPU test program is included @file{tests/test-i386}.
118It can be used to test other x86 virtual CPUs.
119
120@end itemize
121
122Current QEMU limitations:
123
124@itemize
125
126@item No SSE/MMX support (yet).
127
128@item No x86-64 support.
129
130@item IPC syscalls are missing.
131
132@item The x86 segment limits and access rights are not tested at every
133memory access (yet). Hopefully, very few OSes seem to rely on that for
134normal use.
135
136@item On non x86 host CPUs, @code{double}s are used instead of the non standard
13710 byte @code{long double}s of x86 for floating point emulation to get
138maximum performances.
139
140@end itemize
141
bellarddebc7062006-04-30 21:58:41 +0000142@node intro_arm_emulation
bellard1f673132004-04-04 15:21:17 +0000143@section ARM emulation
144
145@itemize
146
147@item Full ARM 7 user emulation.
148
149@item NWFPE FPU support included in user Linux emulation.
150
151@item Can run most ARM Linux binaries.
152
153@end itemize
154
bellarddebc7062006-04-30 21:58:41 +0000155@node intro_ppc_emulation
bellard1f673132004-04-04 15:21:17 +0000156@section PowerPC emulation
157
158@itemize
159
bellarde80cfcf2004-12-19 23:18:01 +0000160@item Full PowerPC 32 bit emulation, including privileged instructions,
bellard1f673132004-04-04 15:21:17 +0000161FPU and MMU.
162
163@item Can run most PowerPC Linux binaries.
164
165@end itemize
166
bellarddebc7062006-04-30 21:58:41 +0000167@node intro_sparc_emulation
bellard1f673132004-04-04 15:21:17 +0000168@section SPARC emulation
169
170@itemize
171
blueswir1f6b647c2007-04-05 18:40:23 +0000172@item Full SPARC V8 emulation, including privileged
bellard34751872005-07-02 14:31:34 +0000173instructions, FPU and MMU. SPARC V9 emulation includes most privileged
blueswir1f6b647c2007-04-05 18:40:23 +0000174instructions, FPU and I/D MMU, but misses most VIS instructions.
bellard1f673132004-04-04 15:21:17 +0000175
blueswir1f6b647c2007-04-05 18:40:23 +0000176@item Can run most 32-bit SPARC Linux binaries and some handcrafted 64-bit SPARC Linux binaries.
bellard34751872005-07-02 14:31:34 +0000177
178@end itemize
179
180Current QEMU limitations:
181
182@itemize
183
bellard34751872005-07-02 14:31:34 +0000184@item IPC syscalls are missing.
185
186@item 128-bit floating point operations are not supported, though none of the
187real CPUs implement them either. FCMPE[SD] are not correctly
188implemented. Floating point exception support is untested.
189
190@item Alignment is not enforced at all.
191
192@item Atomic instructions are not correctly implemented.
193
194@item Sparc64 emulators are not usable for anything yet.
bellard1f673132004-04-04 15:21:17 +0000195
196@end itemize
197
bellarddebc7062006-04-30 21:58:41 +0000198@node QEMU Internals
bellard1f673132004-04-04 15:21:17 +0000199@chapter QEMU Internals
200
bellarddebc7062006-04-30 21:58:41 +0000201@menu
202* QEMU compared to other emulators::
203* Portable dynamic translation::
204* Register allocation::
205* Condition code optimisations::
206* CPU state optimisations::
207* Translation cache::
208* Direct block chaining::
209* Self-modifying code and translated code invalidation::
210* Exception support::
211* MMU emulation::
212* Hardware interrupts::
213* User emulation specific details::
214* Bibliography::
215@end menu
216
217@node QEMU compared to other emulators
bellard1f673132004-04-04 15:21:17 +0000218@section QEMU compared to other emulators
219
220Like bochs [3], QEMU emulates an x86 CPU. But QEMU is much faster than
221bochs as it uses dynamic compilation. Bochs is closely tied to x86 PC
222emulation while QEMU can emulate several processors.
223
224Like Valgrind [2], QEMU does user space emulation and dynamic
225translation. Valgrind is mainly a memory debugger while QEMU has no
226support for it (QEMU could be used to detect out of bound memory
227accesses as Valgrind, but it has no support to track uninitialised data
228as Valgrind does). The Valgrind dynamic translator generates better code
229than QEMU (in particular it does register allocation) but it is closely
230tied to an x86 host and target and has no support for precise exceptions
231and system emulation.
232
233EM86 [4] is the closest project to user space QEMU (and QEMU still uses
234some of its code, in particular the ELF file loader). EM86 was limited
235to an alpha host and used a proprietary and slow interpreter (the
236interpreter part of the FX!32 Digital Win32 code translator [5]).
237
238TWIN [6] is a Windows API emulator like Wine. It is less accurate than
239Wine but includes a protected mode x86 interpreter to launch x86 Windows
bellard36d54d12004-09-05 16:04:16 +0000240executables. Such an approach has greater potential because most of the
bellard1f673132004-04-04 15:21:17 +0000241Windows API is executed natively but it is far more difficult to develop
242because all the data structures and function parameters exchanged
243between the API and the x86 code must be converted.
244
245User mode Linux [7] was the only solution before QEMU to launch a
246Linux kernel as a process while not needing any host kernel
247patches. However, user mode Linux requires heavy kernel patches while
248QEMU accepts unpatched Linux kernels. The price to pay is that QEMU is
249slower.
250
251The new Plex86 [8] PC virtualizer is done in the same spirit as the
252qemu-fast system emulator. It requires a patched Linux kernel to work
253(you cannot launch the same kernel on your PC), but the patches are
254really small. As it is a PC virtualizer (no emulation is done except
255for some priveledged instructions), it has the potential of being
256faster than QEMU. The downside is that a complicated (and potentially
257unsafe) host kernel patch is needed.
258
259The commercial PC Virtualizers (VMWare [9], VirtualPC [10], TwoOStwo
260[11]) are faster than QEMU, but they all need specific, proprietary
261and potentially unsafe host drivers. Moreover, they are unable to
262provide cycle exact simulation as an emulator can.
263
bellarddebc7062006-04-30 21:58:41 +0000264@node Portable dynamic translation
bellard1f673132004-04-04 15:21:17 +0000265@section Portable dynamic translation
266
267QEMU is a dynamic translator. When it first encounters a piece of code,
268it converts it to the host instruction set. Usually dynamic translators
269are very complicated and highly CPU dependent. QEMU uses some tricks
270which make it relatively easily portable and simple while achieving good
271performances.
272
273The basic idea is to split every x86 instruction into fewer simpler
274instructions. Each simple instruction is implemented by a piece of C
275code (see @file{target-i386/op.c}). Then a compile time tool
276(@file{dyngen}) takes the corresponding object file (@file{op.o})
277to generate a dynamic code generator which concatenates the simple
278instructions to build a function (see @file{op.h:dyngen_code()}).
279
280In essence, the process is similar to [1], but more work is done at
281compile time.
282
283A key idea to get optimal performances is that constant parameters can
284be passed to the simple operations. For that purpose, dummy ELF
285relocations are generated with gcc for each constant parameter. Then,
286the tool (@file{dyngen}) can locate the relocations and generate the
287appriopriate C code to resolve them when building the dynamic code.
288
289That way, QEMU is no more difficult to port than a dynamic linker.
290
291To go even faster, GCC static register variables are used to keep the
292state of the virtual CPU.
293
bellarddebc7062006-04-30 21:58:41 +0000294@node Register allocation
bellard1f673132004-04-04 15:21:17 +0000295@section Register allocation
296
297Since QEMU uses fixed simple instructions, no efficient register
298allocation can be done. However, because RISC CPUs have a lot of
299register, most of the virtual CPU state can be put in registers without
300doing complicated register allocation.
301
bellarddebc7062006-04-30 21:58:41 +0000302@node Condition code optimisations
bellard1f673132004-04-04 15:21:17 +0000303@section Condition code optimisations
304
305Good CPU condition codes emulation (@code{EFLAGS} register on x86) is a
306critical point to get good performances. QEMU uses lazy condition code
307evaluation: instead of computing the condition codes after each x86
308instruction, it just stores one operand (called @code{CC_SRC}), the
309result (called @code{CC_DST}) and the type of operation (called
310@code{CC_OP}).
311
312@code{CC_OP} is almost never explicitely set in the generated code
313because it is known at translation time.
314
315In order to increase performances, a backward pass is performed on the
316generated simple instructions (see
317@code{target-i386/translate.c:optimize_flags()}). When it can be proved that
318the condition codes are not needed by the next instructions, no
319condition codes are computed at all.
320
bellarddebc7062006-04-30 21:58:41 +0000321@node CPU state optimisations
bellard1f673132004-04-04 15:21:17 +0000322@section CPU state optimisations
323
324The x86 CPU has many internal states which change the way it evaluates
325instructions. In order to achieve a good speed, the translation phase
326considers that some state information of the virtual x86 CPU cannot
327change in it. For example, if the SS, DS and ES segments have a zero
328base, then the translator does not even generate an addition for the
329segment base.
330
331[The FPU stack pointer register is not handled that way yet].
332
bellarddebc7062006-04-30 21:58:41 +0000333@node Translation cache
bellard1f673132004-04-04 15:21:17 +0000334@section Translation cache
335
bellard15a34c62004-07-08 21:26:26 +0000336A 16 MByte cache holds the most recently used translations. For
bellard1f673132004-04-04 15:21:17 +0000337simplicity, it is completely flushed when it is full. A translation unit
338contains just a single basic block (a block of x86 instructions
339terminated by a jump or by a virtual CPU state change which the
340translator cannot deduce statically).
341
bellarddebc7062006-04-30 21:58:41 +0000342@node Direct block chaining
bellard1f673132004-04-04 15:21:17 +0000343@section Direct block chaining
344
345After each translated basic block is executed, QEMU uses the simulated
346Program Counter (PC) and other cpu state informations (such as the CS
347segment base value) to find the next basic block.
348
349In order to accelerate the most common cases where the new simulated PC
350is known, QEMU can patch a basic block so that it jumps directly to the
351next one.
352
353The most portable code uses an indirect jump. An indirect jump makes
354it easier to make the jump target modification atomic. On some host
355architectures (such as x86 or PowerPC), the @code{JUMP} opcode is
356directly patched so that the block chaining has no overhead.
357
bellarddebc7062006-04-30 21:58:41 +0000358@node Self-modifying code and translated code invalidation
bellard1f673132004-04-04 15:21:17 +0000359@section Self-modifying code and translated code invalidation
360
361Self-modifying code is a special challenge in x86 emulation because no
362instruction cache invalidation is signaled by the application when code
363is modified.
364
365When translated code is generated for a basic block, the corresponding
366host page is write protected if it is not already read-only (with the
367system call @code{mprotect()}). Then, if a write access is done to the
368page, Linux raises a SEGV signal. QEMU then invalidates all the
369translated code in the page and enables write accesses to the page.
370
371Correct translated code invalidation is done efficiently by maintaining
372a linked list of every translated block contained in a given page. Other
373linked lists are also maintained to undo direct block chaining.
374
375Although the overhead of doing @code{mprotect()} calls is important,
376most MSDOS programs can be emulated at reasonnable speed with QEMU and
377DOSEMU.
378
379Note that QEMU also invalidates pages of translated code when it detects
380that memory mappings are modified with @code{mmap()} or @code{munmap()}.
381
382When using a software MMU, the code invalidation is more efficient: if
383a given code page is invalidated too often because of write accesses,
384then a bitmap representing all the code inside the page is
385built. Every store into that page checks the bitmap to see if the code
386really needs to be invalidated. It avoids invalidating the code when
387only data is modified in the page.
388
bellarddebc7062006-04-30 21:58:41 +0000389@node Exception support
bellard1f673132004-04-04 15:21:17 +0000390@section Exception support
391
392longjmp() is used when an exception such as division by zero is
393encountered.
394
395The host SIGSEGV and SIGBUS signal handlers are used to get invalid
396memory accesses. The exact CPU state can be retrieved because all the
397x86 registers are stored in fixed host registers. The simulated program
398counter is found by retranslating the corresponding basic block and by
399looking where the host program counter was at the exception point.
400
401The virtual CPU cannot retrieve the exact @code{EFLAGS} register because
402in some cases it is not computed because of condition code
403optimisations. It is not a big concern because the emulated code can
404still be restarted in any cases.
405
bellarddebc7062006-04-30 21:58:41 +0000406@node MMU emulation
bellard1f673132004-04-04 15:21:17 +0000407@section MMU emulation
408
409For system emulation, QEMU uses the mmap() system call to emulate the
410target CPU MMU. It works as long the emulated OS does not use an area
411reserved by the host OS (such as the area above 0xc0000000 on x86
412Linux).
413
414In order to be able to launch any OS, QEMU also supports a soft
415MMU. In that mode, the MMU virtual to physical address translation is
416done at every memory access. QEMU uses an address translation cache to
417speed up the translation.
418
419In order to avoid flushing the translated code each time the MMU
420mappings change, QEMU uses a physically indexed translation cache. It
421means that each basic block is indexed with its physical address.
422
423When MMU mappings change, only the chaining of the basic blocks is
424reset (i.e. a basic block can no longer jump directly to another one).
425
bellarddebc7062006-04-30 21:58:41 +0000426@node Hardware interrupts
bellard1f673132004-04-04 15:21:17 +0000427@section Hardware interrupts
428
429In order to be faster, QEMU does not check at every basic block if an
430hardware interrupt is pending. Instead, the user must asynchrously
431call a specific function to tell that an interrupt is pending. This
432function resets the chaining of the currently executing basic
433block. It ensures that the execution will return soon in the main loop
434of the CPU emulator. Then the main loop can test if the interrupt is
435pending and handle it.
436
bellarddebc7062006-04-30 21:58:41 +0000437@node User emulation specific details
bellard1f673132004-04-04 15:21:17 +0000438@section User emulation specific details
439
440@subsection Linux system call translation
441
442QEMU includes a generic system call translator for Linux. It means that
443the parameters of the system calls can be converted to fix the
444endianness and 32/64 bit issues. The IOCTLs are converted with a generic
445type description system (see @file{ioctls.h} and @file{thunk.c}).
446
447QEMU supports host CPUs which have pages bigger than 4KB. It records all
448the mappings the process does and try to emulated the @code{mmap()}
449system calls in cases where the host @code{mmap()} call would fail
450because of bad page alignment.
451
452@subsection Linux signals
453
454Normal and real-time signals are queued along with their information
455(@code{siginfo_t}) as it is done in the Linux kernel. Then an interrupt
456request is done to the virtual CPU. When it is interrupted, one queued
457signal is handled by generating a stack frame in the virtual CPU as the
458Linux kernel does. The @code{sigreturn()} system call is emulated to return
459from the virtual signal handler.
460
461Some signals (such as SIGALRM) directly come from the host. Other
462signals are synthetized from the virtual CPU exceptions such as SIGFPE
463when a division by zero is done (see @code{main.c:cpu_loop()}).
464
465The blocked signal mask is still handled by the host Linux kernel so
466that most signal system calls can be redirected directly to the host
467Linux kernel. Only the @code{sigaction()} and @code{sigreturn()} system
468calls need to be fully emulated (see @file{signal.c}).
469
470@subsection clone() system call and threads
471
472The Linux clone() system call is usually used to create a thread. QEMU
473uses the host clone() system call so that real host threads are created
474for each emulated thread. One virtual CPU instance is created for each
475thread.
476
477The virtual x86 CPU atomic operations are emulated with a global lock so
478that their semantic is preserved.
479
480Note that currently there are still some locking issues in QEMU. In
481particular, the translated cache flush is not protected yet against
482reentrancy.
483
484@subsection Self-virtualization
485
486QEMU was conceived so that ultimately it can emulate itself. Although
487it is not very useful, it is an important test to show the power of the
488emulator.
489
490Achieving self-virtualization is not easy because there may be address
491space conflicts. QEMU solves this problem by being an executable ELF
492shared object as the ld-linux.so ELF interpreter. That way, it can be
493relocated at load time.
494
bellarddebc7062006-04-30 21:58:41 +0000495@node Bibliography
bellard1f673132004-04-04 15:21:17 +0000496@section Bibliography
497
498@table @asis
499
500@item [1]
501@url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing
502direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio
503Riccardi.
504
505@item [2]
506@url{http://developer.kde.org/~sewardj/}, Valgrind, an open-source
507memory debugger for x86-GNU/Linux, by Julian Seward.
508
509@item [3]
510@url{http://bochs.sourceforge.net/}, the Bochs IA-32 Emulator Project,
511by Kevin Lawton et al.
512
513@item [4]
514@url{http://www.cs.rose-hulman.edu/~donaldlf/em86/index.html}, the EM86
515x86 emulator on Alpha-Linux.
516
517@item [5]
bellarddebc7062006-04-30 21:58:41 +0000518@url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/@/full_papers/chernoff/chernoff.pdf},
bellard1f673132004-04-04 15:21:17 +0000519DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton
520Chernoff and Ray Hookway.
521
522@item [6]
523@url{http://www.willows.com/}, Windows API library emulation from
524Willows Software.
525
526@item [7]
527@url{http://user-mode-linux.sourceforge.net/},
528The User-mode Linux Kernel.
529
530@item [8]
531@url{http://www.plex86.org/},
532The new Plex86 project.
533
534@item [9]
535@url{http://www.vmware.com/},
536The VMWare PC virtualizer.
537
538@item [10]
539@url{http://www.microsoft.com/windowsxp/virtualpc/},
540The VirtualPC PC virtualizer.
541
542@item [11]
543@url{http://www.twoostwo.org/},
544The TwoOStwo PC virtualizer.
545
546@end table
547
bellarddebc7062006-04-30 21:58:41 +0000548@node Regression Tests
bellard1f673132004-04-04 15:21:17 +0000549@chapter Regression Tests
550
551In the directory @file{tests/}, various interesting testing programs
552are available. There are used for regression testing.
553
bellarddebc7062006-04-30 21:58:41 +0000554@menu
555* test-i386::
556* linux-test::
557* qruncom.c::
558@end menu
559
560@node test-i386
bellard1f673132004-04-04 15:21:17 +0000561@section @file{test-i386}
562
563This program executes most of the 16 bit and 32 bit x86 instructions and
564generates a text output. It can be compared with the output obtained with
565a real CPU or another emulator. The target @code{make test} runs this
566program and a @code{diff} on the generated output.
567
568The Linux system call @code{modify_ldt()} is used to create x86 selectors
569to test some 16 bit addressing and 32 bit with segmentation cases.
570
571The Linux system call @code{vm86()} is used to test vm86 emulation.
572
573Various exceptions are raised to test most of the x86 user space
574exception reporting.
575
bellarddebc7062006-04-30 21:58:41 +0000576@node linux-test
bellard1f673132004-04-04 15:21:17 +0000577@section @file{linux-test}
578
579This program tests various Linux system calls. It is used to verify
580that the system call parameters are correctly converted between target
581and host CPUs.
582
bellarddebc7062006-04-30 21:58:41 +0000583@node qruncom.c
bellard15a34c62004-07-08 21:26:26 +0000584@section @file{qruncom.c}
bellard1f673132004-04-04 15:21:17 +0000585
bellard15a34c62004-07-08 21:26:26 +0000586Example of usage of @code{libqemu} to emulate a user mode i386 CPU.
bellarddebc7062006-04-30 21:58:41 +0000587
588@node Index
589@chapter Index
590@printindex cp
591
592@bye