Statistics
| Branch: | Revision:

root / qemu-doc.texi @ 08239198

History | View | Annotate | Download (13.8 kB)

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