blob: 0c062f5faab36e0e119bce3f30c2eb464595066a [file] [log] [blame]
bellard386405f2003-03-23 21:28:45 +00001\input texinfo @c -*- texinfo -*-
2
bellard322d0c62003-06-15 23:29:28 +00003@settitle QEMU CPU Emulator Reference Documentation
bellard386405f2003-03-23 21:28:45 +00004@titlepage
5@sp 7
bellard322d0c62003-06-15 23:29:28 +00006@center @titlefont{QEMU CPU Emulator Reference Documentation}
bellard386405f2003-03-23 21:28:45 +00007@sp 3
8@end titlepage
9
10@chapter Introduction
11
bellard322d0c62003-06-15 23:29:28 +000012@section Features
bellard386405f2003-03-23 21:28:45 +000013
bellard1eb20522003-06-25 16:21:49 +000014QEMU is a FAST! processor emulator. By using dynamic translation it
15achieves a reasonnable speed while being easy to port on new host
16CPUs.
17
18QEMU has two operating modes:
19@itemize
20@item User mode emulation. In this mode, QEMU can launch Linux processes
21compiled for one CPU on another CPU. Linux system calls are converted
22because of endianness and 32/64 bit mismatches. The Wine Windows API
23emulator (@url{http://www.winehq.org}) and the DOSEMU DOS emulator
24(@url{www.dosemu.org}) are the main targets for QEMU.
25
26@item Full system emulation. In this mode, QEMU emulates a full
27system, including a processor and various peripherials. Currently, it
28is only used to launch an x86 Linux kernel on an x86 Linux system. It
29enables easier testing and debugging of system code. It can also be
30used to provide virtual hosting of several virtual PCs on a single
31server.
32
33@end itemize
34
35As QEMU requires no host kernel patches to run, it is very safe and
36easy to use.
bellard322d0c62003-06-15 23:29:28 +000037
38QEMU generic features:
bellard386405f2003-03-23 21:28:45 +000039
40@itemize
41
bellard1eb20522003-06-25 16:21:49 +000042@item User space only or full system emulation.
bellard386405f2003-03-23 21:28:45 +000043
bellardfd429f22003-03-30 20:59:46 +000044@item Using dynamic translation to native code for reasonnable speed.
bellard386405f2003-03-23 21:28:45 +000045
bellard1eb20522003-06-25 16:21:49 +000046@item Working on x86 and PowerPC hosts. Being tested on ARM, Sparc32, Alpha and S390.
47
48@item Self-modifying code support.
49
bellardd5a0b502003-06-27 12:02:03 +000050@item Precise exceptions support.
bellard1eb20522003-06-25 16:21:49 +000051
52@item The virtual CPU is a library (@code{libqemu}) which can be used
53in other projects.
54
55@end itemize
56
57QEMU user mode emulation features:
58@itemize
bellard386405f2003-03-23 21:28:45 +000059@item Generic Linux system call converter, including most ioctls.
60
61@item clone() emulation using native CPU clone() to use Linux scheduler for threads.
62
bellard322d0c62003-06-15 23:29:28 +000063@item Accurate signal handling by remapping host signals to target signals.
bellard1eb20522003-06-25 16:21:49 +000064@end itemize
65@end itemize
bellarddf0f11a2003-05-28 00:27:57 +000066
bellard1eb20522003-06-25 16:21:49 +000067QEMU full system emulation features:
68@itemize
69@item Using mmap() system calls to simulate the MMU
bellard322d0c62003-06-15 23:29:28 +000070@end itemize
71
72@section x86 emulation
73
74QEMU x86 target features:
75
76@itemize
77
78@item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation.
bellard1eb20522003-06-25 16:21:49 +000079LDT/GDT and IDT are emulated. VM86 mode is also supported to run DOSEMU.
bellard322d0c62003-06-15 23:29:28 +000080
bellard1eb20522003-06-25 16:21:49 +000081@item Support of host page sizes bigger than 4KB in user mode emulation.
bellarddf0f11a2003-05-28 00:27:57 +000082
83@item QEMU can emulate itself on x86.
bellard1eb87252003-04-11 01:12:28 +000084
bellard386405f2003-03-23 21:28:45 +000085@item An extensive Linux x86 CPU test program is included @file{tests/test-i386}.
86It can be used to test other x86 virtual CPUs.
87
88@end itemize
89
bellarddf0f11a2003-05-28 00:27:57 +000090Current QEMU limitations:
bellard386405f2003-03-23 21:28:45 +000091
92@itemize
93
bellard386405f2003-03-23 21:28:45 +000094@item No SSE/MMX support (yet).
95
96@item No x86-64 support.
97
bellarddf0f11a2003-05-28 00:27:57 +000098@item IPC syscalls are missing.
bellard386405f2003-03-23 21:28:45 +000099
100@item The x86 segment limits and access rights are not tested at every
bellard1eb20522003-06-25 16:21:49 +0000101memory access.
bellard386405f2003-03-23 21:28:45 +0000102
103@item On non x86 host CPUs, @code{double}s are used instead of the non standard
10410 byte @code{long double}s of x86 for floating point emulation to get
105maximum performances.
106
bellard1eb20522003-06-25 16:21:49 +0000107@item Full system emulation only works if no data are mapped above the virtual address
1080xc0000000 (yet).
109
110@item Some priviledged instructions or behaviors are missing. Only the ones
111needed for proper Linux kernel operation are emulated.
112
113@item No memory separation between the kernel and the user processes is done.
114It will be implemented very soon.
115
bellard386405f2003-03-23 21:28:45 +0000116@end itemize
117
bellard322d0c62003-06-15 23:29:28 +0000118@section ARM emulation
119
120@itemize
121
122@item ARM emulation can currently launch small programs while using the
123generic dynamic code generation architecture of QEMU.
124
125@item No FPU support (yet).
126
127@item No automatic regression testing (yet).
128
129@end itemize
130
bellardd5a0b502003-06-27 12:02:03 +0000131@chapter QEMU User space emulator invocation
bellard386405f2003-03-23 21:28:45 +0000132
bellardd691f662003-03-24 21:58:34 +0000133@section Quick Start
134
bellard322d0c62003-06-15 23:29:28 +0000135If you need to compile QEMU, please read the @file{README} which gives
136the related information.
137
bellard386405f2003-03-23 21:28:45 +0000138In order to launch a Linux process, QEMU needs the process executable
bellardd691f662003-03-24 21:58:34 +0000139itself and all the target (x86) dynamic libraries used by it.
bellard386405f2003-03-23 21:28:45 +0000140
bellardd691f662003-03-24 21:58:34 +0000141@itemize
142
143@item On x86, you can just try to launch any process by using the native
144libraries:
bellard386405f2003-03-23 21:28:45 +0000145
146@example
bellardd691f662003-03-24 21:58:34 +0000147qemu -L / /bin/ls
bellard386405f2003-03-23 21:28:45 +0000148@end example
149
bellardd691f662003-03-24 21:58:34 +0000150@code{-L /} tells that the x86 dynamic linker must be searched with a
151@file{/} prefix.
bellard386405f2003-03-23 21:28:45 +0000152
bellard1eb87252003-04-11 01:12:28 +0000153@item Since QEMU is also a linux process, you can launch qemu with qemu:
154
155@example
156qemu -L / qemu -L / /bin/ls
157@end example
bellard386405f2003-03-23 21:28:45 +0000158
bellardd691f662003-03-24 21:58:34 +0000159@item On non x86 CPUs, you need first to download at least an x86 glibc
bellard1eb87252003-04-11 01:12:28 +0000160(@file{qemu-XXX-i386-glibc21.tar.gz} on the QEMU web page). Ensure that
bellard644c4332003-03-24 23:00:36 +0000161@code{LD_LIBRARY_PATH} is not set:
162
163@example
164unset LD_LIBRARY_PATH
165@end example
166
167Then you can launch the precompiled @file{ls} x86 executable:
168
bellardd691f662003-03-24 21:58:34 +0000169@example
bellard168485b2003-03-29 16:57:34 +0000170qemu /usr/local/qemu-i386/bin/ls-i386
bellard386405f2003-03-23 21:28:45 +0000171@end example
bellard168485b2003-03-29 16:57:34 +0000172You can look at @file{/usr/local/qemu-i386/bin/qemu-conf.sh} so that
173QEMU is automatically launched by the Linux kernel when you try to
174launch x86 executables. It requires the @code{binfmt_misc} module in the
175Linux kernel.
176
bellard1eb87252003-04-11 01:12:28 +0000177@item The x86 version of QEMU is also included. You can try weird things such as:
178@example
179qemu /usr/local/qemu-i386/bin/qemu-i386 /usr/local/qemu-i386/bin/ls-i386
180@end example
181
bellard168485b2003-03-29 16:57:34 +0000182@end itemize
183
bellarddf0f11a2003-05-28 00:27:57 +0000184@section Wine launch
bellard168485b2003-03-29 16:57:34 +0000185
186@itemize
187
188@item Ensure that you have a working QEMU with the x86 glibc
189distribution (see previous section). In order to verify it, you must be
190able to do:
191
192@example
193qemu /usr/local/qemu-i386/bin/ls-i386
194@end example
195
bellardfd429f22003-03-30 20:59:46 +0000196@item Download the binary x86 Wine install
bellard1eb87252003-04-11 01:12:28 +0000197(@file{qemu-XXX-i386-wine.tar.gz} on the QEMU web page).
bellard168485b2003-03-29 16:57:34 +0000198
bellardfd429f22003-03-30 20:59:46 +0000199@item Configure Wine on your account. Look at the provided script
bellard168485b2003-03-29 16:57:34 +0000200@file{/usr/local/qemu-i386/bin/wine-conf.sh}. Your previous
201@code{$@{HOME@}/.wine} directory is saved to @code{$@{HOME@}/.wine.org}.
202
203@item Then you can try the example @file{putty.exe}:
204
205@example
206qemu /usr/local/qemu-i386/wine/bin/wine /usr/local/qemu-i386/wine/c/Program\ Files/putty.exe
207@end example
bellardd691f662003-03-24 21:58:34 +0000208
209@end itemize
210
211@section Command line options
212
213@example
214usage: qemu [-h] [-d] [-L path] [-s size] program [arguments...]
215@end example
216
bellarddf0f11a2003-05-28 00:27:57 +0000217@table @option
bellardd691f662003-03-24 21:58:34 +0000218@item -h
219Print the help
bellardd691f662003-03-24 21:58:34 +0000220@item -L path
221Set the x86 elf interpreter prefix (default=/usr/local/qemu-i386)
222@item -s size
223Set the x86 stack size in bytes (default=524288)
224@end table
bellard386405f2003-03-23 21:28:45 +0000225
bellarddf0f11a2003-05-28 00:27:57 +0000226Debug options:
227
228@table @option
229@item -d
230Activate log (logfile=/tmp/qemu.log)
231@item -p pagesize
232Act as if the host page size was 'pagesize' bytes
233@end table
234
bellard1eb20522003-06-25 16:21:49 +0000235@chapter QEMU System emulator invocation
236
237@section Quick Start
238
239This section explains how to launch a Linux kernel inside QEMU.
240
241@enumerate
242@item
243Download the archive @file{vl-test-xxx.tar.gz} containing a Linux kernel
244and an initrd (initial Ram Disk). The archive also contains a
245precompiled version of @file{vl}, the QEMU System emulator.
246
247@item Optional: If you want network support (for example to launch X11 examples), you
248must copy the script @file{vl-ifup} in @file{/etc} and configure
249properly @code{sudo} so that the command @code{ifconfig} contained in
250@file{vl-ifup} can be executed as root. You must verify that your host
251kernel supports the TUN/TAP network interfaces: the device
252@file{/dev/net/tun} must be present.
253
254When network is enabled, there is a virtual network connection between
255the host kernel and the emulated kernel. The emulated kernel is seen
256from the host kernel at IP address 172.20.0.2 and the host kernel is
257seen from the emulated kernel at IP address 172.20.0.1.
258
259@item Launch @code{vl.sh}. You should have the following output:
260
261@example
262> ./vl.sh
263connected to host network interface: tun0
264Uncompressing Linux... Ok, booting the kernel.
265Linux version 2.4.20 (bellard@voyager) (gcc version 2.95.2 20000220 (Debian GNU/Linux)) #42 Wed Jun 25 14:16:12 CEST 2003
266BIOS-provided physical RAM map:
267 BIOS-88: 0000000000000000 - 000000000009f000 (usable)
268 BIOS-88: 0000000000100000 - 0000000002000000 (usable)
26932MB LOWMEM available.
270On node 0 totalpages: 8192
271zone(0): 4096 pages.
272zone(1): 4096 pages.
273zone(2): 0 pages.
274Kernel command line: root=/dev/ram ramdisk_size=6144
275Initializing CPU#0
276Detected 501.785 MHz processor.
277Calibrating delay loop... 973.20 BogoMIPS
278Memory: 24776k/32768k available (725k kernel code, 7604k reserved, 151k data, 48k init, 0k highmem)
279Dentry cache hash table entries: 4096 (order: 3, 32768 bytes)
280Inode cache hash table entries: 2048 (order: 2, 16384 bytes)
281Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
282Buffer-cache hash table entries: 1024 (order: 0, 4096 bytes)
283Page-cache hash table entries: 8192 (order: 3, 32768 bytes)
284CPU: Intel Pentium Pro stepping 03
285Checking 'hlt' instruction... OK.
286POSIX conformance testing by UNIFIX
287Linux NET4.0 for Linux 2.4
288Based upon Swansea University Computer Society NET3.039
289Initializing RT netlink socket
290apm: BIOS not found.
291Starting kswapd
292pty: 256 Unix98 ptys configured
293Serial driver version 5.05c (2001-07-08) with no serial options enabled
294ttyS00 at 0x03f8 (irq = 4) is a 16450
295ne.c:v1.10 9/23/94 Donald Becker (becker@scyld.com)
296Last modified Nov 1, 2000 by Paul Gortmaker
297NE*000 ethercard probe at 0x300: 52 54 00 12 34 56
298eth0: NE2000 found at 0x300, using IRQ 9.
299RAMDISK driver initialized: 16 RAM disks of 6144K size 1024 blocksize
300NET4: Linux TCP/IP 1.0 for NET4.0
301IP Protocols: ICMP, UDP, TCP, IGMP
302IP: routing cache hash table of 512 buckets, 4Kbytes
303TCP: Hash tables configured (established 2048 bind 2048)
304NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.
305RAMDISK: ext2 filesystem found at block 0
306RAMDISK: Loading 6144 blocks [1 disk] into ram disk... done.
307Freeing initrd memory: 6144k freed
308VFS: Mounted root (ext2 filesystem).
309Freeing unused kernel memory: 48k freed
310sh: can't access tty; job control turned off
311#
312@end example
313
314@item
315Then you can play with the kernel inside the virtual serial console. You
316can launch @code{ls} for example. Type @key{Ctrl-a h} to have an help
317about the keys you can type inside the virtual serial console. In
bellardd5a0b502003-06-27 12:02:03 +0000318particular, use @key{Ctrl-a x} to exit QEMU and use @key{Ctrl-a b} as
319the Magic SysRq key.
bellard1eb20522003-06-25 16:21:49 +0000320
321@item
322If the network is enabled, launch the script @file{/etc/linuxrc} in the
323emulator (don't forget the leading dot):
324@example
325. /etc/linuxrc
326@end example
327
328Then enable X11 connections on your PC from the emulated Linux:
329@example
330xhost +172.20.0.2
331@end example
332
333You can now launch @file{xterm} or @file{xlogo} and verify that you have
334a real Virtual Linux system !
335
336@end enumerate
337
bellardd5a0b502003-06-27 12:02:03 +0000338NOTES:
339@enumerate
340@item
341A 2.5.66 kernel is also included in the vl-test archive. Just
342replace the bzImage in vl.sh to try it.
343
344@item
345vl creates a temporary file in @var{$VLTMPDIR} (@file{/tmp} is the
346default) containing all the simulated PC memory. If possible, try to use
347a temporary directory using the tmpfs filesystem to avoid too many
348unnecessary disk accesses.
349
350@item
351The example initrd is a modified version of the one made by Kevin
bellard1eb20522003-06-25 16:21:49 +0000352Lawton for the plex86 Project (@url{www.plex86.org}).
353
bellardd5a0b502003-06-27 12:02:03 +0000354@end enumerate
355
bellard1eb20522003-06-25 16:21:49 +0000356@section Kernel Compilation
357
358You can use any Linux kernel within QEMU provided it is mapped at
359address 0x90000000 (the default is 0xc0000000). You must modify only two
360lines in the kernel source:
361
362In asm/page.h, replace
363@example
364#define __PAGE_OFFSET (0xc0000000)
365@end example
366by
367@example
368#define __PAGE_OFFSET (0x90000000)
369@end example
370
371And in arch/i386/vmlinux.lds, replace
372@example
373 . = 0xc0000000 + 0x100000;
374@end example
375by
376@example
377 . = 0x90000000 + 0x100000;
378@end example
379
380The file config-2.4.20 gives the configuration of the example kernel.
381
382Just type
383@example
384make bzImage
385@end example
386
387As you would do to make a real kernel. Then you can use with QEMU
388exactly the same kernel as you would boot on your PC (in
389@file{arch/i386/boot/bzImage}).
390
bellardd5a0b502003-06-27 12:02:03 +0000391If you are not using a 2.5 kernel as host kernel but if you use a target
3922.5 kernel, you must also ensure that the 'HZ' define is set to 100
393(1000 is the default) as QEMU cannot currently emulate timers at
394frequencies greater than 100 Hz on host Linux systems < 2.5. In
395asm/param.h, replace:
396
397@example
398# define HZ 1000 /* Internal kernel timer frequency */
399@end example
400by
401@example
402# define HZ 100 /* Internal kernel timer frequency */
403@end example
404
bellardda415d52003-06-27 18:50:50 +0000405If you have problems running your kernel, verify that neither the SMP nor
406HIGHMEM configuration options are activated.
407
bellard1eb20522003-06-25 16:21:49 +0000408@section PC Emulation
409
410QEMU emulates the following PC peripherials:
411
412@itemize
413@item
414PIC (interrupt controler)
415@item
416PIT (timers)
417@item
418CMOS memory
419@item
420Serial port (port=0x3f8, irq=4)
421@item
422NE2000 network adapter (port=0x300, irq=9)
423@item
bellardd5a0b502003-06-27 12:02:03 +0000424Dumb VGA (to print the @code{Uncompressing Linux} message)
bellard1eb20522003-06-25 16:21:49 +0000425@end itemize
426
bellardda415d52003-06-27 18:50:50 +0000427@section GDB usage
428
429QEMU has a primitive support to work with gdb, so that you can do
430'Ctrl-C' while the kernel is running and inspect its state.
431
432In order to use gdb, launch vl with the '-s' option. It will wait for a
433gdb connection:
434@example
435> vl -s arch/i386/boot/bzImage initrd-2.4.20.img root=/dev/ram0 ramdisk_size=6144
436Connected to host network interface: tun0
437Waiting gdb connection on port 1234
438@end example
439
440Then launch gdb on the 'vmlinux' executable:
441@example
442> gdb vmlinux
443@end example
444
445In gdb, connect to QEMU:
446@example
447(gdb) target remote locahost:1234
448@end example
449
450Then you can use gdb normally. For example, type 'c' to launch the kernel:
451@example
452(gdb) c
453@end example
454
455WARNING: breakpoints and single stepping are not yet supported.
456
bellard386405f2003-03-23 21:28:45 +0000457@chapter QEMU Internals
458
459@section QEMU compared to other emulators
460
bellard1eb20522003-06-25 16:21:49 +0000461Like bochs [3], QEMU emulates an x86 CPU. But QEMU is much faster than
462bochs as it uses dynamic compilation and because it uses the host MMU to
463simulate the x86 MMU. The downside is that currently the emulation is
464not as accurate as bochs (for example, you cannot currently run Windows
465inside QEMU).
bellard386405f2003-03-23 21:28:45 +0000466
467Like Valgrind [2], QEMU does user space emulation and dynamic
468translation. Valgrind is mainly a memory debugger while QEMU has no
bellard1eb20522003-06-25 16:21:49 +0000469support for it (QEMU could be used to detect out of bound memory
470accesses as Valgrind, but it has no support to track uninitialised data
bellardd5a0b502003-06-27 12:02:03 +0000471as Valgrind does). The Valgrind dynamic translator generates better code
bellard1eb20522003-06-25 16:21:49 +0000472than QEMU (in particular it does register allocation) but it is closely
bellardd5a0b502003-06-27 12:02:03 +0000473tied to an x86 host and target and has no support for precise exceptions
bellard1eb20522003-06-25 16:21:49 +0000474and system emulation.
bellard386405f2003-03-23 21:28:45 +0000475
bellard1eb20522003-06-25 16:21:49 +0000476EM86 [4] is the closest project to user space QEMU (and QEMU still uses
477some of its code, in particular the ELF file loader). EM86 was limited
478to an alpha host and used a proprietary and slow interpreter (the
479interpreter part of the FX!32 Digital Win32 code translator [5]).
bellard386405f2003-03-23 21:28:45 +0000480
bellardfd429f22003-03-30 20:59:46 +0000481TWIN [6] is a Windows API emulator like Wine. It is less accurate than
482Wine but includes a protected mode x86 interpreter to launch x86 Windows
483executables. Such an approach as greater potential because most of the
484Windows API is executed natively but it is far more difficult to develop
485because all the data structures and function parameters exchanged
486between the API and the x86 code must be converted.
487
bellard1eb20522003-06-25 16:21:49 +0000488User mode Linux [7] was the only solution before QEMU to launch a Linux
489kernel as a process while not needing any host kernel patches. However,
490user mode Linux requires heavy kernel patches while QEMU accepts
491unpatched Linux kernels. It would be interesting to compare the
492performance of the two approaches.
493
494The new Plex86 [8] PC virtualizer is done in the same spirit as the QEMU
495system emulator. It requires a patched Linux kernel to work (you cannot
496launch the same kernel on your PC), but the patches are really small. As
497it is a PC virtualizer (no emulation is done except for some priveledged
498instructions), it has the potential of being faster than QEMU. The
bellardd5a0b502003-06-27 12:02:03 +0000499downside is that a complicated (and potentially unsafe) host kernel
500patch is needed.
bellard1eb20522003-06-25 16:21:49 +0000501
bellard386405f2003-03-23 21:28:45 +0000502@section Portable dynamic translation
503
504QEMU is a dynamic translator. When it first encounters a piece of code,
505it converts it to the host instruction set. Usually dynamic translators
bellard322d0c62003-06-15 23:29:28 +0000506are very complicated and highly CPU dependent. QEMU uses some tricks
bellard386405f2003-03-23 21:28:45 +0000507which make it relatively easily portable and simple while achieving good
508performances.
509
510The basic idea is to split every x86 instruction into fewer simpler
511instructions. Each simple instruction is implemented by a piece of C
512code (see @file{op-i386.c}). Then a compile time tool (@file{dyngen})
513takes the corresponding object file (@file{op-i386.o}) to generate a
514dynamic code generator which concatenates the simple instructions to
515build a function (see @file{op-i386.h:dyngen_code()}).
516
517In essence, the process is similar to [1], but more work is done at
518compile time.
519
520A key idea to get optimal performances is that constant parameters can
521be passed to the simple operations. For that purpose, dummy ELF
522relocations are generated with gcc for each constant parameter. Then,
523the tool (@file{dyngen}) can locate the relocations and generate the
524appriopriate C code to resolve them when building the dynamic code.
525
526That way, QEMU is no more difficult to port than a dynamic linker.
527
528To go even faster, GCC static register variables are used to keep the
529state of the virtual CPU.
530
531@section Register allocation
532
533Since QEMU uses fixed simple instructions, no efficient register
534allocation can be done. However, because RISC CPUs have a lot of
535register, most of the virtual CPU state can be put in registers without
536doing complicated register allocation.
537
538@section Condition code optimisations
539
540Good CPU condition codes emulation (@code{EFLAGS} register on x86) is a
541critical point to get good performances. QEMU uses lazy condition code
542evaluation: instead of computing the condition codes after each x86
bellardfd429f22003-03-30 20:59:46 +0000543instruction, it just stores one operand (called @code{CC_SRC}), the
bellard386405f2003-03-23 21:28:45 +0000544result (called @code{CC_DST}) and the type of operation (called
545@code{CC_OP}).
546
547@code{CC_OP} is almost never explicitely set in the generated code
548because it is known at translation time.
549
550In order to increase performances, a backward pass is performed on the
551generated simple instructions (see
552@code{translate-i386.c:optimize_flags()}). When it can be proved that
553the condition codes are not needed by the next instructions, no
554condition codes are computed at all.
555
bellardfd429f22003-03-30 20:59:46 +0000556@section CPU state optimisations
bellard386405f2003-03-23 21:28:45 +0000557
558The x86 CPU has many internal states which change the way it evaluates
559instructions. In order to achieve a good speed, the translation phase
560considers that some state information of the virtual x86 CPU cannot
561change in it. For example, if the SS, DS and ES segments have a zero
562base, then the translator does not even generate an addition for the
563segment base.
564
565[The FPU stack pointer register is not handled that way yet].
566
567@section Translation cache
568
569A 2MByte cache holds the most recently used translations. For
570simplicity, it is completely flushed when it is full. A translation unit
571contains just a single basic block (a block of x86 instructions
572terminated by a jump or by a virtual CPU state change which the
573translator cannot deduce statically).
574
bellarddf0f11a2003-05-28 00:27:57 +0000575@section Direct block chaining
576
577After each translated basic block is executed, QEMU uses the simulated
578Program Counter (PC) and other cpu state informations (such as the CS
579segment base value) to find the next basic block.
580
581In order to accelerate the most common cases where the new simulated PC
582is known, QEMU can patch a basic block so that it jumps directly to the
583next one.
584
585The most portable code uses an indirect jump. An indirect jump makes it
586easier to make the jump target modification atomic. On some
587architectures (such as PowerPC), the @code{JUMP} opcode is directly
588patched so that the block chaining has no overhead.
589
590@section Self-modifying code and translated code invalidation
591
592Self-modifying code is a special challenge in x86 emulation because no
593instruction cache invalidation is signaled by the application when code
594is modified.
595
596When translated code is generated for a basic block, the corresponding
597host page is write protected if it is not already read-only (with the
598system call @code{mprotect()}). Then, if a write access is done to the
599page, Linux raises a SEGV signal. QEMU then invalidates all the
600translated code in the page and enables write accesses to the page.
601
602Correct translated code invalidation is done efficiently by maintaining
603a linked list of every translated block contained in a given page. Other
604linked lists are also maintained to undo direct block chaining.
605
606Althought the overhead of doing @code{mprotect()} calls is important,
607most MSDOS programs can be emulated at reasonnable speed with QEMU and
608DOSEMU.
609
610Note that QEMU also invalidates pages of translated code when it detects
611that memory mappings are modified with @code{mmap()} or @code{munmap()}.
bellard386405f2003-03-23 21:28:45 +0000612
613@section Exception support
614
615longjmp() is used when an exception such as division by zero is
bellarddf0f11a2003-05-28 00:27:57 +0000616encountered.
bellard386405f2003-03-23 21:28:45 +0000617
bellarddf0f11a2003-05-28 00:27:57 +0000618The host SIGSEGV and SIGBUS signal handlers are used to get invalid
619memory accesses. The exact CPU state can be retrieved because all the
620x86 registers are stored in fixed host registers. The simulated program
621counter is found by retranslating the corresponding basic block and by
622looking where the host program counter was at the exception point.
623
624The virtual CPU cannot retrieve the exact @code{EFLAGS} register because
625in some cases it is not computed because of condition code
626optimisations. It is not a big concern because the emulated code can
627still be restarted in any cases.
bellard386405f2003-03-23 21:28:45 +0000628
629@section Linux system call translation
630
631QEMU includes a generic system call translator for Linux. It means that
632the parameters of the system calls can be converted to fix the
633endianness and 32/64 bit issues. The IOCTLs are converted with a generic
634type description system (see @file{ioctls.h} and @file{thunk.c}).
635
bellarddf0f11a2003-05-28 00:27:57 +0000636QEMU supports host CPUs which have pages bigger than 4KB. It records all
637the mappings the process does and try to emulated the @code{mmap()}
638system calls in cases where the host @code{mmap()} call would fail
639because of bad page alignment.
640
bellard386405f2003-03-23 21:28:45 +0000641@section Linux signals
642
643Normal and real-time signals are queued along with their information
644(@code{siginfo_t}) as it is done in the Linux kernel. Then an interrupt
645request is done to the virtual CPU. When it is interrupted, one queued
646signal is handled by generating a stack frame in the virtual CPU as the
647Linux kernel does. The @code{sigreturn()} system call is emulated to return
648from the virtual signal handler.
649
650Some signals (such as SIGALRM) directly come from the host. Other
651signals are synthetized from the virtual CPU exceptions such as SIGFPE
652when a division by zero is done (see @code{main.c:cpu_loop()}).
653
654The blocked signal mask is still handled by the host Linux kernel so
655that most signal system calls can be redirected directly to the host
656Linux kernel. Only the @code{sigaction()} and @code{sigreturn()} system
657calls need to be fully emulated (see @file{signal.c}).
658
659@section clone() system call and threads
660
661The Linux clone() system call is usually used to create a thread. QEMU
662uses the host clone() system call so that real host threads are created
663for each emulated thread. One virtual CPU instance is created for each
664thread.
665
666The virtual x86 CPU atomic operations are emulated with a global lock so
667that their semantic is preserved.
668
bellarddf0f11a2003-05-28 00:27:57 +0000669Note that currently there are still some locking issues in QEMU. In
670particular, the translated cache flush is not protected yet against
671reentrancy.
672
bellard1eb87252003-04-11 01:12:28 +0000673@section Self-virtualization
674
675QEMU was conceived so that ultimately it can emulate itself. Althought
676it is not very useful, it is an important test to show the power of the
677emulator.
678
679Achieving self-virtualization is not easy because there may be address
bellard6cd9f352003-04-29 20:40:35 +0000680space conflicts. QEMU solves this problem by being an executable ELF
681shared object as the ld-linux.so ELF interpreter. That way, it can be
682relocated at load time.
bellard1eb87252003-04-11 01:12:28 +0000683
bellard1eb20522003-06-25 16:21:49 +0000684@section MMU emulation
685
686For system emulation, QEMU uses the mmap() system call to emulate the
687target CPU MMU. It works as long the emulated OS does not use an area
688reserved by the host OS (such as the area above 0xc0000000 on x86
689Linux).
690
691It is planned to add a slower but more precise MMU emulation
692with a software MMU.
693
bellard386405f2003-03-23 21:28:45 +0000694@section Bibliography
695
696@table @asis
697
698@item [1]
699@url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing
700direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio
701Riccardi.
702
703@item [2]
704@url{http://developer.kde.org/~sewardj/}, Valgrind, an open-source
705memory debugger for x86-GNU/Linux, by Julian Seward.
706
707@item [3]
708@url{http://bochs.sourceforge.net/}, the Bochs IA-32 Emulator Project,
709by Kevin Lawton et al.
710
711@item [4]
712@url{http://www.cs.rose-hulman.edu/~donaldlf/em86/index.html}, the EM86
713x86 emulator on Alpha-Linux.
714
715@item [5]
716@url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/full_papers/chernoff/chernoff.pdf},
717DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton
718Chernoff and Ray Hookway.
719
bellardfd429f22003-03-30 20:59:46 +0000720@item [6]
721@url{http://www.willows.com/}, Windows API library emulation from
722Willows Software.
723
bellard1eb20522003-06-25 16:21:49 +0000724@item [7]
725@url{http://user-mode-linux.sourceforge.net/},
726The User-mode Linux Kernel.
727
728@item [8]
729@url{http://www.plex86.org/},
730The new Plex86 project.
731
bellard386405f2003-03-23 21:28:45 +0000732@end table
733
734@chapter Regression Tests
735
bellard322d0c62003-06-15 23:29:28 +0000736In the directory @file{tests/}, various interesting testing programs
bellard386405f2003-03-23 21:28:45 +0000737are available. There are used for regression testing.
738
bellard322d0c62003-06-15 23:29:28 +0000739@section @file{hello-i386}
bellard386405f2003-03-23 21:28:45 +0000740
741Very simple statically linked x86 program, just to test QEMU during a
742port to a new host CPU.
743
bellard322d0c62003-06-15 23:29:28 +0000744@section @file{hello-arm}
745
746Very simple statically linked ARM program, just to test QEMU during a
747port to a new host CPU.
748
bellard386405f2003-03-23 21:28:45 +0000749@section @file{test-i386}
750
751This program executes most of the 16 bit and 32 bit x86 instructions and
752generates a text output. It can be compared with the output obtained with
753a real CPU or another emulator. The target @code{make test} runs this
754program and a @code{diff} on the generated output.
755
756The Linux system call @code{modify_ldt()} is used to create x86 selectors
757to test some 16 bit addressing and 32 bit with segmentation cases.
758
bellarddf0f11a2003-05-28 00:27:57 +0000759The Linux system call @code{vm86()} is used to test vm86 emulation.
bellard386405f2003-03-23 21:28:45 +0000760
bellarddf0f11a2003-05-28 00:27:57 +0000761Various exceptions are raised to test most of the x86 user space
762exception reporting.
bellard386405f2003-03-23 21:28:45 +0000763
764@section @file{sha1}
765
766It is a simple benchmark. Care must be taken to interpret the results
767because it mostly tests the ability of the virtual CPU to optimize the
768@code{rol} x86 instruction and the condition code computations.
769