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