1 \input texinfo @c -*- texinfo -*-
4 @settitle QEMU CPU Emulator Reference Documentation
7 @center @titlefont{QEMU CPU Emulator Reference Documentation}
16 QEMU is a FAST! processor emulator. By using dynamic translation it
17 achieves a reasonnable speed while being easy to port on new host
20 QEMU has two operating modes:
25 User mode emulation. In this mode, QEMU can launch Linux processes
26 compiled for one CPU on another CPU. Linux system calls are converted
27 because of endianness and 32/64 bit mismatches. The Wine Windows API
28 emulator (@url{http://www.winehq.org}) and the DOSEMU DOS emulator
29 (@url{www.dosemu.org}) are the main targets for QEMU.
32 Full system emulation. In this mode, QEMU emulates a full
33 system, including a processor and various peripherials. Currently, it
34 is only used to launch an x86 Linux kernel on an x86 Linux system. It
35 enables easier testing and debugging of system code. It can also be
36 used to provide virtual hosting of several virtual PCs on a single
41 As QEMU requires no host kernel patches to run, it is very safe and
44 QEMU generic features:
48 @item User space only or full system emulation.
50 @item Using dynamic translation to native code for reasonnable speed.
52 @item Working on x86 and PowerPC hosts. Being tested on ARM, Sparc32, Alpha and S390.
54 @item Self-modifying code support.
56 @item Precise exceptions support.
58 @item The virtual CPU is a library (@code{libqemu}) which can be used
63 QEMU user mode emulation features:
65 @item Generic Linux system call converter, including most ioctls.
67 @item clone() emulation using native CPU clone() to use Linux scheduler for threads.
69 @item Accurate signal handling by remapping host signals to target signals.
73 QEMU full system emulation features:
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.
78 @section x86 emulation
80 QEMU x86 target features:
84 @item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation.
85 LDT/GDT and IDT are emulated. VM86 mode is also supported to run DOSEMU.
87 @item Support of host page sizes bigger than 4KB in user mode emulation.
89 @item QEMU can emulate itself on x86.
91 @item An extensive Linux x86 CPU test program is included @file{tests/test-i386}.
92 It can be used to test other x86 virtual CPUs.
96 Current QEMU limitations:
100 @item No SSE/MMX support (yet).
102 @item No x86-64 support.
104 @item IPC syscalls are missing.
106 @item The x86 segment limits and access rights are not tested at every
109 @item On non x86 host CPUs, @code{double}s are used instead of the non standard
110 10 byte @code{long double}s of x86 for floating point emulation to get
111 maximum performances.
113 @item Some priviledged instructions or behaviors are missing, especially for segment protection testing (yet).
117 @section ARM emulation
121 @item ARM emulation can currently launch small programs while using the
122 generic dynamic code generation architecture of QEMU.
124 @item No FPU support (yet).
126 @item No automatic regression testing (yet).
130 @section SPARC emulation
132 The SPARC emulation is currently in development.
134 @chapter QEMU User space emulator invocation
138 If you need to compile QEMU, please read the @file{README} which gives
139 the related information.
141 In order to launch a Linux process, QEMU needs the process executable
142 itself and all the target (x86) dynamic libraries used by it.
146 @item On x86, you can just try to launch any process by using the native
150 qemu-i386 -L / /bin/ls
153 @code{-L /} tells that the x86 dynamic linker must be searched with a
156 @item Since QEMU is also a linux process, you can launch qemu with qemu:
159 qemu-i386 -L / qemu-i386 -L / /bin/ls
162 @item On non x86 CPUs, you need first to download at least an x86 glibc
163 (@file{qemu-XXX-i386-glibc21.tar.gz} on the QEMU web page). Ensure that
164 @code{LD_LIBRARY_PATH} is not set:
167 unset LD_LIBRARY_PATH
170 Then you can launch the precompiled @file{ls} x86 executable:
173 qemu-i386 tests/i386/ls
175 You can look at @file{qemu-binfmt-conf.sh} so that
176 QEMU is automatically launched by the Linux kernel when you try to
177 launch x86 executables. It requires the @code{binfmt_misc} module in the
180 @item The x86 version of QEMU is also included. You can try weird things such as:
182 qemu-i386 /usr/local/qemu-i386/bin/qemu-i386 /usr/local/qemu-i386/bin/ls-i386
191 @item Ensure that you have a working QEMU with the x86 glibc
192 distribution (see previous section). In order to verify it, you must be
196 qemu-i386 /usr/local/qemu-i386/bin/ls-i386
199 @item Download the binary x86 Wine install
200 (@file{qemu-XXX-i386-wine.tar.gz} on the QEMU web page).
202 @item Configure Wine on your account. Look at the provided script
203 @file{/usr/local/qemu-i386/bin/wine-conf.sh}. Your previous
204 @code{$@{HOME@}/.wine} directory is saved to @code{$@{HOME@}/.wine.org}.
206 @item Then you can try the example @file{putty.exe}:
209 qemu-i386 /usr/local/qemu-i386/wine/bin/wine /usr/local/qemu-i386/wine/c/Program\ Files/putty.exe
214 @section Command line options
217 usage: qemu-i386 [-h] [-d] [-L path] [-s size] program [arguments...]
224 Set the x86 elf interpreter prefix (default=/usr/local/qemu-i386)
226 Set the x86 stack size in bytes (default=524288)
233 Activate log (logfile=/tmp/qemu.log)
235 Act as if the host page size was 'pagesize' bytes
238 @chapter QEMU System emulator invocation
240 @section Introduction
242 @c man begin DESCRIPTION
244 The QEMU System emulator simulates a complete PC. It can either boot
245 directly a Linux kernel (without any BIOS or boot loader) or boot like a
246 real PC with the included BIOS.
248 In order to meet specific user needs, two versions of QEMU are
254 @code{qemu-fast} uses the host Memory Management Unit (MMU) to simulate
255 the x86 MMU. It is @emph{fast} but has limitations because the whole 4 GB
256 address space cannot be used and some memory mapped peripherials
257 cannot be emulated accurately yet. Therefore, a specific Linux kernel
258 must be used (@xref{linux_compile}).
261 @code{qemu} uses a software MMU. It is about @emph{two times
262 slower} but gives a more accurate emulation.
266 QEMU emulates the following PC peripherials:
270 VGA (hardware level, including all non standard modes)
272 PS/2 mouse and keyboard
274 IDE disk interface (port=0x1f0, irq=14)
276 NE2000 network adapter (port=0x300, irq=9)
278 Serial port (port=0x3f8, irq=4)
280 PIC (interrupt controler)
291 Download and uncompress the linux image (@file{linux.img}) and type:
297 Linux should boot and give you a prompt.
299 @section Direct Linux Boot and Network emulation
301 This section explains how to launch a Linux kernel inside QEMU without
302 having to make a full bootable image. It is very useful for fast Linux
303 kernel testing. The QEMU network configuration is also explained.
307 Download the archive @file{linux-test-xxx.tar.gz} containing a Linux
308 kernel and a disk image.
310 @item Optional: If you want network support (for example to launch X11 examples), you
311 must copy the script @file{qemu-ifup} in @file{/etc} and configure
312 properly @code{sudo} so that the command @code{ifconfig} contained in
313 @file{qemu-ifup} can be executed as root. You must verify that your host
314 kernel supports the TUN/TAP network interfaces: the device
315 @file{/dev/net/tun} must be present.
317 When network is enabled, there is a virtual network connection between
318 the host kernel and the emulated kernel. The emulated kernel is seen
319 from the host kernel at IP address 172.20.0.2 and the host kernel is
320 seen from the emulated kernel at IP address 172.20.0.1.
322 @item Launch @code{qemu.sh}. You should have the following output:
326 connected to host network interface: tun0
327 Uncompressing Linux... Ok, booting the kernel.
328 Linux 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
329 BIOS-provided physical RAM map:
330 BIOS-e801: 0000000000000000 - 000000000009f000 (usable)
331 BIOS-e801: 0000000000100000 - 0000000002000000 (usable)
332 32MB LOWMEM available.
333 On node 0 totalpages: 8192
337 Kernel command line: root=/dev/hda ide1=noprobe ide2=noprobe ide3=noprobe ide4=noprobe ide5=noprobe
338 ide_setup: ide1=noprobe
339 ide_setup: ide2=noprobe
340 ide_setup: ide3=noprobe
341 ide_setup: ide4=noprobe
342 ide_setup: ide5=noprobe
344 Detected 501.285 MHz processor.
345 Calibrating delay loop... 989.59 BogoMIPS
346 Memory: 29268k/32768k available (907k kernel code, 3112k reserved, 212k data, 52k init, 0k highmem)
347 Dentry cache hash table entries: 4096 (order: 3, 32768 bytes)
348 Inode cache hash table entries: 2048 (order: 2, 16384 bytes)
349 Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
350 Buffer-cache hash table entries: 1024 (order: 0, 4096 bytes)
351 Page-cache hash table entries: 8192 (order: 3, 32768 bytes)
352 CPU: Intel Pentium Pro stepping 03
353 Checking 'hlt' instruction... OK.
354 POSIX conformance testing by UNIFIX
355 Linux NET4.0 for Linux 2.4
356 Based upon Swansea University Computer Society NET3.039
357 Initializing RT netlink socket
360 Journalled Block Device driver loaded
361 pty: 256 Unix98 ptys configured
362 Serial driver version 5.05c (2001-07-08) with no serial options enabled
363 ttyS00 at 0x03f8 (irq = 4) is a 16450
364 Uniform Multi-Platform E-IDE driver Revision: 6.31
365 ide: Assuming 50MHz system bus speed for PIO modes; override with idebus=xx
366 hda: QEMU HARDDISK, ATA DISK drive
367 ide0 at 0x1f0-0x1f7,0x3f6 on irq 14
368 hda: 12288 sectors (6 MB) w/256KiB Cache, CHS=12/16/63
370 hda: unknown partition table
371 ne.c:v1.10 9/23/94 Donald Becker (becker@scyld.com)
372 Last modified Nov 1, 2000 by Paul Gortmaker
373 NE*000 ethercard probe at 0x300: 52 54 00 12 34 56
374 eth0: NE2000 found at 0x300, using IRQ 9.
375 RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
376 NET4: Linux TCP/IP 1.0 for NET4.0
377 IP Protocols: ICMP, UDP, TCP, IGMP
378 IP: routing cache hash table of 512 buckets, 4Kbytes
379 TCP: Hash tables configured (established 2048 bind 4096)
380 NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.
381 EXT2-fs warning: mounting unchecked fs, running e2fsck is recommended
382 VFS: Mounted root (ext2 filesystem).
383 Freeing unused kernel memory: 52k freed
384 sh: can't access tty; job control turned off
389 Then you can play with the kernel inside the virtual serial console. You
390 can launch @code{ls} for example. Type @key{Ctrl-a h} to have an help
391 about the keys you can type inside the virtual serial console. In
392 particular, use @key{Ctrl-a x} to exit QEMU and use @key{Ctrl-a b} as
396 If the network is enabled, launch the script @file{/etc/linuxrc} in the
397 emulator (don't forget the leading dot):
402 Then enable X11 connections on your PC from the emulated Linux:
407 You can now launch @file{xterm} or @file{xlogo} and verify that you have
408 a real Virtual Linux system !
415 A 2.5.74 kernel is also included in the archive. Just
416 replace the bzImage in qemu.sh to try it.
419 vl creates a temporary file in @var{$QEMU_TMPDIR} (@file{/tmp} is the
420 default) containing all the simulated PC memory. If possible, try to use
421 a temporary directory using the tmpfs filesystem to avoid too many
422 unnecessary disk accesses.
425 In order to exit cleanly for vl, you can do a @emph{shutdown} inside
426 vl. vl will automatically exit when the Linux shutdown is done.
429 You can boot slightly faster by disabling the probe of non present IDE
430 interfaces. To do so, add the following options on the kernel command
433 ide1=noprobe ide2=noprobe ide3=noprobe ide4=noprobe ide5=noprobe
437 The example disk image is a modified version of the one made by Kevin
438 Lawton for the plex86 Project (@url{www.plex86.org}).
445 @c man begin SYNOPSIS
446 usage: qemu [options] [disk_image]
451 @var{disk_image} is a raw hard image image for IDE hard disk 0.
457 Use @var{file} as hard disk 0 or 1 image (@xref{disk_images}).
461 Write to temporary files instead of disk image files. In this case,
462 the raw disk image you use is not written back. You can however force
463 the write back by pressing @key{C-a s} (@xref{disk_images}).
466 Set virtual RAM size to @var{megs} megabytes.
469 Set network init script [default=/etc/vl-ifup]. This script is
470 launched to configure the host network interface (usually tun0)
471 corresponding to the virtual NE2000 card.
474 Use @var{file} as initial ram disk.
477 Assumes @var{fd} talks to tap/tun and use it. Read
478 @url{http://bellard.org/qemu/tetrinet.html} to have an example of its
483 Normally, QEMU uses SDL to display the VGA output. With this option,
484 you can totally disable graphical output so that QEMU is a simple
485 command line application. The emulated serial port is redirected on
486 the console. Therefore, you can still use QEMU to debug a Linux kernel
487 with a serial console.
491 Linux boot specific (does not require a full PC boot with a BIOS):
494 @item -kernel bzImage
495 Use @var{bzImage} as kernel image.
497 @item -append cmdline
498 Use @var{cmdline} as kernel command line
501 Use @var{file} as initial ram disk.
508 Wait gdb connection to port 1234 (@xref{gdb_usage}).
510 Change gdb connection port.
512 Output log in /tmp/vl.log
515 During emulation, use @key{C-a h} to get terminal commands:
523 Save disk data back to file (if -snapshot)
525 Send break (magic sysrq)
534 @settitle QEMU System Emulator
537 The HTML documentation of QEMU for more precise information and Linux
538 user mode emulator invocation.
551 @subsection Raw disk images
553 The disk images can simply be raw images of the hard disk. You can
554 create them with the command:
556 dd if=/dev/zero of=myimage bs=1024 count=mysize
558 where @var{myimage} is the image filename and @var{mysize} is its size
561 @subsection Snapshot mode
563 If you use the option @option{-snapshot}, all disk images are
564 considered as read only. When sectors in written, they are written in
565 a temporary file created in @file{/tmp}. You can however force the
566 write back to the raw disk images by pressing @key{C-a s}.
568 NOTE: The snapshot mode only works with raw disk images.
570 @subsection Copy On Write disk images
572 QEMU also supports user mode Linux
573 (@url{http://user-mode-linux.sourceforge.net/}) Copy On Write (COW)
574 disk images. The COW disk images are much smaller than normal images
575 as they store only modified sectors. They also permit the use of the
576 same disk image template for many users.
578 To create a COW disk images, use the command:
581 qemu-mkcow -f myrawimage.bin mycowimage.cow
584 @file{myrawimage.bin} is a raw image you want to use as original disk
585 image. It will never be written to.
587 @file{mycowimage.cow} is the COW disk image which is created by
588 @code{qemu-mkcow}. You can use it directly with the @option{-hdx}
589 options. You must not modify the original raw disk image if you use
590 COW images, as COW images only store the modified sectors from the raw
591 disk image. QEMU stores the original raw disk image name and its
592 modified time in the COW disk image so that chances of mistakes are
595 If the raw disk image is not read-only, by pressing @key{C-a s} you
596 can flush the COW disk image back into the raw disk image, as in
599 COW disk images can also be created without a corresponding raw disk
600 image. It is useful to have a big initial virtual disk image without
601 using much disk space. Use:
604 qemu-mkcow mycowimage.cow 1024
607 to create a 1 gigabyte empty COW disk image.
612 COW disk images must be created on file systems supporting
613 @emph{holes} such as ext2 or ext3.
615 Since holes are used, the displayed size of the COW disk image is not
616 the real one. To know it, use the @code{ls -ls} command.
620 @section Linux Kernel Compilation
622 You 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
624 following changes to the Linux kernel (only 2.4.x and 2.5.x were
629 The kernel must be mapped at 0x90000000 (the default is
630 0xc0000000). You must modify only two lines in the kernel source:
632 In @file{include/asm/page.h}, replace
634 #define __PAGE_OFFSET (0xc0000000)
638 #define __PAGE_OFFSET (0x90000000)
641 And in @file{arch/i386/vmlinux.lds}, replace
643 . = 0xc0000000 + 0x100000;
647 . = 0x90000000 + 0x100000;
651 If you want to enable SMP (Symmetric Multi-Processing) support, you
652 must make the following change in @file{include/asm/fixmap.h}. Replace
654 #define FIXADDR_TOP (0xffffX000UL)
658 #define FIXADDR_TOP (0xa7ffX000UL)
660 (X is 'e' or 'f' depending on the kernel version). Although you can
661 use an SMP kernel with QEMU, it only supports one CPU.
664 If you are not using a 2.5 kernel as host kernel but if you use a target
665 2.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
667 frequencies greater than 100 Hz on host Linux systems < 2.5. In
668 @file{include/asm/param.h}, replace:
671 # define HZ 1000 /* Internal kernel timer frequency */
675 # define HZ 100 /* Internal kernel timer frequency */
680 The file config-2.x.x gives the configuration of the example kernels.
687 As you would do to make a real kernel. Then you can use with QEMU
688 exactly the same kernel as you would boot on your PC (in
689 @file{arch/i386/boot/bzImage}).
694 QEMU has a primitive support to work with gdb, so that you can do
695 'Ctrl-C' while the virtual machine is running and inspect its state.
697 In order to use gdb, launch vl with the '-s' option. It will wait for a
700 > vl -s arch/i386/boot/bzImage -hda root-2.4.20.img root=/dev/hda
701 Connected to host network interface: tun0
702 Waiting gdb connection on port 1234
705 Then launch gdb on the 'vmlinux' executable:
710 In gdb, connect to QEMU:
712 (gdb) target remote locahost:1234
715 Then you can use gdb normally. For example, type 'c' to launch the kernel:
720 Here are some useful tips in order to use gdb on system code:
724 Use @code{info reg} to display all the CPU registers.
726 Use @code{x/10i $eip} to display the code at the PC position.
728 Use @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.
732 @chapter QEMU Internals
734 @section QEMU compared to other emulators
736 Like bochs [3], QEMU emulates an x86 CPU. But QEMU is much faster than
737 bochs as it uses dynamic compilation and because it uses the host MMU to
738 simulate the x86 MMU. The downside is that currently the emulation is
739 not as accurate as bochs (for example, you cannot currently run Windows
742 Like Valgrind [2], QEMU does user space emulation and dynamic
743 translation. Valgrind is mainly a memory debugger while QEMU has no
744 support for it (QEMU could be used to detect out of bound memory
745 accesses as Valgrind, but it has no support to track uninitialised data
746 as Valgrind does). The Valgrind dynamic translator generates better code
747 than QEMU (in particular it does register allocation) but it is closely
748 tied to an x86 host and target and has no support for precise exceptions
749 and system emulation.
751 EM86 [4] is the closest project to user space QEMU (and QEMU still uses
752 some of its code, in particular the ELF file loader). EM86 was limited
753 to an alpha host and used a proprietary and slow interpreter (the
754 interpreter part of the FX!32 Digital Win32 code translator [5]).
756 TWIN [6] is a Windows API emulator like Wine. It is less accurate than
757 Wine but includes a protected mode x86 interpreter to launch x86 Windows
758 executables. Such an approach as greater potential because most of the
759 Windows API is executed natively but it is far more difficult to develop
760 because all the data structures and function parameters exchanged
761 between the API and the x86 code must be converted.
763 User mode Linux [7] was the only solution before QEMU to launch a Linux
764 kernel as a process while not needing any host kernel patches. However,
765 user mode Linux requires heavy kernel patches while QEMU accepts
766 unpatched Linux kernels. It would be interesting to compare the
767 performance of the two approaches.
769 The new Plex86 [8] PC virtualizer is done in the same spirit as the QEMU
770 system emulator. It requires a patched Linux kernel to work (you cannot
771 launch the same kernel on your PC), but the patches are really small. As
772 it is a PC virtualizer (no emulation is done except for some priveledged
773 instructions), it has the potential of being faster than QEMU. The
774 downside is that a complicated (and potentially unsafe) host kernel
777 @section Portable dynamic translation
779 QEMU is a dynamic translator. When it first encounters a piece of code,
780 it converts it to the host instruction set. Usually dynamic translators
781 are very complicated and highly CPU dependent. QEMU uses some tricks
782 which make it relatively easily portable and simple while achieving good
785 The basic idea is to split every x86 instruction into fewer simpler
786 instructions. Each simple instruction is implemented by a piece of C
787 code (see @file{op-i386.c}). Then a compile time tool (@file{dyngen})
788 takes the corresponding object file (@file{op-i386.o}) to generate a
789 dynamic code generator which concatenates the simple instructions to
790 build a function (see @file{op-i386.h:dyngen_code()}).
792 In essence, the process is similar to [1], but more work is done at
795 A key idea to get optimal performances is that constant parameters can
796 be passed to the simple operations. For that purpose, dummy ELF
797 relocations are generated with gcc for each constant parameter. Then,
798 the tool (@file{dyngen}) can locate the relocations and generate the
799 appriopriate C code to resolve them when building the dynamic code.
801 That way, QEMU is no more difficult to port than a dynamic linker.
803 To go even faster, GCC static register variables are used to keep the
804 state of the virtual CPU.
806 @section Register allocation
808 Since QEMU uses fixed simple instructions, no efficient register
809 allocation can be done. However, because RISC CPUs have a lot of
810 register, most of the virtual CPU state can be put in registers without
811 doing complicated register allocation.
813 @section Condition code optimisations
815 Good CPU condition codes emulation (@code{EFLAGS} register on x86) is a
816 critical point to get good performances. QEMU uses lazy condition code
817 evaluation: instead of computing the condition codes after each x86
818 instruction, it just stores one operand (called @code{CC_SRC}), the
819 result (called @code{CC_DST}) and the type of operation (called
822 @code{CC_OP} is almost never explicitely set in the generated code
823 because it is known at translation time.
825 In order to increase performances, a backward pass is performed on the
826 generated simple instructions (see
827 @code{translate-i386.c:optimize_flags()}). When it can be proved that
828 the condition codes are not needed by the next instructions, no
829 condition codes are computed at all.
831 @section CPU state optimisations
833 The x86 CPU has many internal states which change the way it evaluates
834 instructions. In order to achieve a good speed, the translation phase
835 considers that some state information of the virtual x86 CPU cannot
836 change in it. For example, if the SS, DS and ES segments have a zero
837 base, then the translator does not even generate an addition for the
840 [The FPU stack pointer register is not handled that way yet].
842 @section Translation cache
844 A 2MByte cache holds the most recently used translations. For
845 simplicity, it is completely flushed when it is full. A translation unit
846 contains just a single basic block (a block of x86 instructions
847 terminated by a jump or by a virtual CPU state change which the
848 translator cannot deduce statically).
850 @section Direct block chaining
852 After each translated basic block is executed, QEMU uses the simulated
853 Program Counter (PC) and other cpu state informations (such as the CS
854 segment base value) to find the next basic block.
856 In order to accelerate the most common cases where the new simulated PC
857 is known, QEMU can patch a basic block so that it jumps directly to the
860 The most portable code uses an indirect jump. An indirect jump makes it
861 easier to make the jump target modification atomic. On some
862 architectures (such as PowerPC), the @code{JUMP} opcode is directly
863 patched so that the block chaining has no overhead.
865 @section Self-modifying code and translated code invalidation
867 Self-modifying code is a special challenge in x86 emulation because no
868 instruction cache invalidation is signaled by the application when code
871 When translated code is generated for a basic block, the corresponding
872 host page is write protected if it is not already read-only (with the
873 system call @code{mprotect()}). Then, if a write access is done to the
874 page, Linux raises a SEGV signal. QEMU then invalidates all the
875 translated code in the page and enables write accesses to the page.
877 Correct translated code invalidation is done efficiently by maintaining
878 a linked list of every translated block contained in a given page. Other
879 linked lists are also maintained to undo direct block chaining.
881 Although the overhead of doing @code{mprotect()} calls is important,
882 most MSDOS programs can be emulated at reasonnable speed with QEMU and
885 Note that QEMU also invalidates pages of translated code when it detects
886 that memory mappings are modified with @code{mmap()} or @code{munmap()}.
888 @section Exception support
890 longjmp() is used when an exception such as division by zero is
893 The host SIGSEGV and SIGBUS signal handlers are used to get invalid
894 memory accesses. The exact CPU state can be retrieved because all the
895 x86 registers are stored in fixed host registers. The simulated program
896 counter is found by retranslating the corresponding basic block and by
897 looking where the host program counter was at the exception point.
899 The virtual CPU cannot retrieve the exact @code{EFLAGS} register because
900 in some cases it is not computed because of condition code
901 optimisations. It is not a big concern because the emulated code can
902 still be restarted in any cases.
904 @section Linux system call translation
906 QEMU includes a generic system call translator for Linux. It means that
907 the parameters of the system calls can be converted to fix the
908 endianness and 32/64 bit issues. The IOCTLs are converted with a generic
909 type description system (see @file{ioctls.h} and @file{thunk.c}).
911 QEMU supports host CPUs which have pages bigger than 4KB. It records all
912 the mappings the process does and try to emulated the @code{mmap()}
913 system calls in cases where the host @code{mmap()} call would fail
914 because of bad page alignment.
916 @section Linux signals
918 Normal 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
920 request is done to the virtual CPU. When it is interrupted, one queued
921 signal is handled by generating a stack frame in the virtual CPU as the
922 Linux kernel does. The @code{sigreturn()} system call is emulated to return
923 from the virtual signal handler.
925 Some signals (such as SIGALRM) directly come from the host. Other
926 signals are synthetized from the virtual CPU exceptions such as SIGFPE
927 when a division by zero is done (see @code{main.c:cpu_loop()}).
929 The blocked signal mask is still handled by the host Linux kernel so
930 that most signal system calls can be redirected directly to the host
931 Linux kernel. Only the @code{sigaction()} and @code{sigreturn()} system
932 calls need to be fully emulated (see @file{signal.c}).
934 @section clone() system call and threads
936 The Linux clone() system call is usually used to create a thread. QEMU
937 uses the host clone() system call so that real host threads are created
938 for each emulated thread. One virtual CPU instance is created for each
941 The virtual x86 CPU atomic operations are emulated with a global lock so
942 that their semantic is preserved.
944 Note that currently there are still some locking issues in QEMU. In
945 particular, the translated cache flush is not protected yet against
948 @section Self-virtualization
950 QEMU was conceived so that ultimately it can emulate itself. Although
951 it is not very useful, it is an important test to show the power of the
954 Achieving self-virtualization is not easy because there may be address
955 space conflicts. QEMU solves this problem by being an executable ELF
956 shared object as the ld-linux.so ELF interpreter. That way, it can be
957 relocated at load time.
959 @section MMU emulation
961 For system emulation, QEMU uses the mmap() system call to emulate the
962 target CPU MMU. It works as long the emulated OS does not use an area
963 reserved by the host OS (such as the area above 0xc0000000 on x86
966 It is planned to add a slower but more precise MMU emulation
969 @section Bibliography
974 @url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing
975 direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio
979 @url{http://developer.kde.org/~sewardj/}, Valgrind, an open-source
980 memory debugger for x86-GNU/Linux, by Julian Seward.
983 @url{http://bochs.sourceforge.net/}, the Bochs IA-32 Emulator Project,
984 by Kevin Lawton et al.
987 @url{http://www.cs.rose-hulman.edu/~donaldlf/em86/index.html}, the EM86
988 x86 emulator on Alpha-Linux.
991 @url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/full_papers/chernoff/chernoff.pdf},
992 DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton
993 Chernoff and Ray Hookway.
996 @url{http://www.willows.com/}, Windows API library emulation from
1000 @url{http://user-mode-linux.sourceforge.net/},
1001 The User-mode Linux Kernel.
1004 @url{http://www.plex86.org/},
1005 The new Plex86 project.
1009 @chapter Regression Tests
1011 In the directory @file{tests/}, various interesting testing programs
1012 are available. There are used for regression testing.
1014 @section @file{test-i386}
1016 This program executes most of the 16 bit and 32 bit x86 instructions and
1017 generates a text output. It can be compared with the output obtained with
1018 a real CPU or another emulator. The target @code{make test} runs this
1019 program and a @code{diff} on the generated output.
1021 The Linux system call @code{modify_ldt()} is used to create x86 selectors
1022 to test some 16 bit addressing and 32 bit with segmentation cases.
1024 The Linux system call @code{vm86()} is used to test vm86 emulation.
1026 Various exceptions are raised to test most of the x86 user space
1027 exception reporting.
1029 @section @file{linux-test}
1031 This program tests various Linux system calls. It is used to verify
1032 that the system call parameters are correctly converted between target
1035 @section @file{hello-i386}
1037 Very simple statically linked x86 program, just to test QEMU during a
1038 port to a new host CPU.
1040 @section @file{hello-arm}
1042 Very simple statically linked ARM program, just to test QEMU during a
1043 port to a new host CPU.
1045 @section @file{sha1}
1047 It is a simple benchmark. Care must be taken to interpret the results
1048 because it mostly tests the ability of the virtual CPU to optimize the
1049 @code{rol} x86 instruction and the condition code computations.