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