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