blob: 0ff1454c1948ef2575d308488c7d0318dde7b4c3 [file] [log] [blame]
bellard386405f2003-03-23 21:28:45 +00001\input texinfo @c -*- texinfo -*-
2
bellard0806e3f2003-10-01 00:15:32 +00003@iftex
bellard322d0c62003-06-15 23:29:28 +00004@settitle QEMU CPU Emulator Reference Documentation
bellard386405f2003-03-23 21:28:45 +00005@titlepage
6@sp 7
bellard322d0c62003-06-15 23:29:28 +00007@center @titlefont{QEMU CPU Emulator Reference Documentation}
bellard386405f2003-03-23 21:28:45 +00008@sp 3
9@end titlepage
bellard0806e3f2003-10-01 00:15:32 +000010@end iftex
bellard386405f2003-03-23 21:28:45 +000011
12@chapter Introduction
13
bellard322d0c62003-06-15 23:29:28 +000014@section Features
bellard386405f2003-03-23 21:28:45 +000015
bellard1eb20522003-06-25 16:21:49 +000016QEMU is a FAST! processor emulator. By using dynamic translation it
17achieves a reasonnable speed while being easy to port on new host
18CPUs.
19
20QEMU has two operating modes:
bellard0806e3f2003-10-01 00:15:32 +000021
22@itemize @minus
23
24@item
25User mode emulation. In this mode, QEMU can launch Linux processes
bellard1eb20522003-06-25 16:21:49 +000026compiled for one CPU on another CPU. Linux system calls are converted
27because of endianness and 32/64 bit mismatches. The Wine Windows API
28emulator (@url{http://www.winehq.org}) and the DOSEMU DOS emulator
29(@url{www.dosemu.org}) are the main targets for QEMU.
30
bellard0806e3f2003-10-01 00:15:32 +000031@item
32Full system emulation. In this mode, QEMU emulates a full
bellard1eb20522003-06-25 16:21:49 +000033system, including a processor and various peripherials. Currently, it
34is only used to launch an x86 Linux kernel on an x86 Linux system. It
35enables easier testing and debugging of system code. It can also be
36used to provide virtual hosting of several virtual PCs on a single
37server.
38
39@end itemize
40
41As QEMU requires no host kernel patches to run, it is very safe and
42easy to use.
bellard322d0c62003-06-15 23:29:28 +000043
44QEMU generic features:
bellard386405f2003-03-23 21:28:45 +000045
46@itemize
47
bellard1eb20522003-06-25 16:21:49 +000048@item User space only or full system emulation.
bellard386405f2003-03-23 21:28:45 +000049
bellardfd429f22003-03-30 20:59:46 +000050@item Using dynamic translation to native code for reasonnable speed.
bellard386405f2003-03-23 21:28:45 +000051
bellard1eb20522003-06-25 16:21:49 +000052@item Working on x86 and PowerPC hosts. Being tested on ARM, Sparc32, Alpha and S390.
53
54@item Self-modifying code support.
55
bellardd5a0b502003-06-27 12:02:03 +000056@item Precise exceptions support.
bellard1eb20522003-06-25 16:21:49 +000057
58@item The virtual CPU is a library (@code{libqemu}) which can be used
59in other projects.
60
61@end itemize
62
63QEMU user mode emulation features:
64@itemize
bellard386405f2003-03-23 21:28:45 +000065@item Generic Linux system call converter, including most ioctls.
66
67@item clone() emulation using native CPU clone() to use Linux scheduler for threads.
68
bellard322d0c62003-06-15 23:29:28 +000069@item Accurate signal handling by remapping host signals to target signals.
bellard1eb20522003-06-25 16:21:49 +000070@end itemize
71@end itemize
bellarddf0f11a2003-05-28 00:27:57 +000072
bellard1eb20522003-06-25 16:21:49 +000073QEMU full system emulation features:
74@itemize
75@item Using mmap() system calls to simulate the MMU
bellard322d0c62003-06-15 23:29:28 +000076@end itemize
77
78@section x86 emulation
79
80QEMU x86 target features:
81
82@itemize
83
84@item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation.
bellard1eb20522003-06-25 16:21:49 +000085LDT/GDT and IDT are emulated. VM86 mode is also supported to run DOSEMU.
bellard322d0c62003-06-15 23:29:28 +000086
bellard1eb20522003-06-25 16:21:49 +000087@item Support of host page sizes bigger than 4KB in user mode emulation.
bellarddf0f11a2003-05-28 00:27:57 +000088
89@item QEMU can emulate itself on x86.
bellard1eb87252003-04-11 01:12:28 +000090
bellard386405f2003-03-23 21:28:45 +000091@item An extensive Linux x86 CPU test program is included @file{tests/test-i386}.
92It can be used to test other x86 virtual CPUs.
93
94@end itemize
95
bellarddf0f11a2003-05-28 00:27:57 +000096Current QEMU limitations:
bellard386405f2003-03-23 21:28:45 +000097
98@itemize
99
bellard386405f2003-03-23 21:28:45 +0000100@item No SSE/MMX support (yet).
101
102@item No x86-64 support.
103
bellarddf0f11a2003-05-28 00:27:57 +0000104@item IPC syscalls are missing.
bellard386405f2003-03-23 21:28:45 +0000105
106@item The x86 segment limits and access rights are not tested at every
bellard1eb20522003-06-25 16:21:49 +0000107memory access.
bellard386405f2003-03-23 21:28:45 +0000108
109@item On non x86 host CPUs, @code{double}s are used instead of the non standard
11010 byte @code{long double}s of x86 for floating point emulation to get
111maximum performances.
112
bellard1eb20522003-06-25 16:21:49 +0000113@item Full system emulation only works if no data are mapped above the virtual address
1140xc0000000 (yet).
115
116@item Some priviledged instructions or behaviors are missing. Only the ones
117needed for proper Linux kernel operation are emulated.
118
119@item No memory separation between the kernel and the user processes is done.
120It will be implemented very soon.
121
bellard386405f2003-03-23 21:28:45 +0000122@end itemize
123
bellard322d0c62003-06-15 23:29:28 +0000124@section ARM emulation
125
126@itemize
127
128@item ARM emulation can currently launch small programs while using the
129generic dynamic code generation architecture of QEMU.
130
131@item No FPU support (yet).
132
133@item No automatic regression testing (yet).
134
135@end itemize
136
bellard0806e3f2003-10-01 00:15:32 +0000137@section SPARC emulation
138
139The SPARC emulation is currently in development.
140
bellardd5a0b502003-06-27 12:02:03 +0000141@chapter QEMU User space emulator invocation
bellard386405f2003-03-23 21:28:45 +0000142
bellardd691f662003-03-24 21:58:34 +0000143@section Quick Start
144
bellard322d0c62003-06-15 23:29:28 +0000145If you need to compile QEMU, please read the @file{README} which gives
146the related information.
147
bellard386405f2003-03-23 21:28:45 +0000148In order to launch a Linux process, QEMU needs the process executable
bellardd691f662003-03-24 21:58:34 +0000149itself and all the target (x86) dynamic libraries used by it.
bellard386405f2003-03-23 21:28:45 +0000150
bellardd691f662003-03-24 21:58:34 +0000151@itemize
152
153@item On x86, you can just try to launch any process by using the native
154libraries:
bellard386405f2003-03-23 21:28:45 +0000155
156@example
bellard0806e3f2003-10-01 00:15:32 +0000157qemu-i386 -L / /bin/ls
bellard386405f2003-03-23 21:28:45 +0000158@end example
159
bellardd691f662003-03-24 21:58:34 +0000160@code{-L /} tells that the x86 dynamic linker must be searched with a
161@file{/} prefix.
bellard386405f2003-03-23 21:28:45 +0000162
bellard1eb87252003-04-11 01:12:28 +0000163@item Since QEMU is also a linux process, you can launch qemu with qemu:
164
165@example
bellard0806e3f2003-10-01 00:15:32 +0000166qemu-i386 -L / qemu-i386 -L / /bin/ls
bellard1eb87252003-04-11 01:12:28 +0000167@end example
bellard386405f2003-03-23 21:28:45 +0000168
bellardd691f662003-03-24 21:58:34 +0000169@item On non x86 CPUs, you need first to download at least an x86 glibc
bellard1eb87252003-04-11 01:12:28 +0000170(@file{qemu-XXX-i386-glibc21.tar.gz} on the QEMU web page). Ensure that
bellard644c4332003-03-24 23:00:36 +0000171@code{LD_LIBRARY_PATH} is not set:
172
173@example
174unset LD_LIBRARY_PATH
175@end example
176
177Then you can launch the precompiled @file{ls} x86 executable:
178
bellardd691f662003-03-24 21:58:34 +0000179@example
bellard0806e3f2003-10-01 00:15:32 +0000180qemu-i386 /usr/local/qemu-i386/bin/ls-i386
bellard386405f2003-03-23 21:28:45 +0000181@end example
bellard168485b2003-03-29 16:57:34 +0000182You can look at @file{/usr/local/qemu-i386/bin/qemu-conf.sh} so that
183QEMU is automatically launched by the Linux kernel when you try to
184launch x86 executables. It requires the @code{binfmt_misc} module in the
185Linux kernel.
186
bellard1eb87252003-04-11 01:12:28 +0000187@item The x86 version of QEMU is also included. You can try weird things such as:
188@example
bellard0806e3f2003-10-01 00:15:32 +0000189qemu-i386 /usr/local/qemu-i386/bin/qemu-i386 /usr/local/qemu-i386/bin/ls-i386
bellard1eb87252003-04-11 01:12:28 +0000190@end example
191
bellard168485b2003-03-29 16:57:34 +0000192@end itemize
193
bellarddf0f11a2003-05-28 00:27:57 +0000194@section Wine launch
bellard168485b2003-03-29 16:57:34 +0000195
196@itemize
197
198@item Ensure that you have a working QEMU with the x86 glibc
199distribution (see previous section). In order to verify it, you must be
200able to do:
201
202@example
bellard0806e3f2003-10-01 00:15:32 +0000203qemu-i386 /usr/local/qemu-i386/bin/ls-i386
bellard168485b2003-03-29 16:57:34 +0000204@end example
205
bellardfd429f22003-03-30 20:59:46 +0000206@item Download the binary x86 Wine install
bellard1eb87252003-04-11 01:12:28 +0000207(@file{qemu-XXX-i386-wine.tar.gz} on the QEMU web page).
bellard168485b2003-03-29 16:57:34 +0000208
bellardfd429f22003-03-30 20:59:46 +0000209@item Configure Wine on your account. Look at the provided script
bellard168485b2003-03-29 16:57:34 +0000210@file{/usr/local/qemu-i386/bin/wine-conf.sh}. Your previous
211@code{$@{HOME@}/.wine} directory is saved to @code{$@{HOME@}/.wine.org}.
212
213@item Then you can try the example @file{putty.exe}:
214
215@example
bellard0806e3f2003-10-01 00:15:32 +0000216qemu-i386 /usr/local/qemu-i386/wine/bin/wine /usr/local/qemu-i386/wine/c/Program\ Files/putty.exe
bellard168485b2003-03-29 16:57:34 +0000217@end example
bellardd691f662003-03-24 21:58:34 +0000218
219@end itemize
220
221@section Command line options
222
223@example
bellard0806e3f2003-10-01 00:15:32 +0000224usage: qemu-i386 [-h] [-d] [-L path] [-s size] program [arguments...]
bellardd691f662003-03-24 21:58:34 +0000225@end example
226
bellarddf0f11a2003-05-28 00:27:57 +0000227@table @option
bellardd691f662003-03-24 21:58:34 +0000228@item -h
229Print the help
bellardd691f662003-03-24 21:58:34 +0000230@item -L path
231Set the x86 elf interpreter prefix (default=/usr/local/qemu-i386)
232@item -s size
233Set the x86 stack size in bytes (default=524288)
234@end table
bellard386405f2003-03-23 21:28:45 +0000235
bellarddf0f11a2003-05-28 00:27:57 +0000236Debug options:
237
238@table @option
239@item -d
240Activate log (logfile=/tmp/qemu.log)
241@item -p pagesize
242Act as if the host page size was 'pagesize' bytes
243@end table
244
bellard1eb20522003-06-25 16:21:49 +0000245@chapter QEMU System emulator invocation
246
bellard0806e3f2003-10-01 00:15:32 +0000247@section Introduction
248
249@c man begin DESCRIPTION
250
251The QEMU System emulator simulates a complete PC. It can either boot
252directly a Linux kernel (without any BIOS or boot loader) or boot like a
253real PC with the included BIOS.
254
255In order to meet specific user needs, two versions of QEMU are
256available:
257
258@enumerate
259
260@item
261@code{qemu} uses the host Memory Management Unit (MMU) to simulate
262the x86 MMU. It is @emph{fast} but has limitations because the whole 4 GB
263address space cannot be used and some memory mapped peripherials
264cannot be emulated accurately yet. Therefore, a specific Linux kernel
265must be used (@xref{linux_compile}).
266
267@item
268@code{qemu-softmmu} uses a software MMU. It is about @emph{two times
269slower} but gives a more accurate emulation. (XXX: Linux cannot be ran
270unpatched yet).
271
272@end enumerate
273
274QEMU emulates the following PC peripherials:
275
276@itemize @minus
277@item
278VGA (hardware level, including all non standard modes)
279@item
280PS/2 mouse and keyboard
281@item
282IDE disk interface (port=0x1f0, irq=14)
283@item
284NE2000 network adapter (port=0x300, irq=9)
285@item
286Serial port (port=0x3f8, irq=4)
287@item
288PIC (interrupt controler)
289@item
290PIT (timers)
291@item
292CMOS memory
293@end itemize
294
295@c man end
296
bellard1eb20522003-06-25 16:21:49 +0000297@section Quick Start
298
bellard0806e3f2003-10-01 00:15:32 +0000299Download the linux image (@file{linux.img}) and type:
300
301@example
302qemu-softmmu linux.img
303@end example
304
305Linux should boot and give you a prompt.
306
307@section Direct Linux Boot and Network emulation
308
309This section explains how to launch a Linux kernel inside QEMU without
310having to make a full bootable image. It is very useful for fast Linux
311kernel testing. The QEMU network configuration is also explained.
bellard1eb20522003-06-25 16:21:49 +0000312
313@enumerate
314@item
bellard0806e3f2003-10-01 00:15:32 +0000315Download the archive @file{linux-test-xxx.tar.gz} containing a Linux
316kernel and a disk image.
bellard1eb20522003-06-25 16:21:49 +0000317
318@item Optional: If you want network support (for example to launch X11 examples), you
bellard0806e3f2003-10-01 00:15:32 +0000319must copy the script @file{qemu-ifup} in @file{/etc} and configure
bellard1eb20522003-06-25 16:21:49 +0000320properly @code{sudo} so that the command @code{ifconfig} contained in
bellard0806e3f2003-10-01 00:15:32 +0000321@file{qemu-ifup} can be executed as root. You must verify that your host
bellard1eb20522003-06-25 16:21:49 +0000322kernel supports the TUN/TAP network interfaces: the device
323@file{/dev/net/tun} must be present.
324
325When network is enabled, there is a virtual network connection between
326the host kernel and the emulated kernel. The emulated kernel is seen
327from the host kernel at IP address 172.20.0.2 and the host kernel is
328seen from the emulated kernel at IP address 172.20.0.1.
329
bellard0806e3f2003-10-01 00:15:32 +0000330@item Launch @code{qemu.sh}. You should have the following output:
bellard1eb20522003-06-25 16:21:49 +0000331
332@example
bellard0806e3f2003-10-01 00:15:32 +0000333> ./qemu.sh
bellard1eb20522003-06-25 16:21:49 +0000334connected to host network interface: tun0
335Uncompressing Linux... Ok, booting the kernel.
bellard46907642003-07-07 12:17:46 +0000336Linux version 2.4.20 (fabrice@localhost.localdomain) (gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110)) #22 lun jui 7 13:37:41 CEST 2003
bellard1eb20522003-06-25 16:21:49 +0000337BIOS-provided physical RAM map:
bellard46907642003-07-07 12:17:46 +0000338 BIOS-e801: 0000000000000000 - 000000000009f000 (usable)
339 BIOS-e801: 0000000000100000 - 0000000002000000 (usable)
bellard1eb20522003-06-25 16:21:49 +000034032MB LOWMEM available.
341On node 0 totalpages: 8192
342zone(0): 4096 pages.
343zone(1): 4096 pages.
344zone(2): 0 pages.
bellard46907642003-07-07 12:17:46 +0000345Kernel command line: root=/dev/hda ide1=noprobe ide2=noprobe ide3=noprobe ide4=noprobe ide5=noprobe
346ide_setup: ide1=noprobe
347ide_setup: ide2=noprobe
348ide_setup: ide3=noprobe
349ide_setup: ide4=noprobe
350ide_setup: ide5=noprobe
bellard1eb20522003-06-25 16:21:49 +0000351Initializing CPU#0
bellard46907642003-07-07 12:17:46 +0000352Detected 501.285 MHz processor.
353Calibrating delay loop... 989.59 BogoMIPS
354Memory: 29268k/32768k available (907k kernel code, 3112k reserved, 212k data, 52k init, 0k highmem)
bellard1eb20522003-06-25 16:21:49 +0000355Dentry cache hash table entries: 4096 (order: 3, 32768 bytes)
356Inode cache hash table entries: 2048 (order: 2, 16384 bytes)
357Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
358Buffer-cache hash table entries: 1024 (order: 0, 4096 bytes)
359Page-cache hash table entries: 8192 (order: 3, 32768 bytes)
360CPU: Intel Pentium Pro stepping 03
361Checking 'hlt' instruction... OK.
362POSIX conformance testing by UNIFIX
363Linux NET4.0 for Linux 2.4
364Based upon Swansea University Computer Society NET3.039
365Initializing RT netlink socket
366apm: BIOS not found.
367Starting kswapd
bellard46907642003-07-07 12:17:46 +0000368Journalled Block Device driver loaded
bellard1eb20522003-06-25 16:21:49 +0000369pty: 256 Unix98 ptys configured
370Serial driver version 5.05c (2001-07-08) with no serial options enabled
371ttyS00 at 0x03f8 (irq = 4) is a 16450
bellard46907642003-07-07 12:17:46 +0000372Uniform Multi-Platform E-IDE driver Revision: 6.31
373ide: Assuming 50MHz system bus speed for PIO modes; override with idebus=xx
374hda: QEMU HARDDISK, ATA DISK drive
375ide0 at 0x1f0-0x1f7,0x3f6 on irq 14
376hda: 12288 sectors (6 MB) w/256KiB Cache, CHS=12/16/63
377Partition check:
378 hda: unknown partition table
bellard1eb20522003-06-25 16:21:49 +0000379ne.c:v1.10 9/23/94 Donald Becker (becker@scyld.com)
380Last modified Nov 1, 2000 by Paul Gortmaker
381NE*000 ethercard probe at 0x300: 52 54 00 12 34 56
382eth0: NE2000 found at 0x300, using IRQ 9.
bellard46907642003-07-07 12:17:46 +0000383RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
bellard1eb20522003-06-25 16:21:49 +0000384NET4: Linux TCP/IP 1.0 for NET4.0
385IP Protocols: ICMP, UDP, TCP, IGMP
386IP: routing cache hash table of 512 buckets, 4Kbytes
bellard46907642003-07-07 12:17:46 +0000387TCP: Hash tables configured (established 2048 bind 4096)
bellard1eb20522003-06-25 16:21:49 +0000388NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.
bellard46907642003-07-07 12:17:46 +0000389EXT2-fs warning: mounting unchecked fs, running e2fsck is recommended
bellard1eb20522003-06-25 16:21:49 +0000390VFS: Mounted root (ext2 filesystem).
bellard46907642003-07-07 12:17:46 +0000391Freeing unused kernel memory: 52k freed
bellard1eb20522003-06-25 16:21:49 +0000392sh: can't access tty; job control turned off
393#
394@end example
395
396@item
397Then you can play with the kernel inside the virtual serial console. You
398can launch @code{ls} for example. Type @key{Ctrl-a h} to have an help
399about the keys you can type inside the virtual serial console. In
bellardd5a0b502003-06-27 12:02:03 +0000400particular, use @key{Ctrl-a x} to exit QEMU and use @key{Ctrl-a b} as
401the Magic SysRq key.
bellard1eb20522003-06-25 16:21:49 +0000402
403@item
404If the network is enabled, launch the script @file{/etc/linuxrc} in the
405emulator (don't forget the leading dot):
406@example
407. /etc/linuxrc
408@end example
409
410Then enable X11 connections on your PC from the emulated Linux:
411@example
412xhost +172.20.0.2
413@end example
414
415You can now launch @file{xterm} or @file{xlogo} and verify that you have
416a real Virtual Linux system !
417
418@end enumerate
419
bellardd5a0b502003-06-27 12:02:03 +0000420NOTES:
421@enumerate
422@item
bellard0806e3f2003-10-01 00:15:32 +0000423A 2.5.74 kernel is also included in the archive. Just
424replace the bzImage in qemu.sh to try it.
bellardd5a0b502003-06-27 12:02:03 +0000425
426@item
bellard0806e3f2003-10-01 00:15:32 +0000427vl creates a temporary file in @var{$QEMU_TMPDIR} (@file{/tmp} is the
bellardd5a0b502003-06-27 12:02:03 +0000428default) containing all the simulated PC memory. If possible, try to use
429a temporary directory using the tmpfs filesystem to avoid too many
430unnecessary disk accesses.
431
432@item
bellard46907642003-07-07 12:17:46 +0000433In order to exit cleanly for vl, you can do a @emph{shutdown} inside
434vl. vl will automatically exit when the Linux shutdown is done.
435
436@item
437You can boot slightly faster by disabling the probe of non present IDE
438interfaces. To do so, add the following options on the kernel command
439line:
440@example
441ide1=noprobe ide2=noprobe ide3=noprobe ide4=noprobe ide5=noprobe
442@end example
443
444@item
445The example disk image is a modified version of the one made by Kevin
bellard1eb20522003-06-25 16:21:49 +0000446Lawton for the plex86 Project (@url{www.plex86.org}).
447
bellardd5a0b502003-06-27 12:02:03 +0000448@end enumerate
449
bellardec410fc2003-06-30 23:16:33 +0000450@section Invocation
451
452@example
bellard0806e3f2003-10-01 00:15:32 +0000453@c man begin SYNOPSIS
454usage: qemu [options] [disk_image]
455@c man end
bellardec410fc2003-06-30 23:16:33 +0000456@end example
457
bellard0806e3f2003-10-01 00:15:32 +0000458@c man begin OPTIONS
459@var{disk_image} is a raw hard image image for IDE hard disk 0.
bellardec410fc2003-06-30 23:16:33 +0000460
461General options:
462@table @option
bellardec410fc2003-06-30 23:16:33 +0000463@item -hda file
464@item -hdb file
bellard0806e3f2003-10-01 00:15:32 +0000465Use @var{file} as hard disk 0 or 1 image (@xref{disk_images}).
bellard1f47a922003-07-06 19:01:55 +0000466
467@item -snapshot
468
469Write to temporary files instead of disk image files. In this case,
470the raw disk image you use is not written back. You can however force
471the write back by pressing @key{C-a s} (@xref{disk_images}).
bellardec410fc2003-06-30 23:16:33 +0000472
473@item -m megs
474Set virtual RAM size to @var{megs} megabytes.
475
476@item -n script
477Set network init script [default=/etc/vl-ifup]. This script is
478launched to configure the host network interface (usually tun0)
479corresponding to the virtual NE2000 card.
bellard46907642003-07-07 12:17:46 +0000480
481@item -initrd file
bellard0806e3f2003-10-01 00:15:32 +0000482Use @var{file} as initial ram disk.
483
484@item -tun-fd fd
485Assumes @var{fd} talks to tap/tun and use it. Read
486@url{http://bellard.org/qemu/tetrinet.html} to have an example of its
487use.
488
489@item -nographic
490
491Normally, QEMU uses SDL to display the VGA output. With this option,
492you can totally disable graphical output so that QEMU is a simple
493command line application. The emulated serial port is redirected on
494the console. Therefore, you can still use QEMU to debug a Linux kernel
495with a serial console.
496
497@end table
498
499Linux boot specific (does not require a full PC boot with a BIOS):
500@table @option
501
502@item -kernel bzImage
503Use @var{bzImage} as kernel image.
504
505@item -append cmdline
506Use @var{cmdline} as kernel command line
507
508@item -initrd file
509Use @var{file} as initial ram disk.
510
bellardec410fc2003-06-30 23:16:33 +0000511@end table
512
513Debug options:
514@table @option
515@item -s
bellard0806e3f2003-10-01 00:15:32 +0000516Wait gdb connection to port 1234 (@xref{gdb_usage}).
bellardec410fc2003-06-30 23:16:33 +0000517@item -p port
518Change gdb connection port.
519@item -d
520Output log in /tmp/vl.log
521@end table
522
523During emulation, use @key{C-a h} to get terminal commands:
524
525@table @key
526@item C-a h
527Print this help
528@item C-a x
529Exit emulatior
bellard1f47a922003-07-06 19:01:55 +0000530@item C-a s
531Save disk data back to file (if -snapshot)
532@item C-a b
bellardec410fc2003-06-30 23:16:33 +0000533Send break (magic sysrq)
bellard1f47a922003-07-06 19:01:55 +0000534@item C-a C-a
bellardec410fc2003-06-30 23:16:33 +0000535Send C-a
536@end table
bellard0806e3f2003-10-01 00:15:32 +0000537@c man end
bellardec410fc2003-06-30 23:16:33 +0000538
bellard0806e3f2003-10-01 00:15:32 +0000539@ignore
540
541@setfilename qemu
542@settitle QEMU System Emulator
543
544@c man begin SEEALSO
545The HTML documentation of QEMU for more precise information and Linux
546user mode emulator invocation.
547@c man end
548
549@c man begin AUTHOR
550Fabrice Bellard
551@c man end
552
553@end ignore
554
555@end ignore
bellard1f47a922003-07-06 19:01:55 +0000556@node disk_images
557@section Disk Images
558
559@subsection Raw disk images
560
561The disk images can simply be raw images of the hard disk. You can
562create them with the command:
563@example
564dd if=/dev/zero of=myimage bs=1024 count=mysize
565@end example
566where @var{myimage} is the image filename and @var{mysize} is its size
567in kilobytes.
568
569@subsection Snapshot mode
570
571If you use the option @option{-snapshot}, all disk images are
572considered as read only. When sectors in written, they are written in
573a temporary file created in @file{/tmp}. You can however force the
574write back to the raw disk images by pressing @key{C-a s}.
575
576NOTE: The snapshot mode only works with raw disk images.
577
578@subsection Copy On Write disk images
579
580QEMU also supports user mode Linux
581(@url{http://user-mode-linux.sourceforge.net/}) Copy On Write (COW)
582disk images. The COW disk images are much smaller than normal images
583as they store only modified sectors. They also permit the use of the
584same disk image template for many users.
585
586To create a COW disk images, use the command:
587
588@example
bellard0806e3f2003-10-01 00:15:32 +0000589qemu-mkcow -f myrawimage.bin mycowimage.cow
bellard1f47a922003-07-06 19:01:55 +0000590@end example
591
592@file{myrawimage.bin} is a raw image you want to use as original disk
593image. It will never be written to.
594
595@file{mycowimage.cow} is the COW disk image which is created by
bellard0806e3f2003-10-01 00:15:32 +0000596@code{qemu-mkcow}. You can use it directly with the @option{-hdx}
bellard1f47a922003-07-06 19:01:55 +0000597options. You must not modify the original raw disk image if you use
598COW images, as COW images only store the modified sectors from the raw
599disk image. QEMU stores the original raw disk image name and its
600modified time in the COW disk image so that chances of mistakes are
601reduced.
602
bellard9d0fe222003-07-13 22:08:50 +0000603If the raw disk image is not read-only, by pressing @key{C-a s} you
604can flush the COW disk image back into the raw disk image, as in
605snapshot mode.
bellard1f47a922003-07-06 19:01:55 +0000606
607COW disk images can also be created without a corresponding raw disk
608image. It is useful to have a big initial virtual disk image without
609using much disk space. Use:
610
611@example
bellard0806e3f2003-10-01 00:15:32 +0000612qemu-mkcow mycowimage.cow 1024
bellard1f47a922003-07-06 19:01:55 +0000613@end example
614
615to create a 1 gigabyte empty COW disk image.
616
617NOTES:
618@enumerate
619@item
620COW disk images must be created on file systems supporting
621@emph{holes} such as ext2 or ext3.
622@item
623Since holes are used, the displayed size of the COW disk image is not
624the real one. To know it, use the @code{ls -ls} command.
625@end enumerate
626
bellard0806e3f2003-10-01 00:15:32 +0000627@node linux_compile
bellard46907642003-07-07 12:17:46 +0000628@section Linux Kernel Compilation
bellard1eb20522003-06-25 16:21:49 +0000629
bellard46907642003-07-07 12:17:46 +0000630You should be able to use any kernel with QEMU provided you make the
631following changes (only 2.4.x and 2.5.x were tested):
bellard1eb20522003-06-25 16:21:49 +0000632
bellard46907642003-07-07 12:17:46 +0000633@enumerate
634@item
635The kernel must be mapped at 0x90000000 (the default is
6360xc0000000). You must modify only two lines in the kernel source:
637
638In @file{include/asm/page.h}, replace
bellard1eb20522003-06-25 16:21:49 +0000639@example
640#define __PAGE_OFFSET (0xc0000000)
641@end example
642by
643@example
644#define __PAGE_OFFSET (0x90000000)
645@end example
646
bellard46907642003-07-07 12:17:46 +0000647And in @file{arch/i386/vmlinux.lds}, replace
bellard1eb20522003-06-25 16:21:49 +0000648@example
649 . = 0xc0000000 + 0x100000;
650@end example
651by
652@example
653 . = 0x90000000 + 0x100000;
654@end example
655
bellard46907642003-07-07 12:17:46 +0000656@item
657If you want to enable SMP (Symmetric Multi-Processing) support, you
658must make the following change in @file{include/asm/fixmap.h}. Replace
bellard1eb20522003-06-25 16:21:49 +0000659@example
bellard46907642003-07-07 12:17:46 +0000660#define FIXADDR_TOP (0xffffX000UL)
bellard1eb20522003-06-25 16:21:49 +0000661@end example
bellard46907642003-07-07 12:17:46 +0000662by
663@example
664#define FIXADDR_TOP (0xa7ffX000UL)
665@end example
666(X is 'e' or 'f' depending on the kernel version). Although you can
667use an SMP kernel with QEMU, it only supports one CPU.
bellard1eb20522003-06-25 16:21:49 +0000668
bellard46907642003-07-07 12:17:46 +0000669@item
bellardd5a0b502003-06-27 12:02:03 +0000670If you are not using a 2.5 kernel as host kernel but if you use a target
6712.5 kernel, you must also ensure that the 'HZ' define is set to 100
672(1000 is the default) as QEMU cannot currently emulate timers at
673frequencies greater than 100 Hz on host Linux systems < 2.5. In
bellard46907642003-07-07 12:17:46 +0000674@file{include/asm/param.h}, replace:
bellardd5a0b502003-06-27 12:02:03 +0000675
676@example
677# define HZ 1000 /* Internal kernel timer frequency */
678@end example
679by
680@example
681# define HZ 100 /* Internal kernel timer frequency */
682@end example
683
bellard46907642003-07-07 12:17:46 +0000684@end enumerate
685
686The file config-2.x.x gives the configuration of the example kernels.
687
688Just type
689@example
690make bzImage
691@end example
692
693As you would do to make a real kernel. Then you can use with QEMU
694exactly the same kernel as you would boot on your PC (in
695@file{arch/i386/boot/bzImage}).
bellardda415d52003-06-27 18:50:50 +0000696
bellard0806e3f2003-10-01 00:15:32 +0000697@node gdb_usage
bellardda415d52003-06-27 18:50:50 +0000698@section GDB usage
699
700QEMU has a primitive support to work with gdb, so that you can do
bellard0806e3f2003-10-01 00:15:32 +0000701'Ctrl-C' while the virtual machine is running and inspect its state.
bellardda415d52003-06-27 18:50:50 +0000702
703In order to use gdb, launch vl with the '-s' option. It will wait for a
704gdb connection:
705@example
bellardd6b49362003-07-13 22:37:44 +0000706> vl -s arch/i386/boot/bzImage -hda root-2.4.20.img root=/dev/hda
bellardda415d52003-06-27 18:50:50 +0000707Connected to host network interface: tun0
708Waiting gdb connection on port 1234
709@end example
710
711Then launch gdb on the 'vmlinux' executable:
712@example
713> gdb vmlinux
714@end example
715
716In gdb, connect to QEMU:
717@example
718(gdb) target remote locahost:1234
719@end example
720
721Then you can use gdb normally. For example, type 'c' to launch the kernel:
722@example
723(gdb) c
724@end example
725
726WARNING: breakpoints and single stepping are not yet supported.
727
bellard0806e3f2003-10-01 00:15:32 +0000728Here are some useful tips in order to use gdb on system code:
729
730@enumerate
731@item
732Use @code{info reg} to display all the CPU registers.
733@item
734Use @code{x/10i $eip} to display the code at the PC position.
735@item
736Use @code{set architecture i8086} to dump 16 bit code. Then use
737@code{x/10i $cs*16+*eip} to dump the code at the PC position.
738@end enumerate
739
bellard386405f2003-03-23 21:28:45 +0000740@chapter QEMU Internals
741
742@section QEMU compared to other emulators
743
bellard1eb20522003-06-25 16:21:49 +0000744Like bochs [3], QEMU emulates an x86 CPU. But QEMU is much faster than
745bochs as it uses dynamic compilation and because it uses the host MMU to
746simulate the x86 MMU. The downside is that currently the emulation is
747not as accurate as bochs (for example, you cannot currently run Windows
748inside QEMU).
bellard386405f2003-03-23 21:28:45 +0000749
750Like Valgrind [2], QEMU does user space emulation and dynamic
751translation. Valgrind is mainly a memory debugger while QEMU has no
bellard1eb20522003-06-25 16:21:49 +0000752support for it (QEMU could be used to detect out of bound memory
753accesses as Valgrind, but it has no support to track uninitialised data
bellardd5a0b502003-06-27 12:02:03 +0000754as Valgrind does). The Valgrind dynamic translator generates better code
bellard1eb20522003-06-25 16:21:49 +0000755than QEMU (in particular it does register allocation) but it is closely
bellardd5a0b502003-06-27 12:02:03 +0000756tied to an x86 host and target and has no support for precise exceptions
bellard1eb20522003-06-25 16:21:49 +0000757and system emulation.
bellard386405f2003-03-23 21:28:45 +0000758
bellard1eb20522003-06-25 16:21:49 +0000759EM86 [4] is the closest project to user space QEMU (and QEMU still uses
760some of its code, in particular the ELF file loader). EM86 was limited
761to an alpha host and used a proprietary and slow interpreter (the
762interpreter part of the FX!32 Digital Win32 code translator [5]).
bellard386405f2003-03-23 21:28:45 +0000763
bellardfd429f22003-03-30 20:59:46 +0000764TWIN [6] is a Windows API emulator like Wine. It is less accurate than
765Wine but includes a protected mode x86 interpreter to launch x86 Windows
766executables. Such an approach as greater potential because most of the
767Windows API is executed natively but it is far more difficult to develop
768because all the data structures and function parameters exchanged
769between the API and the x86 code must be converted.
770
bellard1eb20522003-06-25 16:21:49 +0000771User mode Linux [7] was the only solution before QEMU to launch a Linux
772kernel as a process while not needing any host kernel patches. However,
773user mode Linux requires heavy kernel patches while QEMU accepts
774unpatched Linux kernels. It would be interesting to compare the
775performance of the two approaches.
776
777The new Plex86 [8] PC virtualizer is done in the same spirit as the QEMU
778system emulator. It requires a patched Linux kernel to work (you cannot
779launch the same kernel on your PC), but the patches are really small. As
780it is a PC virtualizer (no emulation is done except for some priveledged
781instructions), it has the potential of being faster than QEMU. The
bellardd5a0b502003-06-27 12:02:03 +0000782downside is that a complicated (and potentially unsafe) host kernel
783patch is needed.
bellard1eb20522003-06-25 16:21:49 +0000784
bellard386405f2003-03-23 21:28:45 +0000785@section Portable dynamic translation
786
787QEMU is a dynamic translator. When it first encounters a piece of code,
788it converts it to the host instruction set. Usually dynamic translators
bellard322d0c62003-06-15 23:29:28 +0000789are very complicated and highly CPU dependent. QEMU uses some tricks
bellard386405f2003-03-23 21:28:45 +0000790which make it relatively easily portable and simple while achieving good
791performances.
792
793The basic idea is to split every x86 instruction into fewer simpler
794instructions. Each simple instruction is implemented by a piece of C
795code (see @file{op-i386.c}). Then a compile time tool (@file{dyngen})
796takes the corresponding object file (@file{op-i386.o}) to generate a
797dynamic code generator which concatenates the simple instructions to
798build a function (see @file{op-i386.h:dyngen_code()}).
799
800In essence, the process is similar to [1], but more work is done at
801compile time.
802
803A key idea to get optimal performances is that constant parameters can
804be passed to the simple operations. For that purpose, dummy ELF
805relocations are generated with gcc for each constant parameter. Then,
806the tool (@file{dyngen}) can locate the relocations and generate the
807appriopriate C code to resolve them when building the dynamic code.
808
809That way, QEMU is no more difficult to port than a dynamic linker.
810
811To go even faster, GCC static register variables are used to keep the
812state of the virtual CPU.
813
814@section Register allocation
815
816Since QEMU uses fixed simple instructions, no efficient register
817allocation can be done. However, because RISC CPUs have a lot of
818register, most of the virtual CPU state can be put in registers without
819doing complicated register allocation.
820
821@section Condition code optimisations
822
823Good CPU condition codes emulation (@code{EFLAGS} register on x86) is a
824critical point to get good performances. QEMU uses lazy condition code
825evaluation: instead of computing the condition codes after each x86
bellardfd429f22003-03-30 20:59:46 +0000826instruction, it just stores one operand (called @code{CC_SRC}), the
bellard386405f2003-03-23 21:28:45 +0000827result (called @code{CC_DST}) and the type of operation (called
828@code{CC_OP}).
829
830@code{CC_OP} is almost never explicitely set in the generated code
831because it is known at translation time.
832
833In order to increase performances, a backward pass is performed on the
834generated simple instructions (see
835@code{translate-i386.c:optimize_flags()}). When it can be proved that
836the condition codes are not needed by the next instructions, no
837condition codes are computed at all.
838
bellardfd429f22003-03-30 20:59:46 +0000839@section CPU state optimisations
bellard386405f2003-03-23 21:28:45 +0000840
841The x86 CPU has many internal states which change the way it evaluates
842instructions. In order to achieve a good speed, the translation phase
843considers that some state information of the virtual x86 CPU cannot
844change in it. For example, if the SS, DS and ES segments have a zero
845base, then the translator does not even generate an addition for the
846segment base.
847
848[The FPU stack pointer register is not handled that way yet].
849
850@section Translation cache
851
852A 2MByte cache holds the most recently used translations. For
853simplicity, it is completely flushed when it is full. A translation unit
854contains just a single basic block (a block of x86 instructions
855terminated by a jump or by a virtual CPU state change which the
856translator cannot deduce statically).
857
bellarddf0f11a2003-05-28 00:27:57 +0000858@section Direct block chaining
859
860After each translated basic block is executed, QEMU uses the simulated
861Program Counter (PC) and other cpu state informations (such as the CS
862segment base value) to find the next basic block.
863
864In order to accelerate the most common cases where the new simulated PC
865is known, QEMU can patch a basic block so that it jumps directly to the
866next one.
867
868The most portable code uses an indirect jump. An indirect jump makes it
869easier to make the jump target modification atomic. On some
870architectures (such as PowerPC), the @code{JUMP} opcode is directly
871patched so that the block chaining has no overhead.
872
873@section Self-modifying code and translated code invalidation
874
875Self-modifying code is a special challenge in x86 emulation because no
876instruction cache invalidation is signaled by the application when code
877is modified.
878
879When translated code is generated for a basic block, the corresponding
880host page is write protected if it is not already read-only (with the
881system call @code{mprotect()}). Then, if a write access is done to the
882page, Linux raises a SEGV signal. QEMU then invalidates all the
883translated code in the page and enables write accesses to the page.
884
885Correct translated code invalidation is done efficiently by maintaining
886a linked list of every translated block contained in a given page. Other
887linked lists are also maintained to undo direct block chaining.
888
bellard46907642003-07-07 12:17:46 +0000889Although the overhead of doing @code{mprotect()} calls is important,
bellarddf0f11a2003-05-28 00:27:57 +0000890most MSDOS programs can be emulated at reasonnable speed with QEMU and
891DOSEMU.
892
893Note that QEMU also invalidates pages of translated code when it detects
894that memory mappings are modified with @code{mmap()} or @code{munmap()}.
bellard386405f2003-03-23 21:28:45 +0000895
896@section Exception support
897
898longjmp() is used when an exception such as division by zero is
bellarddf0f11a2003-05-28 00:27:57 +0000899encountered.
bellard386405f2003-03-23 21:28:45 +0000900
bellarddf0f11a2003-05-28 00:27:57 +0000901The host SIGSEGV and SIGBUS signal handlers are used to get invalid
902memory accesses. The exact CPU state can be retrieved because all the
903x86 registers are stored in fixed host registers. The simulated program
904counter is found by retranslating the corresponding basic block and by
905looking where the host program counter was at the exception point.
906
907The virtual CPU cannot retrieve the exact @code{EFLAGS} register because
908in some cases it is not computed because of condition code
909optimisations. It is not a big concern because the emulated code can
910still be restarted in any cases.
bellard386405f2003-03-23 21:28:45 +0000911
912@section Linux system call translation
913
914QEMU includes a generic system call translator for Linux. It means that
915the parameters of the system calls can be converted to fix the
916endianness and 32/64 bit issues. The IOCTLs are converted with a generic
917type description system (see @file{ioctls.h} and @file{thunk.c}).
918
bellarddf0f11a2003-05-28 00:27:57 +0000919QEMU supports host CPUs which have pages bigger than 4KB. It records all
920the mappings the process does and try to emulated the @code{mmap()}
921system calls in cases where the host @code{mmap()} call would fail
922because of bad page alignment.
923
bellard386405f2003-03-23 21:28:45 +0000924@section Linux signals
925
926Normal and real-time signals are queued along with their information
927(@code{siginfo_t}) as it is done in the Linux kernel. Then an interrupt
928request is done to the virtual CPU. When it is interrupted, one queued
929signal is handled by generating a stack frame in the virtual CPU as the
930Linux kernel does. The @code{sigreturn()} system call is emulated to return
931from the virtual signal handler.
932
933Some signals (such as SIGALRM) directly come from the host. Other
934signals are synthetized from the virtual CPU exceptions such as SIGFPE
935when a division by zero is done (see @code{main.c:cpu_loop()}).
936
937The blocked signal mask is still handled by the host Linux kernel so
938that most signal system calls can be redirected directly to the host
939Linux kernel. Only the @code{sigaction()} and @code{sigreturn()} system
940calls need to be fully emulated (see @file{signal.c}).
941
942@section clone() system call and threads
943
944The Linux clone() system call is usually used to create a thread. QEMU
945uses the host clone() system call so that real host threads are created
946for each emulated thread. One virtual CPU instance is created for each
947thread.
948
949The virtual x86 CPU atomic operations are emulated with a global lock so
950that their semantic is preserved.
951
bellarddf0f11a2003-05-28 00:27:57 +0000952Note that currently there are still some locking issues in QEMU. In
953particular, the translated cache flush is not protected yet against
954reentrancy.
955
bellard1eb87252003-04-11 01:12:28 +0000956@section Self-virtualization
957
bellard46907642003-07-07 12:17:46 +0000958QEMU was conceived so that ultimately it can emulate itself. Although
bellard1eb87252003-04-11 01:12:28 +0000959it is not very useful, it is an important test to show the power of the
960emulator.
961
962Achieving self-virtualization is not easy because there may be address
bellard6cd9f352003-04-29 20:40:35 +0000963space conflicts. QEMU solves this problem by being an executable ELF
964shared object as the ld-linux.so ELF interpreter. That way, it can be
965relocated at load time.
bellard1eb87252003-04-11 01:12:28 +0000966
bellard1eb20522003-06-25 16:21:49 +0000967@section MMU emulation
968
969For system emulation, QEMU uses the mmap() system call to emulate the
970target CPU MMU. It works as long the emulated OS does not use an area
971reserved by the host OS (such as the area above 0xc0000000 on x86
972Linux).
973
974It is planned to add a slower but more precise MMU emulation
975with a software MMU.
976
bellard386405f2003-03-23 21:28:45 +0000977@section Bibliography
978
979@table @asis
980
981@item [1]
982@url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing
983direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio
984Riccardi.
985
986@item [2]
987@url{http://developer.kde.org/~sewardj/}, Valgrind, an open-source
988memory debugger for x86-GNU/Linux, by Julian Seward.
989
990@item [3]
991@url{http://bochs.sourceforge.net/}, the Bochs IA-32 Emulator Project,
992by Kevin Lawton et al.
993
994@item [4]
995@url{http://www.cs.rose-hulman.edu/~donaldlf/em86/index.html}, the EM86
996x86 emulator on Alpha-Linux.
997
998@item [5]
999@url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/full_papers/chernoff/chernoff.pdf},
1000DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton
1001Chernoff and Ray Hookway.
1002
bellardfd429f22003-03-30 20:59:46 +00001003@item [6]
1004@url{http://www.willows.com/}, Windows API library emulation from
1005Willows Software.
1006
bellard1eb20522003-06-25 16:21:49 +00001007@item [7]
1008@url{http://user-mode-linux.sourceforge.net/},
1009The User-mode Linux Kernel.
1010
1011@item [8]
1012@url{http://www.plex86.org/},
1013The new Plex86 project.
1014
bellard386405f2003-03-23 21:28:45 +00001015@end table
1016
1017@chapter Regression Tests
1018
bellard322d0c62003-06-15 23:29:28 +00001019In the directory @file{tests/}, various interesting testing programs
bellard386405f2003-03-23 21:28:45 +00001020are available. There are used for regression testing.
1021
bellard322d0c62003-06-15 23:29:28 +00001022@section @file{hello-i386}
bellard386405f2003-03-23 21:28:45 +00001023
1024Very simple statically linked x86 program, just to test QEMU during a
1025port to a new host CPU.
1026
bellard322d0c62003-06-15 23:29:28 +00001027@section @file{hello-arm}
1028
1029Very simple statically linked ARM program, just to test QEMU during a
1030port to a new host CPU.
1031
bellard386405f2003-03-23 21:28:45 +00001032@section @file{test-i386}
1033
1034This program executes most of the 16 bit and 32 bit x86 instructions and
1035generates a text output. It can be compared with the output obtained with
1036a real CPU or another emulator. The target @code{make test} runs this
1037program and a @code{diff} on the generated output.
1038
1039The Linux system call @code{modify_ldt()} is used to create x86 selectors
1040to test some 16 bit addressing and 32 bit with segmentation cases.
1041
bellarddf0f11a2003-05-28 00:27:57 +00001042The Linux system call @code{vm86()} is used to test vm86 emulation.
bellard386405f2003-03-23 21:28:45 +00001043
bellarddf0f11a2003-05-28 00:27:57 +00001044Various exceptions are raised to test most of the x86 user space
1045exception reporting.
bellard386405f2003-03-23 21:28:45 +00001046
1047@section @file{sha1}
1048
1049It is a simple benchmark. Care must be taken to interpret the results
1050because it mostly tests the ability of the virtual CPU to optimize the
1051@code{rol} x86 instruction and the condition code computations.
1052