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