]> git.proxmox.com Git - qemu.git/blame - qemu-doc.texi
gcc3 compile fixes
[qemu.git] / qemu-doc.texi
CommitLineData
386405f7
FB
1\input texinfo @c -*- texinfo -*-
2
322d0c66 3@settitle QEMU CPU Emulator Reference Documentation
386405f7
FB
4@titlepage
5@sp 7
322d0c66 6@center @titlefont{QEMU CPU Emulator Reference Documentation}
386405f7
FB
7@sp 3
8@end titlepage
9
10@chapter Introduction
11
322d0c66 12@section Features
386405f7 13
322d0c66
FB
14QEMU is a FAST! processor emulator. Its purpose is to run Linux executables
15compiled for one architecture on another. For example, x86 Linux
16processes can be ran on PowerPC Linux architectures. By using dynamic
17translation it achieves a reasonnable speed while being easy to port on
18new host CPUs. Its main goal is to be able to launch the @code{Wine}
19Windows API emulator (@url{http://www.winehq.org}) or @code{DOSEMU}
20(@url{http://www.dosemu.org}) on non-x86 CPUs.
21
22QEMU generic features:
386405f7
FB
23
24@itemize
25
322d0c66 26@item User space only emulation.
386405f7 27
322d0c66 28@item Working on x86 and PowerPC hosts. Being tested on ARM, Sparc32, Alpha and S390.
386405f7 29
fd429f2f 30@item Using dynamic translation to native code for reasonnable speed.
386405f7 31
386405f7
FB
32@item Generic Linux system call converter, including most ioctls.
33
34@item clone() emulation using native CPU clone() to use Linux scheduler for threads.
35
322d0c66 36@item Accurate signal handling by remapping host signals to target signals.
df0f11a0
FB
37
38@item Self-modifying code support.
39
322d0c66
FB
40@item The virtual CPU is a library (@code{libqemu}) which can be used
41in other projects.
42
43@end itemize
44
45@section x86 emulation
46
47QEMU x86 target features:
48
49@itemize
50
51@item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation.
52User space LDT and GDT are emulated. VM86 mode is also supported to run DOSEMU.
53
54@item Precise user space x86 exceptions.
55
df0f11a0
FB
56@item Support of host page sizes bigger than 4KB.
57
58@item QEMU can emulate itself on x86.
1eb87257 59
386405f7
FB
60@item An extensive Linux x86 CPU test program is included @file{tests/test-i386}.
61It can be used to test other x86 virtual CPUs.
62
63@end itemize
64
df0f11a0 65Current QEMU limitations:
386405f7
FB
66
67@itemize
68
386405f7
FB
69@item No SSE/MMX support (yet).
70
71@item No x86-64 support.
72
df0f11a0 73@item IPC syscalls are missing.
386405f7
FB
74
75@item The x86 segment limits and access rights are not tested at every
76memory access (and will never be to have good performances).
77
78@item On non x86 host CPUs, @code{double}s are used instead of the non standard
7910 byte @code{long double}s of x86 for floating point emulation to get
80maximum performances.
81
82@end itemize
83
322d0c66
FB
84@section ARM emulation
85
86@itemize
87
88@item ARM emulation can currently launch small programs while using the
89generic dynamic code generation architecture of QEMU.
90
91@item No FPU support (yet).
92
93@item No automatic regression testing (yet).
94
95@end itemize
96
386405f7
FB
97@chapter Invocation
98
d691f669
FB
99@section Quick Start
100
322d0c66
FB
101If you need to compile QEMU, please read the @file{README} which gives
102the related information.
103
386405f7 104In order to launch a Linux process, QEMU needs the process executable
d691f669
FB
105itself and all the target (x86) dynamic libraries used by it.
106
107@itemize
386405f7 108
d691f669
FB
109@item On x86, you can just try to launch any process by using the native
110libraries:
386405f7
FB
111
112@example
d691f669 113qemu -L / /bin/ls
386405f7
FB
114@end example
115
d691f669
FB
116@code{-L /} tells that the x86 dynamic linker must be searched with a
117@file{/} prefix.
386405f7 118
1eb87257
FB
119@item Since QEMU is also a linux process, you can launch qemu with qemu:
120
121@example
122qemu -L / qemu -L / /bin/ls
123@end example
386405f7 124
d691f669 125@item On non x86 CPUs, you need first to download at least an x86 glibc
1eb87257 126(@file{qemu-XXX-i386-glibc21.tar.gz} on the QEMU web page). Ensure that
644c433c
FB
127@code{LD_LIBRARY_PATH} is not set:
128
129@example
130unset LD_LIBRARY_PATH
131@end example
132
133Then you can launch the precompiled @file{ls} x86 executable:
134
d691f669 135@example
168485b7
FB
136qemu /usr/local/qemu-i386/bin/ls-i386
137@end example
138You can look at @file{/usr/local/qemu-i386/bin/qemu-conf.sh} so that
139QEMU is automatically launched by the Linux kernel when you try to
140launch x86 executables. It requires the @code{binfmt_misc} module in the
141Linux kernel.
142
1eb87257
FB
143@item The x86 version of QEMU is also included. You can try weird things such as:
144@example
145qemu /usr/local/qemu-i386/bin/qemu-i386 /usr/local/qemu-i386/bin/ls-i386
146@end example
147
168485b7
FB
148@end itemize
149
df0f11a0 150@section Wine launch
168485b7
FB
151
152@itemize
153
154@item Ensure that you have a working QEMU with the x86 glibc
155distribution (see previous section). In order to verify it, you must be
156able to do:
157
158@example
159qemu /usr/local/qemu-i386/bin/ls-i386
160@end example
161
fd429f2f 162@item Download the binary x86 Wine install
1eb87257 163(@file{qemu-XXX-i386-wine.tar.gz} on the QEMU web page).
168485b7 164
fd429f2f 165@item Configure Wine on your account. Look at the provided script
168485b7
FB
166@file{/usr/local/qemu-i386/bin/wine-conf.sh}. Your previous
167@code{$@{HOME@}/.wine} directory is saved to @code{$@{HOME@}/.wine.org}.
168
169@item Then you can try the example @file{putty.exe}:
170
171@example
172qemu /usr/local/qemu-i386/wine/bin/wine /usr/local/qemu-i386/wine/c/Program\ Files/putty.exe
386405f7 173@end example
d691f669
FB
174
175@end itemize
176
177@section Command line options
178
179@example
180usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]
181@end example
182
df0f11a0 183@table @option
d691f669
FB
184@item -h
185Print the help
d691f669
FB
186@item -L path
187Set the x86 elf interpreter prefix (default=/usr/local/qemu-i386)
188@item -s size
189Set the x86 stack size in bytes (default=524288)
190@end table
386405f7 191
df0f11a0
FB
192Debug options:
193
194@table @option
195@item -d
196Activate log (logfile=/tmp/qemu.log)
197@item -p pagesize
198Act as if the host page size was 'pagesize' bytes
199@end table
200
386405f7
FB
201@chapter QEMU Internals
202
203@section QEMU compared to other emulators
204
205Unlike bochs [3], QEMU emulates only a user space x86 CPU. It means that
206you cannot launch an operating system with it. The benefit is that it is
207simpler and faster due to the fact that some of the low level CPU state
208can be ignored (in particular, no virtual memory needs to be emulated).
209
210Like Valgrind [2], QEMU does user space emulation and dynamic
211translation. Valgrind is mainly a memory debugger while QEMU has no
212support for it (QEMU could be used to detect out of bound memory accesses
213as Valgrind, but it has no support to track uninitialised data as
214Valgrind does). Valgrind dynamic translator generates better code than
215QEMU (in particular it does register allocation) but it is closely tied
322d0c66 216to an x86 host and target.
386405f7
FB
217
218EM86 [4] is the closest project to QEMU (and QEMU still uses some of its
219code, in particular the ELF file loader). EM86 was limited to an alpha
220host and used a proprietary and slow interpreter (the interpreter part
221of the FX!32 Digital Win32 code translator [5]).
222
fd429f2f
FB
223TWIN [6] is a Windows API emulator like Wine. It is less accurate than
224Wine but includes a protected mode x86 interpreter to launch x86 Windows
225executables. Such an approach as greater potential because most of the
226Windows API is executed natively but it is far more difficult to develop
227because all the data structures and function parameters exchanged
228between the API and the x86 code must be converted.
229
386405f7
FB
230@section Portable dynamic translation
231
232QEMU is a dynamic translator. When it first encounters a piece of code,
233it converts it to the host instruction set. Usually dynamic translators
322d0c66 234are very complicated and highly CPU dependent. QEMU uses some tricks
386405f7
FB
235which make it relatively easily portable and simple while achieving good
236performances.
237
238The basic idea is to split every x86 instruction into fewer simpler
239instructions. Each simple instruction is implemented by a piece of C
240code (see @file{op-i386.c}). Then a compile time tool (@file{dyngen})
241takes the corresponding object file (@file{op-i386.o}) to generate a
242dynamic code generator which concatenates the simple instructions to
243build a function (see @file{op-i386.h:dyngen_code()}).
244
245In essence, the process is similar to [1], but more work is done at
246compile time.
247
248A key idea to get optimal performances is that constant parameters can
249be passed to the simple operations. For that purpose, dummy ELF
250relocations are generated with gcc for each constant parameter. Then,
251the tool (@file{dyngen}) can locate the relocations and generate the
252appriopriate C code to resolve them when building the dynamic code.
253
254That way, QEMU is no more difficult to port than a dynamic linker.
255
256To go even faster, GCC static register variables are used to keep the
257state of the virtual CPU.
258
259@section Register allocation
260
261Since QEMU uses fixed simple instructions, no efficient register
262allocation can be done. However, because RISC CPUs have a lot of
263register, most of the virtual CPU state can be put in registers without
264doing complicated register allocation.
265
266@section Condition code optimisations
267
268Good CPU condition codes emulation (@code{EFLAGS} register on x86) is a
269critical point to get good performances. QEMU uses lazy condition code
270evaluation: instead of computing the condition codes after each x86
fd429f2f 271instruction, it just stores one operand (called @code{CC_SRC}), the
386405f7
FB
272result (called @code{CC_DST}) and the type of operation (called
273@code{CC_OP}).
274
275@code{CC_OP} is almost never explicitely set in the generated code
276because it is known at translation time.
277
278In order to increase performances, a backward pass is performed on the
279generated simple instructions (see
280@code{translate-i386.c:optimize_flags()}). When it can be proved that
281the condition codes are not needed by the next instructions, no
282condition codes are computed at all.
283
fd429f2f 284@section CPU state optimisations
386405f7
FB
285
286The x86 CPU has many internal states which change the way it evaluates
287instructions. In order to achieve a good speed, the translation phase
288considers that some state information of the virtual x86 CPU cannot
289change in it. For example, if the SS, DS and ES segments have a zero
290base, then the translator does not even generate an addition for the
291segment base.
292
293[The FPU stack pointer register is not handled that way yet].
294
295@section Translation cache
296
297A 2MByte cache holds the most recently used translations. For
298simplicity, it is completely flushed when it is full. A translation unit
299contains just a single basic block (a block of x86 instructions
300terminated by a jump or by a virtual CPU state change which the
301translator cannot deduce statically).
302
df0f11a0
FB
303@section Direct block chaining
304
305After each translated basic block is executed, QEMU uses the simulated
306Program Counter (PC) and other cpu state informations (such as the CS
307segment base value) to find the next basic block.
308
309In order to accelerate the most common cases where the new simulated PC
310is known, QEMU can patch a basic block so that it jumps directly to the
311next one.
312
313The most portable code uses an indirect jump. An indirect jump makes it
314easier to make the jump target modification atomic. On some
315architectures (such as PowerPC), the @code{JUMP} opcode is directly
316patched so that the block chaining has no overhead.
317
318@section Self-modifying code and translated code invalidation
319
320Self-modifying code is a special challenge in x86 emulation because no
321instruction cache invalidation is signaled by the application when code
322is modified.
323
324When translated code is generated for a basic block, the corresponding
325host page is write protected if it is not already read-only (with the
326system call @code{mprotect()}). Then, if a write access is done to the
327page, Linux raises a SEGV signal. QEMU then invalidates all the
328translated code in the page and enables write accesses to the page.
329
330Correct translated code invalidation is done efficiently by maintaining
331a linked list of every translated block contained in a given page. Other
332linked lists are also maintained to undo direct block chaining.
333
334Althought the overhead of doing @code{mprotect()} calls is important,
335most MSDOS programs can be emulated at reasonnable speed with QEMU and
336DOSEMU.
337
338Note that QEMU also invalidates pages of translated code when it detects
339that memory mappings are modified with @code{mmap()} or @code{munmap()}.
386405f7
FB
340
341@section Exception support
342
343longjmp() is used when an exception such as division by zero is
df0f11a0 344encountered.
386405f7 345
df0f11a0
FB
346The host SIGSEGV and SIGBUS signal handlers are used to get invalid
347memory accesses. The exact CPU state can be retrieved because all the
348x86 registers are stored in fixed host registers. The simulated program
349counter is found by retranslating the corresponding basic block and by
350looking where the host program counter was at the exception point.
351
352The virtual CPU cannot retrieve the exact @code{EFLAGS} register because
353in some cases it is not computed because of condition code
354optimisations. It is not a big concern because the emulated code can
355still be restarted in any cases.
386405f7
FB
356
357@section Linux system call translation
358
359QEMU includes a generic system call translator for Linux. It means that
360the parameters of the system calls can be converted to fix the
361endianness and 32/64 bit issues. The IOCTLs are converted with a generic
362type description system (see @file{ioctls.h} and @file{thunk.c}).
363
df0f11a0
FB
364QEMU supports host CPUs which have pages bigger than 4KB. It records all
365the mappings the process does and try to emulated the @code{mmap()}
366system calls in cases where the host @code{mmap()} call would fail
367because of bad page alignment.
368
386405f7
FB
369@section Linux signals
370
371Normal and real-time signals are queued along with their information
372(@code{siginfo_t}) as it is done in the Linux kernel. Then an interrupt
373request is done to the virtual CPU. When it is interrupted, one queued
374signal is handled by generating a stack frame in the virtual CPU as the
375Linux kernel does. The @code{sigreturn()} system call is emulated to return
376from the virtual signal handler.
377
378Some signals (such as SIGALRM) directly come from the host. Other
379signals are synthetized from the virtual CPU exceptions such as SIGFPE
380when a division by zero is done (see @code{main.c:cpu_loop()}).
381
382The blocked signal mask is still handled by the host Linux kernel so
383that most signal system calls can be redirected directly to the host
384Linux kernel. Only the @code{sigaction()} and @code{sigreturn()} system
385calls need to be fully emulated (see @file{signal.c}).
386
387@section clone() system call and threads
388
389The Linux clone() system call is usually used to create a thread. QEMU
390uses the host clone() system call so that real host threads are created
391for each emulated thread. One virtual CPU instance is created for each
392thread.
393
394The virtual x86 CPU atomic operations are emulated with a global lock so
395that their semantic is preserved.
396
df0f11a0
FB
397Note that currently there are still some locking issues in QEMU. In
398particular, the translated cache flush is not protected yet against
399reentrancy.
400
1eb87257
FB
401@section Self-virtualization
402
403QEMU was conceived so that ultimately it can emulate itself. Althought
404it is not very useful, it is an important test to show the power of the
405emulator.
406
407Achieving self-virtualization is not easy because there may be address
6cd9f35b
FB
408space conflicts. QEMU solves this problem by being an executable ELF
409shared object as the ld-linux.so ELF interpreter. That way, it can be
410relocated at load time.
1eb87257 411
386405f7
FB
412@section Bibliography
413
414@table @asis
415
416@item [1]
417@url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing
418direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio
419Riccardi.
420
421@item [2]
422@url{http://developer.kde.org/~sewardj/}, Valgrind, an open-source
423memory debugger for x86-GNU/Linux, by Julian Seward.
424
425@item [3]
426@url{http://bochs.sourceforge.net/}, the Bochs IA-32 Emulator Project,
427by Kevin Lawton et al.
428
429@item [4]
430@url{http://www.cs.rose-hulman.edu/~donaldlf/em86/index.html}, the EM86
431x86 emulator on Alpha-Linux.
432
433@item [5]
434@url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/full_papers/chernoff/chernoff.pdf},
435DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton
436Chernoff and Ray Hookway.
437
fd429f2f
FB
438@item [6]
439@url{http://www.willows.com/}, Windows API library emulation from
440Willows Software.
441
386405f7
FB
442@end table
443
444@chapter Regression Tests
445
322d0c66 446In the directory @file{tests/}, various interesting testing programs
386405f7
FB
447are available. There are used for regression testing.
448
322d0c66 449@section @file{hello-i386}
386405f7
FB
450
451Very simple statically linked x86 program, just to test QEMU during a
452port to a new host CPU.
453
322d0c66
FB
454@section @file{hello-arm}
455
456Very simple statically linked ARM program, just to test QEMU during a
457port to a new host CPU.
458
386405f7
FB
459@section @file{test-i386}
460
461This program executes most of the 16 bit and 32 bit x86 instructions and
462generates a text output. It can be compared with the output obtained with
463a real CPU or another emulator. The target @code{make test} runs this
464program and a @code{diff} on the generated output.
465
466The Linux system call @code{modify_ldt()} is used to create x86 selectors
467to test some 16 bit addressing and 32 bit with segmentation cases.
468
df0f11a0 469The Linux system call @code{vm86()} is used to test vm86 emulation.
386405f7 470
df0f11a0
FB
471Various exceptions are raised to test most of the x86 user space
472exception reporting.
386405f7
FB
473
474@section @file{sha1}
475
476It is a simple benchmark. Care must be taken to interpret the results
477because it mostly tests the ability of the virtual CPU to optimize the
478@code{rol} x86 instruction and the condition code computations.
479