Statistics
| Branch: | Revision:

root / qemu-doc.texi @ 16f62432

History | View | Annotate | Download (33.5 kB)

1 386405f7 bellard
\input texinfo @c -*- texinfo -*-
2 386405f7 bellard
3 0806e3f6 bellard
@iftex
4 322d0c66 bellard
@settitle QEMU CPU Emulator Reference Documentation
5 386405f7 bellard
@titlepage
6 386405f7 bellard
@sp 7
7 322d0c66 bellard
@center @titlefont{QEMU CPU Emulator Reference Documentation}
8 386405f7 bellard
@sp 3
9 386405f7 bellard
@end titlepage
10 0806e3f6 bellard
@end iftex
11 386405f7 bellard
12 386405f7 bellard
@chapter Introduction
13 386405f7 bellard
14 322d0c66 bellard
@section Features
15 386405f7 bellard
16 1eb20527 bellard
QEMU is a FAST! processor emulator. By using dynamic translation it
17 1eb20527 bellard
achieves a reasonnable speed while being easy to port on new host
18 1eb20527 bellard
CPUs.
19 1eb20527 bellard
20 1eb20527 bellard
QEMU has two operating modes:
21 0806e3f6 bellard
22 0806e3f6 bellard
@itemize @minus
23 0806e3f6 bellard
24 0806e3f6 bellard
@item 
25 0806e3f6 bellard
User mode emulation. In this mode, QEMU can launch Linux processes
26 1eb20527 bellard
compiled for one CPU on another CPU. Linux system calls are converted
27 1eb20527 bellard
because of endianness and 32/64 bit mismatches. The Wine Windows API
28 1eb20527 bellard
emulator (@url{http://www.winehq.org}) and the DOSEMU DOS emulator
29 2be3bc02 bellard
(@url{http://www.dosemu.org}) are the main targets for QEMU.
30 1eb20527 bellard
31 0806e3f6 bellard
@item 
32 0806e3f6 bellard
Full system emulation. In this mode, QEMU emulates a full
33 1eb20527 bellard
system, including a processor and various peripherials. Currently, it
34 1eb20527 bellard
is only used to launch an x86 Linux kernel on an x86 Linux system. It
35 1eb20527 bellard
enables easier testing and debugging of system code. It can also be
36 1eb20527 bellard
used to provide virtual hosting of several virtual PCs on a single
37 1eb20527 bellard
server.
38 1eb20527 bellard
39 1eb20527 bellard
@end itemize
40 1eb20527 bellard
41 1eb20527 bellard
As QEMU requires no host kernel patches to run, it is very safe and
42 1eb20527 bellard
easy to use.
43 322d0c66 bellard
44 322d0c66 bellard
QEMU generic features:
45 386405f7 bellard
46 386405f7 bellard
@itemize 
47 386405f7 bellard
48 1eb20527 bellard
@item User space only or full system emulation.
49 1eb20527 bellard
50 1eb20527 bellard
@item Using dynamic translation to native code for reasonnable speed.
51 386405f7 bellard
52 322d0c66 bellard
@item Working on x86 and PowerPC hosts. Being tested on ARM, Sparc32, Alpha and S390.
53 386405f7 bellard
54 1eb20527 bellard
@item Self-modifying code support.
55 1eb20527 bellard
56 d5a0b50c bellard
@item Precise exceptions support.
57 386405f7 bellard
58 1eb20527 bellard
@item The virtual CPU is a library (@code{libqemu}) which can be used 
59 1eb20527 bellard
in other projects.
60 1eb20527 bellard
61 1eb20527 bellard
@end itemize
62 1eb20527 bellard
63 1eb20527 bellard
QEMU user mode emulation features:
64 1eb20527 bellard
@itemize 
65 386405f7 bellard
@item Generic Linux system call converter, including most ioctls.
66 386405f7 bellard
67 386405f7 bellard
@item clone() emulation using native CPU clone() to use Linux scheduler for threads.
68 386405f7 bellard
69 322d0c66 bellard
@item Accurate signal handling by remapping host signals to target signals. 
70 1eb20527 bellard
@end itemize
71 1eb20527 bellard
@end itemize
72 df0f11a0 bellard
73 1eb20527 bellard
QEMU full system emulation features:
74 1eb20527 bellard
@itemize 
75 285dc330 bellard
@item QEMU can either use a full software MMU for maximum portability or use the host system call mmap() to simulate the target MMU.
76 322d0c66 bellard
@end itemize
77 322d0c66 bellard
78 322d0c66 bellard
@section x86 emulation
79 322d0c66 bellard
80 322d0c66 bellard
QEMU x86 target features:
81 322d0c66 bellard
82 322d0c66 bellard
@itemize 
83 322d0c66 bellard
84 322d0c66 bellard
@item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation. 
85 1eb20527 bellard
LDT/GDT and IDT are emulated. VM86 mode is also supported to run DOSEMU.
86 322d0c66 bellard
87 1eb20527 bellard
@item Support of host page sizes bigger than 4KB in user mode emulation.
88 df0f11a0 bellard
89 df0f11a0 bellard
@item QEMU can emulate itself on x86.
90 1eb87257 bellard
91 386405f7 bellard
@item An extensive Linux x86 CPU test program is included @file{tests/test-i386}. 
92 386405f7 bellard
It can be used to test other x86 virtual CPUs.
93 386405f7 bellard
94 386405f7 bellard
@end itemize
95 386405f7 bellard
96 df0f11a0 bellard
Current QEMU limitations:
97 386405f7 bellard
98 386405f7 bellard
@itemize 
99 386405f7 bellard
100 386405f7 bellard
@item No SSE/MMX support (yet).
101 386405f7 bellard
102 386405f7 bellard
@item No x86-64 support.
103 386405f7 bellard
104 df0f11a0 bellard
@item IPC syscalls are missing.
105 386405f7 bellard
106 386405f7 bellard
@item The x86 segment limits and access rights are not tested at every 
107 1eb20527 bellard
memory access.
108 386405f7 bellard
109 386405f7 bellard
@item On non x86 host CPUs, @code{double}s are used instead of the non standard 
110 386405f7 bellard
10 byte @code{long double}s of x86 for floating point emulation to get
111 386405f7 bellard
maximum performances.
112 386405f7 bellard
113 285dc330 bellard
@item Some priviledged instructions or behaviors are missing, especially for segment protection testing (yet). 
114 1eb20527 bellard
115 386405f7 bellard
@end itemize
116 386405f7 bellard
117 322d0c66 bellard
@section ARM emulation
118 322d0c66 bellard
119 322d0c66 bellard
@itemize
120 322d0c66 bellard
121 322d0c66 bellard
@item ARM emulation can currently launch small programs while using the
122 322d0c66 bellard
generic dynamic code generation architecture of QEMU.
123 322d0c66 bellard
124 322d0c66 bellard
@item No FPU support (yet).
125 322d0c66 bellard
126 322d0c66 bellard
@item No automatic regression testing (yet).
127 322d0c66 bellard
128 322d0c66 bellard
@end itemize
129 322d0c66 bellard
130 0806e3f6 bellard
@section SPARC emulation
131 0806e3f6 bellard
132 0806e3f6 bellard
The SPARC emulation is currently in development.
133 0806e3f6 bellard
134 5b9f457a bellard
@chapter Installation
135 5b9f457a bellard
136 5b9f457a bellard
If you want to compile QEMU, please read the @file{README} which gives
137 5b9f457a bellard
the related information. Otherwise just download the binary
138 5b9f457a bellard
distribution (@file{qemu-XXX-i386.tar.gz}) and untar it as root in
139 5b9f457a bellard
@file{/}:
140 5b9f457a bellard
141 5b9f457a bellard
@example
142 5b9f457a bellard
su
143 5b9f457a bellard
cd /
144 5b9f457a bellard
tar zxvf /tmp/qemu-XXX-i386.tar.gz
145 5b9f457a bellard
@end example
146 5b9f457a bellard
147 d5a0b50c bellard
@chapter QEMU User space emulator invocation
148 386405f7 bellard
149 d691f669 bellard
@section Quick Start
150 d691f669 bellard
151 386405f7 bellard
In order to launch a Linux process, QEMU needs the process executable
152 d691f669 bellard
itself and all the target (x86) dynamic libraries used by it. 
153 d691f669 bellard
154 d691f669 bellard
@itemize
155 386405f7 bellard
156 d691f669 bellard
@item On x86, you can just try to launch any process by using the native
157 d691f669 bellard
libraries:
158 386405f7 bellard
159 386405f7 bellard
@example 
160 0806e3f6 bellard
qemu-i386 -L / /bin/ls
161 386405f7 bellard
@end example
162 386405f7 bellard
163 d691f669 bellard
@code{-L /} tells that the x86 dynamic linker must be searched with a
164 d691f669 bellard
@file{/} prefix.
165 386405f7 bellard
166 5b9f457a bellard
@item Since QEMU is also a linux process, you can launch qemu with qemu (NOTE: you can only do that if you compiled QEMU from the sources):
167 1eb87257 bellard
168 1eb87257 bellard
@example 
169 0806e3f6 bellard
qemu-i386 -L / qemu-i386 -L / /bin/ls
170 1eb87257 bellard
@end example
171 386405f7 bellard
172 d691f669 bellard
@item On non x86 CPUs, you need first to download at least an x86 glibc
173 5b9f457a bellard
(@file{qemu-runtime-i386-XXX-.tar.gz} on the QEMU web page). Ensure that
174 644c433c bellard
@code{LD_LIBRARY_PATH} is not set:
175 644c433c bellard
176 644c433c bellard
@example
177 644c433c bellard
unset LD_LIBRARY_PATH 
178 644c433c bellard
@end example
179 644c433c bellard
180 644c433c bellard
Then you can launch the precompiled @file{ls} x86 executable:
181 644c433c bellard
182 d691f669 bellard
@example
183 285dc330 bellard
qemu-i386 tests/i386/ls
184 168485b7 bellard
@end example
185 285dc330 bellard
You can look at @file{qemu-binfmt-conf.sh} so that
186 168485b7 bellard
QEMU is automatically launched by the Linux kernel when you try to
187 168485b7 bellard
launch x86 executables. It requires the @code{binfmt_misc} module in the
188 168485b7 bellard
Linux kernel.
189 168485b7 bellard
190 1eb87257 bellard
@item The x86 version of QEMU is also included. You can try weird things such as:
191 1eb87257 bellard
@example
192 0806e3f6 bellard
qemu-i386 /usr/local/qemu-i386/bin/qemu-i386 /usr/local/qemu-i386/bin/ls-i386
193 1eb87257 bellard
@end example
194 1eb87257 bellard
195 168485b7 bellard
@end itemize
196 168485b7 bellard
197 df0f11a0 bellard
@section Wine launch
198 168485b7 bellard
199 168485b7 bellard
@itemize
200 168485b7 bellard
201 168485b7 bellard
@item Ensure that you have a working QEMU with the x86 glibc
202 168485b7 bellard
distribution (see previous section). In order to verify it, you must be
203 168485b7 bellard
able to do:
204 168485b7 bellard
205 168485b7 bellard
@example
206 0806e3f6 bellard
qemu-i386 /usr/local/qemu-i386/bin/ls-i386
207 168485b7 bellard
@end example
208 168485b7 bellard
209 fd429f2f bellard
@item Download the binary x86 Wine install
210 1eb87257 bellard
(@file{qemu-XXX-i386-wine.tar.gz} on the QEMU web page). 
211 168485b7 bellard
212 fd429f2f bellard
@item Configure Wine on your account. Look at the provided script
213 168485b7 bellard
@file{/usr/local/qemu-i386/bin/wine-conf.sh}. Your previous
214 168485b7 bellard
@code{$@{HOME@}/.wine} directory is saved to @code{$@{HOME@}/.wine.org}.
215 168485b7 bellard
216 168485b7 bellard
@item Then you can try the example @file{putty.exe}:
217 168485b7 bellard
218 168485b7 bellard
@example
219 0806e3f6 bellard
qemu-i386 /usr/local/qemu-i386/wine/bin/wine /usr/local/qemu-i386/wine/c/Program\ Files/putty.exe
220 386405f7 bellard
@end example
221 d691f669 bellard
222 d691f669 bellard
@end itemize
223 d691f669 bellard
224 d691f669 bellard
@section Command line options
225 d691f669 bellard
226 d691f669 bellard
@example
227 0806e3f6 bellard
usage: qemu-i386 [-h] [-d] [-L path] [-s size] program [arguments...]
228 d691f669 bellard
@end example
229 d691f669 bellard
230 df0f11a0 bellard
@table @option
231 d691f669 bellard
@item -h
232 d691f669 bellard
Print the help
233 d691f669 bellard
@item -L path   
234 d691f669 bellard
Set the x86 elf interpreter prefix (default=/usr/local/qemu-i386)
235 d691f669 bellard
@item -s size
236 d691f669 bellard
Set the x86 stack size in bytes (default=524288)
237 d691f669 bellard
@end table
238 386405f7 bellard
239 df0f11a0 bellard
Debug options:
240 df0f11a0 bellard
241 df0f11a0 bellard
@table @option
242 df0f11a0 bellard
@item -d
243 df0f11a0 bellard
Activate log (logfile=/tmp/qemu.log)
244 df0f11a0 bellard
@item -p pagesize
245 df0f11a0 bellard
Act as if the host page size was 'pagesize' bytes
246 df0f11a0 bellard
@end table
247 df0f11a0 bellard
248 1eb20527 bellard
@chapter QEMU System emulator invocation
249 1eb20527 bellard
250 0806e3f6 bellard
@section Introduction
251 0806e3f6 bellard
252 0806e3f6 bellard
@c man begin DESCRIPTION
253 0806e3f6 bellard
254 0806e3f6 bellard
The QEMU System emulator simulates a complete PC. It can either boot
255 0806e3f6 bellard
directly a Linux kernel (without any BIOS or boot loader) or boot like a
256 0806e3f6 bellard
real PC with the included BIOS.
257 0806e3f6 bellard
258 0806e3f6 bellard
In order to meet specific user needs, two versions of QEMU are
259 0806e3f6 bellard
available:
260 0806e3f6 bellard
261 0806e3f6 bellard
@enumerate
262 0806e3f6 bellard
263 0806e3f6 bellard
@item 
264 285dc330 bellard
@code{qemu-fast} uses the host Memory Management Unit (MMU) to simulate 
265 0806e3f6 bellard
the x86 MMU. It is @emph{fast} but has limitations because the whole 4 GB
266 0806e3f6 bellard
address space cannot be used and some memory mapped peripherials
267 0806e3f6 bellard
cannot be emulated accurately yet. Therefore, a specific Linux kernel
268 0806e3f6 bellard
must be used (@xref{linux_compile}).
269 0806e3f6 bellard
270 0806e3f6 bellard
@item 
271 285dc330 bellard
@code{qemu} uses a software MMU. It is about @emph{two times 
272 285dc330 bellard
slower} but gives a more accurate emulation. 
273 0806e3f6 bellard
274 0806e3f6 bellard
@end enumerate
275 0806e3f6 bellard
276 0806e3f6 bellard
QEMU emulates the following PC peripherials:
277 0806e3f6 bellard
278 0806e3f6 bellard
@itemize @minus
279 0806e3f6 bellard
@item
280 0806e3f6 bellard
VGA (hardware level, including all non standard modes)
281 0806e3f6 bellard
@item
282 0806e3f6 bellard
PS/2 mouse and keyboard
283 0806e3f6 bellard
@item 
284 181f1558 bellard
2 IDE interfaces with hard disk and CD-ROM support
285 0806e3f6 bellard
@item 
286 0806e3f6 bellard
NE2000 network adapter (port=0x300, irq=9)
287 0806e3f6 bellard
@item
288 181f1558 bellard
Serial port
289 181f1558 bellard
@item 
290 181f1558 bellard
Soundblaster 16 card
291 0806e3f6 bellard
@item
292 0806e3f6 bellard
PIC (interrupt controler)
293 0806e3f6 bellard
@item
294 0806e3f6 bellard
PIT (timers)
295 0806e3f6 bellard
@item 
296 0806e3f6 bellard
CMOS memory
297 0806e3f6 bellard
@end itemize
298 0806e3f6 bellard
299 0806e3f6 bellard
@c man end
300 0806e3f6 bellard
301 1eb20527 bellard
@section Quick Start
302 1eb20527 bellard
303 285dc330 bellard
Download and uncompress the linux image (@file{linux.img}) and type:
304 0806e3f6 bellard
305 0806e3f6 bellard
@example
306 285dc330 bellard
qemu linux.img
307 0806e3f6 bellard
@end example
308 0806e3f6 bellard
309 0806e3f6 bellard
Linux should boot and give you a prompt.
310 0806e3f6 bellard
311 0806e3f6 bellard
@section Direct Linux Boot and Network emulation
312 0806e3f6 bellard
313 0806e3f6 bellard
This section explains how to launch a Linux kernel inside QEMU without
314 0806e3f6 bellard
having to make a full bootable image. It is very useful for fast Linux
315 0806e3f6 bellard
kernel testing. The QEMU network configuration is also explained.
316 1eb20527 bellard
317 1eb20527 bellard
@enumerate
318 1eb20527 bellard
@item
319 0806e3f6 bellard
Download the archive @file{linux-test-xxx.tar.gz} containing a Linux
320 0806e3f6 bellard
kernel and a disk image. 
321 1eb20527 bellard
322 1eb20527 bellard
@item Optional: If you want network support (for example to launch X11 examples), you
323 0806e3f6 bellard
must copy the script @file{qemu-ifup} in @file{/etc} and configure
324 1eb20527 bellard
properly @code{sudo} so that the command @code{ifconfig} contained in
325 0806e3f6 bellard
@file{qemu-ifup} can be executed as root. You must verify that your host
326 1eb20527 bellard
kernel supports the TUN/TAP network interfaces: the device
327 1eb20527 bellard
@file{/dev/net/tun} must be present.
328 1eb20527 bellard
329 1eb20527 bellard
When network is enabled, there is a virtual network connection between
330 1eb20527 bellard
the host kernel and the emulated kernel. The emulated kernel is seen
331 1eb20527 bellard
from the host kernel at IP address 172.20.0.2 and the host kernel is
332 1eb20527 bellard
seen from the emulated kernel at IP address 172.20.0.1.
333 1eb20527 bellard
334 0806e3f6 bellard
@item Launch @code{qemu.sh}. You should have the following output:
335 1eb20527 bellard
336 1eb20527 bellard
@example
337 0806e3f6 bellard
> ./qemu.sh 
338 181f1558 bellard
Connected to host network interface: tun0
339 181f1558 bellard
Linux version 2.4.21 (bellard@voyager.localdomain) (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #5 Tue Nov 11 18:18:53 CET 2003
340 1eb20527 bellard
BIOS-provided physical RAM map:
341 4690764b bellard
 BIOS-e801: 0000000000000000 - 000000000009f000 (usable)
342 4690764b bellard
 BIOS-e801: 0000000000100000 - 0000000002000000 (usable)
343 1eb20527 bellard
32MB LOWMEM available.
344 1eb20527 bellard
On node 0 totalpages: 8192
345 1eb20527 bellard
zone(0): 4096 pages.
346 1eb20527 bellard
zone(1): 4096 pages.
347 1eb20527 bellard
zone(2): 0 pages.
348 181f1558 bellard
Kernel command line: root=/dev/hda sb=0x220,5,1,5 ide2=noprobe ide3=noprobe ide4=noprobe ide5=noprobe console=ttyS0
349 4690764b bellard
ide_setup: ide2=noprobe
350 4690764b bellard
ide_setup: ide3=noprobe
351 4690764b bellard
ide_setup: ide4=noprobe
352 4690764b bellard
ide_setup: ide5=noprobe
353 1eb20527 bellard
Initializing CPU#0
354 181f1558 bellard
Detected 2399.621 MHz processor.
355 181f1558 bellard
Console: colour EGA 80x25
356 181f1558 bellard
Calibrating delay loop... 4744.80 BogoMIPS
357 181f1558 bellard
Memory: 28872k/32768k available (1210k kernel code, 3508k reserved, 266k data, 64k init, 0k highmem)
358 1eb20527 bellard
Dentry cache hash table entries: 4096 (order: 3, 32768 bytes)
359 1eb20527 bellard
Inode cache hash table entries: 2048 (order: 2, 16384 bytes)
360 181f1558 bellard
Mount cache hash table entries: 512 (order: 0, 4096 bytes)
361 1eb20527 bellard
Buffer-cache hash table entries: 1024 (order: 0, 4096 bytes)
362 1eb20527 bellard
Page-cache hash table entries: 8192 (order: 3, 32768 bytes)
363 1eb20527 bellard
CPU: Intel Pentium Pro stepping 03
364 1eb20527 bellard
Checking 'hlt' instruction... OK.
365 1eb20527 bellard
POSIX conformance testing by UNIFIX
366 1eb20527 bellard
Linux NET4.0 for Linux 2.4
367 1eb20527 bellard
Based upon Swansea University Computer Society NET3.039
368 1eb20527 bellard
Initializing RT netlink socket
369 1eb20527 bellard
apm: BIOS not found.
370 1eb20527 bellard
Starting kswapd
371 4690764b bellard
Journalled Block Device driver loaded
372 181f1558 bellard
Detected PS/2 Mouse Port.
373 1eb20527 bellard
pty: 256 Unix98 ptys configured
374 1eb20527 bellard
Serial driver version 5.05c (2001-07-08) with no serial options enabled
375 1eb20527 bellard
ttyS00 at 0x03f8 (irq = 4) is a 16450
376 1eb20527 bellard
ne.c:v1.10 9/23/94 Donald Becker (becker@scyld.com)
377 1eb20527 bellard
Last modified Nov 1, 2000 by Paul Gortmaker
378 1eb20527 bellard
NE*000 ethercard probe at 0x300: 52 54 00 12 34 56
379 1eb20527 bellard
eth0: NE2000 found at 0x300, using IRQ 9.
380 4690764b bellard
RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
381 181f1558 bellard
Uniform Multi-Platform E-IDE driver Revision: 7.00beta4-2.4
382 181f1558 bellard
ide: Assuming 50MHz system bus speed for PIO modes; override with idebus=xx
383 181f1558 bellard
hda: QEMU HARDDISK, ATA DISK drive
384 181f1558 bellard
ide0 at 0x1f0-0x1f7,0x3f6 on irq 14
385 181f1558 bellard
hda: attached ide-disk driver.
386 181f1558 bellard
hda: 20480 sectors (10 MB) w/256KiB Cache, CHS=20/16/63
387 181f1558 bellard
Partition check:
388 181f1558 bellard
 hda:
389 181f1558 bellard
Soundblaster audio driver Copyright (C) by Hannu Savolainen 1993-1996
390 1eb20527 bellard
NET4: Linux TCP/IP 1.0 for NET4.0
391 1eb20527 bellard
IP Protocols: ICMP, UDP, TCP, IGMP
392 1eb20527 bellard
IP: routing cache hash table of 512 buckets, 4Kbytes
393 4690764b bellard
TCP: Hash tables configured (established 2048 bind 4096)
394 1eb20527 bellard
NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.
395 4690764b bellard
EXT2-fs warning: mounting unchecked fs, running e2fsck is recommended
396 1eb20527 bellard
VFS: Mounted root (ext2 filesystem).
397 181f1558 bellard
Freeing unused kernel memory: 64k freed
398 181f1558 bellard
 
399 181f1558 bellard
Linux version 2.4.21 (bellard@voyager.localdomain) (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #5 Tue Nov 11 18:18:53 CET 2003
400 181f1558 bellard
 
401 181f1558 bellard
QEMU Linux test distribution (based on Redhat 9)
402 181f1558 bellard
 
403 181f1558 bellard
Type 'exit' to halt the system
404 181f1558 bellard
 
405 181f1558 bellard
sh-2.05b# 
406 1eb20527 bellard
@end example
407 1eb20527 bellard
408 1eb20527 bellard
@item
409 1eb20527 bellard
Then you can play with the kernel inside the virtual serial console. You
410 1eb20527 bellard
can launch @code{ls} for example. Type @key{Ctrl-a h} to have an help
411 1eb20527 bellard
about the keys you can type inside the virtual serial console. In
412 d5a0b50c bellard
particular, use @key{Ctrl-a x} to exit QEMU and use @key{Ctrl-a b} as
413 d5a0b50c bellard
the Magic SysRq key.
414 1eb20527 bellard
415 1eb20527 bellard
@item 
416 1eb20527 bellard
If the network is enabled, launch the script @file{/etc/linuxrc} in the
417 1eb20527 bellard
emulator (don't forget the leading dot):
418 1eb20527 bellard
@example
419 1eb20527 bellard
. /etc/linuxrc
420 1eb20527 bellard
@end example
421 1eb20527 bellard
422 1eb20527 bellard
Then enable X11 connections on your PC from the emulated Linux: 
423 1eb20527 bellard
@example
424 1eb20527 bellard
xhost +172.20.0.2
425 1eb20527 bellard
@end example
426 1eb20527 bellard
427 1eb20527 bellard
You can now launch @file{xterm} or @file{xlogo} and verify that you have
428 1eb20527 bellard
a real Virtual Linux system !
429 1eb20527 bellard
430 1eb20527 bellard
@end enumerate
431 1eb20527 bellard
432 d5a0b50c bellard
NOTES:
433 d5a0b50c bellard
@enumerate
434 d5a0b50c bellard
@item 
435 0806e3f6 bellard
A 2.5.74 kernel is also included in the archive. Just
436 0806e3f6 bellard
replace the bzImage in qemu.sh to try it.
437 d5a0b50c bellard
438 d5a0b50c bellard
@item 
439 9d4520d0 bellard
qemu creates a temporary file in @var{$QEMU_TMPDIR} (@file{/tmp} is the
440 d5a0b50c bellard
default) containing all the simulated PC memory. If possible, try to use
441 d5a0b50c bellard
a temporary directory using the tmpfs filesystem to avoid too many
442 d5a0b50c bellard
unnecessary disk accesses.
443 d5a0b50c bellard
444 d5a0b50c bellard
@item 
445 9d4520d0 bellard
In order to exit cleanly from qemu, you can do a @emph{shutdown} inside
446 9d4520d0 bellard
qemu. qemu will automatically exit when the Linux shutdown is done.
447 4690764b bellard
448 4690764b bellard
@item 
449 4690764b bellard
You can boot slightly faster by disabling the probe of non present IDE
450 4690764b bellard
interfaces. To do so, add the following options on the kernel command
451 4690764b bellard
line:
452 4690764b bellard
@example
453 4690764b bellard
ide1=noprobe ide2=noprobe ide3=noprobe ide4=noprobe ide5=noprobe
454 4690764b bellard
@end example
455 4690764b bellard
456 4690764b bellard
@item 
457 4690764b bellard
The example disk image is a modified version of the one made by Kevin
458 1eb20527 bellard
Lawton for the plex86 Project (@url{www.plex86.org}).
459 1eb20527 bellard
460 d5a0b50c bellard
@end enumerate
461 d5a0b50c bellard
462 ec410fc9 bellard
@section Invocation
463 ec410fc9 bellard
464 ec410fc9 bellard
@example
465 0806e3f6 bellard
@c man begin SYNOPSIS
466 0806e3f6 bellard
usage: qemu [options] [disk_image]
467 0806e3f6 bellard
@c man end
468 ec410fc9 bellard
@end example
469 ec410fc9 bellard
470 0806e3f6 bellard
@c man begin OPTIONS
471 9d4520d0 bellard
@var{disk_image} is a raw hard disk image for IDE hard disk 0.
472 ec410fc9 bellard
473 ec410fc9 bellard
General options:
474 ec410fc9 bellard
@table @option
475 2be3bc02 bellard
@item -fda file
476 2be3bc02 bellard
@item -fdb file
477 2be3bc02 bellard
Use @var{file} as floppy disk 0/1 image (@xref{disk_images}).
478 2be3bc02 bellard
479 ec410fc9 bellard
@item -hda file
480 ec410fc9 bellard
@item -hdb file
481 181f1558 bellard
@item -hdc file
482 181f1558 bellard
@item -hdd file
483 2be3bc02 bellard
Use @var{file} as hard disk 0, 1, 2 or 3 image (@xref{disk_images}).
484 1f47a922 bellard
485 181f1558 bellard
@item -cdrom file
486 181f1558 bellard
Use @var{file} as CD-ROM image (you cannot use @option{-hdc} and and
487 181f1558 bellard
@option{-cdrom} at the same time).
488 181f1558 bellard
489 2be3bc02 bellard
@item -boot [a|b|c|d]
490 2be3bc02 bellard
Boot on floppy (a, b), hard disk (c) or CD-ROM (d). Hard disk boot is
491 2be3bc02 bellard
the default.
492 1f47a922 bellard
493 181f1558 bellard
@item -snapshot
494 1f47a922 bellard
Write to temporary files instead of disk image files. In this case,
495 1f47a922 bellard
the raw disk image you use is not written back. You can however force
496 1f47a922 bellard
the write back by pressing @key{C-a s} (@xref{disk_images}). 
497 ec410fc9 bellard
498 ec410fc9 bellard
@item -m megs
499 ec410fc9 bellard
Set virtual RAM size to @var{megs} megabytes.
500 ec410fc9 bellard
501 ec410fc9 bellard
@item -n script      
502 9d4520d0 bellard
Set network init script [default=/etc/qemu-ifup]. This script is
503 ec410fc9 bellard
launched to configure the host network interface (usually tun0)
504 ec410fc9 bellard
corresponding to the virtual NE2000 card.
505 4690764b bellard
506 4690764b bellard
@item -initrd file
507 0806e3f6 bellard
Use @var{file} as initial ram disk.
508 0806e3f6 bellard
509 0806e3f6 bellard
@item -tun-fd fd      
510 0806e3f6 bellard
Assumes @var{fd} talks to tap/tun and use it. Read
511 0806e3f6 bellard
@url{http://bellard.org/qemu/tetrinet.html} to have an example of its
512 0806e3f6 bellard
use.
513 0806e3f6 bellard
514 0806e3f6 bellard
@item -nographic
515 0806e3f6 bellard
516 0806e3f6 bellard
Normally, QEMU uses SDL to display the VGA output. With this option,
517 0806e3f6 bellard
you can totally disable graphical output so that QEMU is a simple
518 0806e3f6 bellard
command line application. The emulated serial port is redirected on
519 0806e3f6 bellard
the console. Therefore, you can still use QEMU to debug a Linux kernel
520 0806e3f6 bellard
with a serial console.
521 0806e3f6 bellard
522 0806e3f6 bellard
@end table
523 0806e3f6 bellard
524 0806e3f6 bellard
Linux boot specific (does not require a full PC boot with a BIOS):
525 0806e3f6 bellard
@table @option
526 0806e3f6 bellard
527 0806e3f6 bellard
@item -kernel bzImage 
528 0806e3f6 bellard
Use @var{bzImage} as kernel image.
529 0806e3f6 bellard
530 0806e3f6 bellard
@item -append cmdline 
531 0806e3f6 bellard
Use @var{cmdline} as kernel command line
532 0806e3f6 bellard
533 0806e3f6 bellard
@item -initrd file
534 0806e3f6 bellard
Use @var{file} as initial ram disk.
535 0806e3f6 bellard
536 ec410fc9 bellard
@end table
537 ec410fc9 bellard
538 ec410fc9 bellard
Debug options:
539 ec410fc9 bellard
@table @option
540 ec410fc9 bellard
@item -s
541 0806e3f6 bellard
Wait gdb connection to port 1234 (@xref{gdb_usage}). 
542 ec410fc9 bellard
@item -p port
543 ec410fc9 bellard
Change gdb connection port.
544 ec410fc9 bellard
@item -d             
545 9d4520d0 bellard
Output log in /tmp/qemu.log
546 ec410fc9 bellard
@end table
547 ec410fc9 bellard
548 ec410fc9 bellard
During emulation, use @key{C-a h} to get terminal commands:
549 ec410fc9 bellard
550 ec410fc9 bellard
@table @key
551 ec410fc9 bellard
@item C-a h
552 ec410fc9 bellard
Print this help
553 ec410fc9 bellard
@item C-a x    
554 ec410fc9 bellard
Exit emulatior
555 1f47a922 bellard
@item C-a s    
556 1f47a922 bellard
Save disk data back to file (if -snapshot)
557 1f47a922 bellard
@item C-a b
558 ec410fc9 bellard
Send break (magic sysrq)
559 1f47a922 bellard
@item C-a C-a
560 ec410fc9 bellard
Send C-a
561 ec410fc9 bellard
@end table
562 0806e3f6 bellard
@c man end
563 0806e3f6 bellard
564 0806e3f6 bellard
@ignore
565 0806e3f6 bellard
566 0806e3f6 bellard
@setfilename qemu 
567 0806e3f6 bellard
@settitle QEMU System Emulator
568 0806e3f6 bellard
569 0806e3f6 bellard
@c man begin SEEALSO
570 0806e3f6 bellard
The HTML documentation of QEMU for more precise information and Linux
571 0806e3f6 bellard
user mode emulator invocation.
572 0806e3f6 bellard
@c man end
573 0806e3f6 bellard
574 0806e3f6 bellard
@c man begin AUTHOR
575 0806e3f6 bellard
Fabrice Bellard
576 0806e3f6 bellard
@c man end
577 0806e3f6 bellard
578 0806e3f6 bellard
@end ignore
579 ec410fc9 bellard
580 0806e3f6 bellard
@end ignore
581 1f47a922 bellard
@node disk_images
582 1f47a922 bellard
@section Disk Images
583 1f47a922 bellard
584 1f47a922 bellard
@subsection Raw disk images
585 1f47a922 bellard
586 1f47a922 bellard
The disk images can simply be raw images of the hard disk. You can
587 1f47a922 bellard
create them with the command:
588 1f47a922 bellard
@example
589 1f47a922 bellard
dd if=/dev/zero of=myimage bs=1024 count=mysize
590 1f47a922 bellard
@end example
591 1f47a922 bellard
where @var{myimage} is the image filename and @var{mysize} is its size
592 1f47a922 bellard
in kilobytes.
593 1f47a922 bellard
594 1f47a922 bellard
@subsection Snapshot mode
595 1f47a922 bellard
596 1f47a922 bellard
If you use the option @option{-snapshot}, all disk images are
597 1f47a922 bellard
considered as read only. When sectors in written, they are written in
598 1f47a922 bellard
a temporary file created in @file{/tmp}. You can however force the
599 1f47a922 bellard
write back to the raw disk images by pressing @key{C-a s}.
600 1f47a922 bellard
601 1f47a922 bellard
NOTE: The snapshot mode only works with raw disk images.
602 1f47a922 bellard
603 1f47a922 bellard
@subsection Copy On Write disk images
604 1f47a922 bellard
605 1f47a922 bellard
QEMU also supports user mode Linux
606 1f47a922 bellard
(@url{http://user-mode-linux.sourceforge.net/}) Copy On Write (COW)
607 1f47a922 bellard
disk images. The COW disk images are much smaller than normal images
608 1f47a922 bellard
as they store only modified sectors. They also permit the use of the
609 1f47a922 bellard
same disk image template for many users.
610 1f47a922 bellard
611 1f47a922 bellard
To create a COW disk images, use the command:
612 1f47a922 bellard
613 1f47a922 bellard
@example
614 0806e3f6 bellard
qemu-mkcow -f myrawimage.bin mycowimage.cow
615 1f47a922 bellard
@end example
616 1f47a922 bellard
617 1f47a922 bellard
@file{myrawimage.bin} is a raw image you want to use as original disk
618 1f47a922 bellard
image. It will never be written to.
619 1f47a922 bellard
620 1f47a922 bellard
@file{mycowimage.cow} is the COW disk image which is created by
621 0806e3f6 bellard
@code{qemu-mkcow}. You can use it directly with the @option{-hdx}
622 1f47a922 bellard
options. You must not modify the original raw disk image if you use
623 1f47a922 bellard
COW images, as COW images only store the modified sectors from the raw
624 1f47a922 bellard
disk image. QEMU stores the original raw disk image name and its
625 1f47a922 bellard
modified time in the COW disk image so that chances of mistakes are
626 1f47a922 bellard
reduced.
627 1f47a922 bellard
628 9d0fe224 bellard
If the raw disk image is not read-only, by pressing @key{C-a s} you
629 9d0fe224 bellard
can flush the COW disk image back into the raw disk image, as in
630 9d0fe224 bellard
snapshot mode.
631 1f47a922 bellard
632 1f47a922 bellard
COW disk images can also be created without a corresponding raw disk
633 1f47a922 bellard
image. It is useful to have a big initial virtual disk image without
634 1f47a922 bellard
using much disk space. Use:
635 1f47a922 bellard
636 1f47a922 bellard
@example
637 0806e3f6 bellard
qemu-mkcow mycowimage.cow 1024
638 1f47a922 bellard
@end example
639 1f47a922 bellard
640 1f47a922 bellard
to create a 1 gigabyte empty COW disk image.
641 1f47a922 bellard
642 1f47a922 bellard
NOTES: 
643 1f47a922 bellard
@enumerate
644 1f47a922 bellard
@item
645 1f47a922 bellard
COW disk images must be created on file systems supporting
646 1f47a922 bellard
@emph{holes} such as ext2 or ext3.
647 1f47a922 bellard
@item 
648 1f47a922 bellard
Since holes are used, the displayed size of the COW disk image is not
649 1f47a922 bellard
the real one. To know it, use the @code{ls -ls} command.
650 1f47a922 bellard
@end enumerate
651 1f47a922 bellard
652 0806e3f6 bellard
@node linux_compile
653 4690764b bellard
@section Linux Kernel Compilation
654 4690764b bellard
655 285dc330 bellard
You can use any linux kernel with QEMU. However, if you want to use
656 285dc330 bellard
@code{qemu-fast} to get maximum performances, you should make the
657 285dc330 bellard
following changes to the Linux kernel (only 2.4.x and 2.5.x were
658 285dc330 bellard
tested):
659 1eb20527 bellard
660 4690764b bellard
@enumerate
661 4690764b bellard
@item
662 4690764b bellard
The kernel must be mapped at 0x90000000 (the default is
663 4690764b bellard
0xc0000000). You must modify only two lines in the kernel source:
664 1eb20527 bellard
665 4690764b bellard
In @file{include/asm/page.h}, replace
666 1eb20527 bellard
@example
667 1eb20527 bellard
#define __PAGE_OFFSET           (0xc0000000)
668 1eb20527 bellard
@end example
669 1eb20527 bellard
by
670 1eb20527 bellard
@example
671 1eb20527 bellard
#define __PAGE_OFFSET           (0x90000000)
672 1eb20527 bellard
@end example
673 1eb20527 bellard
674 4690764b bellard
And in @file{arch/i386/vmlinux.lds}, replace
675 1eb20527 bellard
@example
676 1eb20527 bellard
  . = 0xc0000000 + 0x100000;
677 1eb20527 bellard
@end example
678 1eb20527 bellard
by 
679 1eb20527 bellard
@example
680 1eb20527 bellard
  . = 0x90000000 + 0x100000;
681 1eb20527 bellard
@end example
682 1eb20527 bellard
683 4690764b bellard
@item
684 4690764b bellard
If you want to enable SMP (Symmetric Multi-Processing) support, you
685 4690764b bellard
must make the following change in @file{include/asm/fixmap.h}. Replace
686 1eb20527 bellard
@example
687 4690764b bellard
#define FIXADDR_TOP	(0xffffX000UL)
688 1eb20527 bellard
@end example
689 4690764b bellard
by 
690 4690764b bellard
@example
691 4690764b bellard
#define FIXADDR_TOP	(0xa7ffX000UL)
692 4690764b bellard
@end example
693 4690764b bellard
(X is 'e' or 'f' depending on the kernel version). Although you can
694 4690764b bellard
use an SMP kernel with QEMU, it only supports one CPU.
695 1eb20527 bellard
696 4690764b bellard
@item
697 d5a0b50c bellard
If you are not using a 2.5 kernel as host kernel but if you use a target
698 d5a0b50c bellard
2.5 kernel, you must also ensure that the 'HZ' define is set to 100
699 d5a0b50c bellard
(1000 is the default) as QEMU cannot currently emulate timers at
700 d5a0b50c bellard
frequencies greater than 100 Hz on host Linux systems < 2.5. In
701 4690764b bellard
@file{include/asm/param.h}, replace:
702 d5a0b50c bellard
703 d5a0b50c bellard
@example
704 d5a0b50c bellard
# define HZ		1000		/* Internal kernel timer frequency */
705 d5a0b50c bellard
@end example
706 d5a0b50c bellard
by
707 d5a0b50c bellard
@example
708 d5a0b50c bellard
# define HZ		100		/* Internal kernel timer frequency */
709 d5a0b50c bellard
@end example
710 d5a0b50c bellard
711 4690764b bellard
@end enumerate
712 4690764b bellard
713 4690764b bellard
The file config-2.x.x gives the configuration of the example kernels.
714 4690764b bellard
715 4690764b bellard
Just type
716 4690764b bellard
@example
717 4690764b bellard
make bzImage
718 4690764b bellard
@end example
719 4690764b bellard
720 4690764b bellard
As you would do to make a real kernel. Then you can use with QEMU
721 4690764b bellard
exactly the same kernel as you would boot on your PC (in
722 4690764b bellard
@file{arch/i386/boot/bzImage}).
723 da415d54 bellard
724 0806e3f6 bellard
@node gdb_usage
725 da415d54 bellard
@section GDB usage
726 da415d54 bellard
727 da415d54 bellard
QEMU has a primitive support to work with gdb, so that you can do
728 0806e3f6 bellard
'Ctrl-C' while the virtual machine is running and inspect its state.
729 da415d54 bellard
730 9d4520d0 bellard
In order to use gdb, launch qemu with the '-s' option. It will wait for a
731 da415d54 bellard
gdb connection:
732 da415d54 bellard
@example
733 6c9bf893 bellard
> qemu -s -kernel arch/i386/boot/bzImage -hda root-2.4.20.img -append "root=/dev/hda"
734 da415d54 bellard
Connected to host network interface: tun0
735 da415d54 bellard
Waiting gdb connection on port 1234
736 da415d54 bellard
@end example
737 da415d54 bellard
738 da415d54 bellard
Then launch gdb on the 'vmlinux' executable:
739 da415d54 bellard
@example
740 da415d54 bellard
> gdb vmlinux
741 da415d54 bellard
@end example
742 da415d54 bellard
743 da415d54 bellard
In gdb, connect to QEMU:
744 da415d54 bellard
@example
745 6c9bf893 bellard
(gdb) target remote localhost:1234
746 da415d54 bellard
@end example
747 da415d54 bellard
748 da415d54 bellard
Then you can use gdb normally. For example, type 'c' to launch the kernel:
749 da415d54 bellard
@example
750 da415d54 bellard
(gdb) c
751 da415d54 bellard
@end example
752 da415d54 bellard
753 0806e3f6 bellard
Here are some useful tips in order to use gdb on system code:
754 0806e3f6 bellard
755 0806e3f6 bellard
@enumerate
756 0806e3f6 bellard
@item
757 0806e3f6 bellard
Use @code{info reg} to display all the CPU registers.
758 0806e3f6 bellard
@item
759 0806e3f6 bellard
Use @code{x/10i $eip} to display the code at the PC position.
760 0806e3f6 bellard
@item
761 0806e3f6 bellard
Use @code{set architecture i8086} to dump 16 bit code. Then use
762 0806e3f6 bellard
@code{x/10i $cs*16+*eip} to dump the code at the PC position.
763 0806e3f6 bellard
@end enumerate
764 0806e3f6 bellard
765 386405f7 bellard
@chapter QEMU Internals
766 386405f7 bellard
767 386405f7 bellard
@section QEMU compared to other emulators
768 386405f7 bellard
769 1eb20527 bellard
Like bochs [3], QEMU emulates an x86 CPU. But QEMU is much faster than
770 1eb20527 bellard
bochs as it uses dynamic compilation and because it uses the host MMU to
771 1eb20527 bellard
simulate the x86 MMU. The downside is that currently the emulation is
772 1eb20527 bellard
not as accurate as bochs (for example, you cannot currently run Windows
773 1eb20527 bellard
inside QEMU).
774 386405f7 bellard
775 386405f7 bellard
Like Valgrind [2], QEMU does user space emulation and dynamic
776 386405f7 bellard
translation. Valgrind is mainly a memory debugger while QEMU has no
777 1eb20527 bellard
support for it (QEMU could be used to detect out of bound memory
778 1eb20527 bellard
accesses as Valgrind, but it has no support to track uninitialised data
779 d5a0b50c bellard
as Valgrind does). The Valgrind dynamic translator generates better code
780 1eb20527 bellard
than QEMU (in particular it does register allocation) but it is closely
781 d5a0b50c bellard
tied to an x86 host and target and has no support for precise exceptions
782 1eb20527 bellard
and system emulation.
783 1eb20527 bellard
784 1eb20527 bellard
EM86 [4] is the closest project to user space QEMU (and QEMU still uses
785 1eb20527 bellard
some of its code, in particular the ELF file loader). EM86 was limited
786 1eb20527 bellard
to an alpha host and used a proprietary and slow interpreter (the
787 1eb20527 bellard
interpreter part of the FX!32 Digital Win32 code translator [5]).
788 386405f7 bellard
789 fd429f2f bellard
TWIN [6] is a Windows API emulator like Wine. It is less accurate than
790 fd429f2f bellard
Wine but includes a protected mode x86 interpreter to launch x86 Windows
791 fd429f2f bellard
executables. Such an approach as greater potential because most of the
792 fd429f2f bellard
Windows API is executed natively but it is far more difficult to develop
793 fd429f2f bellard
because all the data structures and function parameters exchanged
794 fd429f2f bellard
between the API and the x86 code must be converted.
795 fd429f2f bellard
796 1eb20527 bellard
User mode Linux [7] was the only solution before QEMU to launch a Linux
797 1eb20527 bellard
kernel as a process while not needing any host kernel patches. However,
798 1eb20527 bellard
user mode Linux requires heavy kernel patches while QEMU accepts
799 1eb20527 bellard
unpatched Linux kernels. It would be interesting to compare the
800 1eb20527 bellard
performance of the two approaches.
801 1eb20527 bellard
802 1eb20527 bellard
The new Plex86 [8] PC virtualizer is done in the same spirit as the QEMU
803 1eb20527 bellard
system emulator. It requires a patched Linux kernel to work (you cannot
804 1eb20527 bellard
launch the same kernel on your PC), but the patches are really small. As
805 1eb20527 bellard
it is a PC virtualizer (no emulation is done except for some priveledged
806 1eb20527 bellard
instructions), it has the potential of being faster than QEMU. The
807 d5a0b50c bellard
downside is that a complicated (and potentially unsafe) host kernel
808 d5a0b50c bellard
patch is needed.
809 1eb20527 bellard
810 386405f7 bellard
@section Portable dynamic translation
811 386405f7 bellard
812 386405f7 bellard
QEMU is a dynamic translator. When it first encounters a piece of code,
813 386405f7 bellard
it converts it to the host instruction set. Usually dynamic translators
814 322d0c66 bellard
are very complicated and highly CPU dependent. QEMU uses some tricks
815 386405f7 bellard
which make it relatively easily portable and simple while achieving good
816 386405f7 bellard
performances.
817 386405f7 bellard
818 386405f7 bellard
The basic idea is to split every x86 instruction into fewer simpler
819 386405f7 bellard
instructions. Each simple instruction is implemented by a piece of C
820 386405f7 bellard
code (see @file{op-i386.c}). Then a compile time tool (@file{dyngen})
821 386405f7 bellard
takes the corresponding object file (@file{op-i386.o}) to generate a
822 386405f7 bellard
dynamic code generator which concatenates the simple instructions to
823 386405f7 bellard
build a function (see @file{op-i386.h:dyngen_code()}).
824 386405f7 bellard
825 386405f7 bellard
In essence, the process is similar to [1], but more work is done at
826 386405f7 bellard
compile time. 
827 386405f7 bellard
828 386405f7 bellard
A key idea to get optimal performances is that constant parameters can
829 386405f7 bellard
be passed to the simple operations. For that purpose, dummy ELF
830 386405f7 bellard
relocations are generated with gcc for each constant parameter. Then,
831 386405f7 bellard
the tool (@file{dyngen}) can locate the relocations and generate the
832 386405f7 bellard
appriopriate C code to resolve them when building the dynamic code.
833 386405f7 bellard
834 386405f7 bellard
That way, QEMU is no more difficult to port than a dynamic linker.
835 386405f7 bellard
836 386405f7 bellard
To go even faster, GCC static register variables are used to keep the
837 386405f7 bellard
state of the virtual CPU.
838 386405f7 bellard
839 386405f7 bellard
@section Register allocation
840 386405f7 bellard
841 386405f7 bellard
Since QEMU uses fixed simple instructions, no efficient register
842 386405f7 bellard
allocation can be done. However, because RISC CPUs have a lot of
843 386405f7 bellard
register, most of the virtual CPU state can be put in registers without
844 386405f7 bellard
doing complicated register allocation.
845 386405f7 bellard
846 386405f7 bellard
@section Condition code optimisations
847 386405f7 bellard
848 386405f7 bellard
Good CPU condition codes emulation (@code{EFLAGS} register on x86) is a
849 386405f7 bellard
critical point to get good performances. QEMU uses lazy condition code
850 386405f7 bellard
evaluation: instead of computing the condition codes after each x86
851 fd429f2f bellard
instruction, it just stores one operand (called @code{CC_SRC}), the
852 386405f7 bellard
result (called @code{CC_DST}) and the type of operation (called
853 386405f7 bellard
@code{CC_OP}).
854 386405f7 bellard
855 386405f7 bellard
@code{CC_OP} is almost never explicitely set in the generated code
856 386405f7 bellard
because it is known at translation time.
857 386405f7 bellard
858 386405f7 bellard
In order to increase performances, a backward pass is performed on the
859 386405f7 bellard
generated simple instructions (see
860 386405f7 bellard
@code{translate-i386.c:optimize_flags()}). When it can be proved that
861 386405f7 bellard
the condition codes are not needed by the next instructions, no
862 386405f7 bellard
condition codes are computed at all.
863 386405f7 bellard
864 fd429f2f bellard
@section CPU state optimisations
865 386405f7 bellard
866 386405f7 bellard
The x86 CPU has many internal states which change the way it evaluates
867 386405f7 bellard
instructions. In order to achieve a good speed, the translation phase
868 386405f7 bellard
considers that some state information of the virtual x86 CPU cannot
869 386405f7 bellard
change in it. For example, if the SS, DS and ES segments have a zero
870 386405f7 bellard
base, then the translator does not even generate an addition for the
871 386405f7 bellard
segment base.
872 386405f7 bellard
873 386405f7 bellard
[The FPU stack pointer register is not handled that way yet].
874 386405f7 bellard
875 386405f7 bellard
@section Translation cache
876 386405f7 bellard
877 386405f7 bellard
A 2MByte cache holds the most recently used translations. For
878 386405f7 bellard
simplicity, it is completely flushed when it is full. A translation unit
879 386405f7 bellard
contains just a single basic block (a block of x86 instructions
880 386405f7 bellard
terminated by a jump or by a virtual CPU state change which the
881 386405f7 bellard
translator cannot deduce statically).
882 386405f7 bellard
883 df0f11a0 bellard
@section Direct block chaining
884 df0f11a0 bellard
885 df0f11a0 bellard
After each translated basic block is executed, QEMU uses the simulated
886 df0f11a0 bellard
Program Counter (PC) and other cpu state informations (such as the CS
887 df0f11a0 bellard
segment base value) to find the next basic block.
888 df0f11a0 bellard
889 df0f11a0 bellard
In order to accelerate the most common cases where the new simulated PC
890 df0f11a0 bellard
is known, QEMU can patch a basic block so that it jumps directly to the
891 df0f11a0 bellard
next one.
892 df0f11a0 bellard
893 df0f11a0 bellard
The most portable code uses an indirect jump. An indirect jump makes it
894 df0f11a0 bellard
easier to make the jump target modification atomic. On some
895 df0f11a0 bellard
architectures (such as PowerPC), the @code{JUMP} opcode is directly
896 df0f11a0 bellard
patched so that the block chaining has no overhead.
897 df0f11a0 bellard
898 df0f11a0 bellard
@section Self-modifying code and translated code invalidation
899 df0f11a0 bellard
900 df0f11a0 bellard
Self-modifying code is a special challenge in x86 emulation because no
901 df0f11a0 bellard
instruction cache invalidation is signaled by the application when code
902 df0f11a0 bellard
is modified.
903 df0f11a0 bellard
904 df0f11a0 bellard
When translated code is generated for a basic block, the corresponding
905 df0f11a0 bellard
host page is write protected if it is not already read-only (with the
906 df0f11a0 bellard
system call @code{mprotect()}). Then, if a write access is done to the
907 df0f11a0 bellard
page, Linux raises a SEGV signal. QEMU then invalidates all the
908 df0f11a0 bellard
translated code in the page and enables write accesses to the page.
909 df0f11a0 bellard
910 df0f11a0 bellard
Correct translated code invalidation is done efficiently by maintaining
911 df0f11a0 bellard
a linked list of every translated block contained in a given page. Other
912 df0f11a0 bellard
linked lists are also maintained to undo direct block chaining. 
913 df0f11a0 bellard
914 4690764b bellard
Although the overhead of doing @code{mprotect()} calls is important,
915 df0f11a0 bellard
most MSDOS programs can be emulated at reasonnable speed with QEMU and
916 df0f11a0 bellard
DOSEMU.
917 df0f11a0 bellard
918 df0f11a0 bellard
Note that QEMU also invalidates pages of translated code when it detects
919 df0f11a0 bellard
that memory mappings are modified with @code{mmap()} or @code{munmap()}.
920 386405f7 bellard
921 386405f7 bellard
@section Exception support
922 386405f7 bellard
923 386405f7 bellard
longjmp() is used when an exception such as division by zero is
924 df0f11a0 bellard
encountered. 
925 386405f7 bellard
926 df0f11a0 bellard
The host SIGSEGV and SIGBUS signal handlers are used to get invalid
927 df0f11a0 bellard
memory accesses. The exact CPU state can be retrieved because all the
928 df0f11a0 bellard
x86 registers are stored in fixed host registers. The simulated program
929 df0f11a0 bellard
counter is found by retranslating the corresponding basic block and by
930 df0f11a0 bellard
looking where the host program counter was at the exception point.
931 df0f11a0 bellard
932 df0f11a0 bellard
The virtual CPU cannot retrieve the exact @code{EFLAGS} register because
933 df0f11a0 bellard
in some cases it is not computed because of condition code
934 df0f11a0 bellard
optimisations. It is not a big concern because the emulated code can
935 df0f11a0 bellard
still be restarted in any cases.
936 386405f7 bellard
937 386405f7 bellard
@section Linux system call translation
938 386405f7 bellard
939 386405f7 bellard
QEMU includes a generic system call translator for Linux. It means that
940 386405f7 bellard
the parameters of the system calls can be converted to fix the
941 386405f7 bellard
endianness and 32/64 bit issues. The IOCTLs are converted with a generic
942 386405f7 bellard
type description system (see @file{ioctls.h} and @file{thunk.c}).
943 386405f7 bellard
944 df0f11a0 bellard
QEMU supports host CPUs which have pages bigger than 4KB. It records all
945 df0f11a0 bellard
the mappings the process does and try to emulated the @code{mmap()}
946 df0f11a0 bellard
system calls in cases where the host @code{mmap()} call would fail
947 df0f11a0 bellard
because of bad page alignment.
948 df0f11a0 bellard
949 386405f7 bellard
@section Linux signals
950 386405f7 bellard
951 386405f7 bellard
Normal and real-time signals are queued along with their information
952 386405f7 bellard
(@code{siginfo_t}) as it is done in the Linux kernel. Then an interrupt
953 386405f7 bellard
request is done to the virtual CPU. When it is interrupted, one queued
954 386405f7 bellard
signal is handled by generating a stack frame in the virtual CPU as the
955 386405f7 bellard
Linux kernel does. The @code{sigreturn()} system call is emulated to return
956 386405f7 bellard
from the virtual signal handler.
957 386405f7 bellard
958 386405f7 bellard
Some signals (such as SIGALRM) directly come from the host. Other
959 386405f7 bellard
signals are synthetized from the virtual CPU exceptions such as SIGFPE
960 386405f7 bellard
when a division by zero is done (see @code{main.c:cpu_loop()}).
961 386405f7 bellard
962 386405f7 bellard
The blocked signal mask is still handled by the host Linux kernel so
963 386405f7 bellard
that most signal system calls can be redirected directly to the host
964 386405f7 bellard
Linux kernel. Only the @code{sigaction()} and @code{sigreturn()} system
965 386405f7 bellard
calls need to be fully emulated (see @file{signal.c}).
966 386405f7 bellard
967 386405f7 bellard
@section clone() system call and threads
968 386405f7 bellard
969 386405f7 bellard
The Linux clone() system call is usually used to create a thread. QEMU
970 386405f7 bellard
uses the host clone() system call so that real host threads are created
971 386405f7 bellard
for each emulated thread. One virtual CPU instance is created for each
972 386405f7 bellard
thread.
973 386405f7 bellard
974 386405f7 bellard
The virtual x86 CPU atomic operations are emulated with a global lock so
975 386405f7 bellard
that their semantic is preserved.
976 386405f7 bellard
977 df0f11a0 bellard
Note that currently there are still some locking issues in QEMU. In
978 df0f11a0 bellard
particular, the translated cache flush is not protected yet against
979 df0f11a0 bellard
reentrancy.
980 df0f11a0 bellard
981 1eb87257 bellard
@section Self-virtualization
982 1eb87257 bellard
983 4690764b bellard
QEMU was conceived so that ultimately it can emulate itself. Although
984 1eb87257 bellard
it is not very useful, it is an important test to show the power of the
985 1eb87257 bellard
emulator.
986 1eb87257 bellard
987 1eb87257 bellard
Achieving self-virtualization is not easy because there may be address
988 6cd9f35b bellard
space conflicts. QEMU solves this problem by being an executable ELF
989 6cd9f35b bellard
shared object as the ld-linux.so ELF interpreter. That way, it can be
990 6cd9f35b bellard
relocated at load time.
991 1eb87257 bellard
992 1eb20527 bellard
@section MMU emulation
993 1eb20527 bellard
994 1eb20527 bellard
For system emulation, QEMU uses the mmap() system call to emulate the
995 1eb20527 bellard
target CPU MMU. It works as long the emulated OS does not use an area
996 1eb20527 bellard
reserved by the host OS (such as the area above 0xc0000000 on x86
997 1eb20527 bellard
Linux).
998 1eb20527 bellard
999 1eb20527 bellard
It is planned to add a slower but more precise MMU emulation
1000 1eb20527 bellard
with a software MMU.
1001 1eb20527 bellard
1002 386405f7 bellard
@section Bibliography
1003 386405f7 bellard
1004 386405f7 bellard
@table @asis
1005 386405f7 bellard
1006 386405f7 bellard
@item [1] 
1007 386405f7 bellard
@url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing
1008 386405f7 bellard
direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio
1009 386405f7 bellard
Riccardi.
1010 386405f7 bellard
1011 386405f7 bellard
@item [2]
1012 386405f7 bellard
@url{http://developer.kde.org/~sewardj/}, Valgrind, an open-source
1013 386405f7 bellard
memory debugger for x86-GNU/Linux, by Julian Seward.
1014 386405f7 bellard
1015 386405f7 bellard
@item [3]
1016 386405f7 bellard
@url{http://bochs.sourceforge.net/}, the Bochs IA-32 Emulator Project,
1017 386405f7 bellard
by Kevin Lawton et al.
1018 386405f7 bellard
1019 386405f7 bellard
@item [4]
1020 386405f7 bellard
@url{http://www.cs.rose-hulman.edu/~donaldlf/em86/index.html}, the EM86
1021 386405f7 bellard
x86 emulator on Alpha-Linux.
1022 386405f7 bellard
1023 386405f7 bellard
@item [5]
1024 386405f7 bellard
@url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/full_papers/chernoff/chernoff.pdf},
1025 386405f7 bellard
DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton
1026 386405f7 bellard
Chernoff and Ray Hookway.
1027 386405f7 bellard
1028 fd429f2f bellard
@item [6]
1029 fd429f2f bellard
@url{http://www.willows.com/}, Windows API library emulation from
1030 fd429f2f bellard
Willows Software.
1031 fd429f2f bellard
1032 1eb20527 bellard
@item [7]
1033 1eb20527 bellard
@url{http://user-mode-linux.sourceforge.net/}, 
1034 1eb20527 bellard
The User-mode Linux Kernel.
1035 1eb20527 bellard
1036 1eb20527 bellard
@item [8]
1037 1eb20527 bellard
@url{http://www.plex86.org/}, 
1038 1eb20527 bellard
The new Plex86 project.
1039 1eb20527 bellard
1040 386405f7 bellard
@end table
1041 386405f7 bellard
1042 386405f7 bellard
@chapter Regression Tests
1043 386405f7 bellard
1044 322d0c66 bellard
In the directory @file{tests/}, various interesting testing programs
1045 386405f7 bellard
are available. There are used for regression testing.
1046 386405f7 bellard
1047 386405f7 bellard
@section @file{test-i386}
1048 386405f7 bellard
1049 386405f7 bellard
This program executes most of the 16 bit and 32 bit x86 instructions and
1050 386405f7 bellard
generates a text output. It can be compared with the output obtained with
1051 386405f7 bellard
a real CPU or another emulator. The target @code{make test} runs this
1052 386405f7 bellard
program and a @code{diff} on the generated output.
1053 386405f7 bellard
1054 386405f7 bellard
The Linux system call @code{modify_ldt()} is used to create x86 selectors
1055 386405f7 bellard
to test some 16 bit addressing and 32 bit with segmentation cases.
1056 386405f7 bellard
1057 df0f11a0 bellard
The Linux system call @code{vm86()} is used to test vm86 emulation.
1058 386405f7 bellard
1059 df0f11a0 bellard
Various exceptions are raised to test most of the x86 user space
1060 df0f11a0 bellard
exception reporting.
1061 386405f7 bellard
1062 285dc330 bellard
@section @file{linux-test}
1063 285dc330 bellard
1064 285dc330 bellard
This program tests various Linux system calls. It is used to verify
1065 285dc330 bellard
that the system call parameters are correctly converted between target
1066 285dc330 bellard
and host CPUs.
1067 285dc330 bellard
1068 285dc330 bellard
@section @file{hello-i386}
1069 285dc330 bellard
1070 285dc330 bellard
Very simple statically linked x86 program, just to test QEMU during a
1071 285dc330 bellard
port to a new host CPU.
1072 285dc330 bellard
1073 285dc330 bellard
@section @file{hello-arm}
1074 285dc330 bellard
1075 285dc330 bellard
Very simple statically linked ARM program, just to test QEMU during a
1076 285dc330 bellard
port to a new host CPU.
1077 285dc330 bellard
1078 386405f7 bellard
@section @file{sha1}
1079 386405f7 bellard
1080 386405f7 bellard
It is a simple benchmark. Care must be taken to interpret the results
1081 386405f7 bellard
because it mostly tests the ability of the virtual CPU to optimize the
1082 386405f7 bellard
@code{rol} x86 instruction and the condition code computations.