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