~jderose/ubuntu/raring/qemu/vde-again

« back to all changes in this revision

Viewing changes to qemu-tech.texi

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno, Aurelien Jarno
  • Date: 2008-08-25 04:38:35 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20080825043835-8e3tftavy8bujdch
Tags: 0.9.1-6
[ Aurelien Jarno ]
* debian/control: 
  - Update list of supported targets (Closes: bug#488339).
* debian/qemu-make-debian-root:
  - Use mktemp instead of $$ to create temporary directories (Closes: 
    bug#496394).
* debian/links:
  - Add missing links to manpages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
\input texinfo @c -*- texinfo -*-
 
2
@c %**start of header
 
3
@setfilename qemu-tech.info
 
4
@settitle QEMU Internals
 
5
@exampleindent 0
 
6
@paragraphindent 0
 
7
@c %**end of header
2
8
 
3
9
@iftex
4
 
@settitle QEMU Internals
5
10
@titlepage
6
11
@sp 7
7
12
@center @titlefont{QEMU Internals}
9
14
@end titlepage
10
15
@end iftex
11
16
 
 
17
@ifnottex
 
18
@node Top
 
19
@top
 
20
 
 
21
@menu
 
22
* Introduction::
 
23
* QEMU Internals::
 
24
* Regression Tests::
 
25
* Index::
 
26
@end menu
 
27
@end ifnottex
 
28
 
 
29
@contents
 
30
 
 
31
@node Introduction
12
32
@chapter Introduction
13
33
 
 
34
@menu
 
35
* intro_features::        Features
 
36
* intro_x86_emulation::   x86 emulation
 
37
* intro_arm_emulation::   ARM emulation
 
38
* intro_mips_emulation::  MIPS emulation
 
39
* intro_ppc_emulation::   PowerPC emulation
 
40
* intro_sparc_emulation:: SPARC emulation
 
41
@end menu
 
42
 
 
43
@node intro_features
14
44
@section Features
15
45
 
16
46
QEMU is a FAST! processor emulator using a portable dynamic
20
50
 
21
51
@itemize @minus
22
52
 
23
 
@item 
 
53
@item
24
54
Full system emulation. In this mode, QEMU emulates a full system
25
 
(usually a PC), including a processor and various peripherials. It can
 
55
(usually a PC), including a processor and various peripherals. It can
26
56
be used to launch an different Operating System without rebooting the
27
57
PC or to debug system code.
28
58
 
29
 
@item 
 
59
@item
30
60
User mode emulation (Linux host only). In this mode, QEMU can launch
31
61
Linux processes compiled for one CPU on another CPU. It can be used to
32
62
launch the Wine Windows API emulator (@url{http://www.winehq.org}) or
39
69
 
40
70
QEMU generic features:
41
71
 
42
 
@itemize 
 
72
@itemize
43
73
 
44
74
@item User space only or full system emulation.
45
75
 
46
 
@item Using dynamic translation to native code for reasonnable speed.
 
76
@item Using dynamic translation to native code for reasonable speed.
47
77
 
48
78
@item Working on x86 and PowerPC hosts. Being tested on ARM, Sparc32, Alpha and S390.
49
79
 
51
81
 
52
82
@item Precise exceptions support.
53
83
 
54
 
@item The virtual CPU is a library (@code{libqemu}) which can be used 
 
84
@item The virtual CPU is a library (@code{libqemu}) which can be used
55
85
in other projects (look at @file{qemu/tests/qruncom.c} to have an
56
86
example of user mode @code{libqemu} usage).
57
87
 
58
88
@end itemize
59
89
 
60
90
QEMU user mode emulation features:
61
 
@itemize 
 
91
@itemize
62
92
@item Generic Linux system call converter, including most ioctls.
63
93
 
64
94
@item clone() emulation using native CPU clone() to use Linux scheduler for threads.
65
95
 
66
 
@item Accurate signal handling by remapping host signals to target signals. 
67
 
@end itemize
 
96
@item Accurate signal handling by remapping host signals to target signals.
68
97
@end itemize
69
98
 
70
99
QEMU full system emulation features:
71
 
@itemize 
 
100
@itemize
72
101
@item QEMU can either use a full software MMU for maximum portability or use the host system call mmap() to simulate the target MMU.
73
102
@end itemize
74
103
 
 
104
@node intro_x86_emulation
75
105
@section x86 emulation
76
106
 
77
107
QEMU x86 target features:
78
108
 
79
 
@itemize 
 
109
@itemize
80
110
 
81
 
@item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation. 
 
111
@item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation.
82
112
LDT/GDT and IDT are emulated. VM86 mode is also supported to run DOSEMU.
83
113
 
84
114
@item Support of host page sizes bigger than 4KB in user mode emulation.
85
115
 
86
116
@item QEMU can emulate itself on x86.
87
117
 
88
 
@item An extensive Linux x86 CPU test program is included @file{tests/test-i386}. 
 
118
@item An extensive Linux x86 CPU test program is included @file{tests/test-i386}.
89
119
It can be used to test other x86 virtual CPUs.
90
120
 
91
121
@end itemize
92
122
 
93
123
Current QEMU limitations:
94
124
 
95
 
@itemize 
 
125
@itemize
96
126
 
97
127
@item No SSE/MMX support (yet).
98
128
 
100
130
 
101
131
@item IPC syscalls are missing.
102
132
 
103
 
@item The x86 segment limits and access rights are not tested at every 
 
133
@item The x86 segment limits and access rights are not tested at every
104
134
memory access (yet). Hopefully, very few OSes seem to rely on that for
105
135
normal use.
106
136
 
107
 
@item On non x86 host CPUs, @code{double}s are used instead of the non standard 
 
137
@item On non x86 host CPUs, @code{double}s are used instead of the non standard
108
138
10 byte @code{long double}s of x86 for floating point emulation to get
109
139
maximum performances.
110
140
 
111
141
@end itemize
112
142
 
 
143
@node intro_arm_emulation
113
144
@section ARM emulation
114
145
 
115
146
@itemize
122
153
 
123
154
@end itemize
124
155
 
 
156
@node intro_mips_emulation
 
157
@section MIPS emulation
 
158
 
 
159
@itemize
 
160
 
 
161
@item The system emulation allows full MIPS32/MIPS64 Release 2 emulation,
 
162
including privileged instructions, FPU and MMU, in both little and big
 
163
endian modes.
 
164
 
 
165
@item The Linux userland emulation can run many 32 bit MIPS Linux binaries.
 
166
 
 
167
@end itemize
 
168
 
 
169
Current QEMU limitations:
 
170
 
 
171
@itemize
 
172
 
 
173
@item Self-modifying code is not always handled correctly.
 
174
 
 
175
@item 64 bit userland emulation is not implemented.
 
176
 
 
177
@item The system emulation is not complete enough to run real firmware.
 
178
 
 
179
@item The watchpoint debug facility is not implemented.
 
180
 
 
181
@end itemize
 
182
 
 
183
@node intro_ppc_emulation
125
184
@section PowerPC emulation
126
185
 
127
186
@itemize
128
187
 
129
 
@item Full PowerPC 32 bit emulation, including priviledged instructions, 
 
188
@item Full PowerPC 32 bit emulation, including privileged instructions,
130
189
FPU and MMU.
131
190
 
132
191
@item Can run most PowerPC Linux binaries.
133
192
 
134
193
@end itemize
135
194
 
 
195
@node intro_sparc_emulation
136
196
@section SPARC emulation
137
197
 
138
198
@itemize
139
199
 
140
 
@item SPARC V8 user support, except FPU instructions.
141
 
 
142
 
@item Can run some SPARC Linux binaries.
143
 
 
144
 
@end itemize
145
 
 
 
200
@item Full SPARC V8 emulation, including privileged
 
201
instructions, FPU and MMU. SPARC V9 emulation includes most privileged
 
202
and VIS instructions, FPU and I/D MMU. Alignment is fully enforced.
 
203
 
 
204
@item Can run most 32-bit SPARC Linux binaries, SPARC32PLUS Linux binaries and
 
205
some 64-bit SPARC Linux binaries.
 
206
 
 
207
@end itemize
 
208
 
 
209
Current QEMU limitations:
 
210
 
 
211
@itemize
 
212
 
 
213
@item IPC syscalls are missing.
 
214
 
 
215
@item Floating point exception support is buggy.
 
216
 
 
217
@item Atomic instructions are not correctly implemented.
 
218
 
 
219
@item Sparc64 emulators are not usable for anything yet.
 
220
 
 
221
@end itemize
 
222
 
 
223
@node QEMU Internals
146
224
@chapter QEMU Internals
147
225
 
 
226
@menu
 
227
* QEMU compared to other emulators::
 
228
* Portable dynamic translation::
 
229
* Register allocation::
 
230
* Condition code optimisations::
 
231
* CPU state optimisations::
 
232
* Translation cache::
 
233
* Direct block chaining::
 
234
* Self-modifying code and translated code invalidation::
 
235
* Exception support::
 
236
* MMU emulation::
 
237
* Hardware interrupts::
 
238
* User emulation specific details::
 
239
* Bibliography::
 
240
@end menu
 
241
 
 
242
@node QEMU compared to other emulators
148
243
@section QEMU compared to other emulators
149
244
 
150
245
Like bochs [3], QEMU emulates an x86 CPU. But QEMU is much faster than
167
262
 
168
263
TWIN [6] is a Windows API emulator like Wine. It is less accurate than
169
264
Wine but includes a protected mode x86 interpreter to launch x86 Windows
170
 
executables. Such an approach as greater potential because most of the
 
265
executables. Such an approach has greater potential because most of the
171
266
Windows API is executed natively but it is far more difficult to develop
172
267
because all the data structures and function parameters exchanged
173
268
between the API and the x86 code must be converted.
191
286
and potentially unsafe host drivers. Moreover, they are unable to
192
287
provide cycle exact simulation as an emulator can.
193
288
 
 
289
@node Portable dynamic translation
194
290
@section Portable dynamic translation
195
291
 
196
292
QEMU is a dynamic translator. When it first encounters a piece of code,
207
303
instructions to build a function (see @file{op.h:dyngen_code()}).
208
304
 
209
305
In essence, the process is similar to [1], but more work is done at
210
 
compile time. 
 
306
compile time.
211
307
 
212
308
A key idea to get optimal performances is that constant parameters can
213
309
be passed to the simple operations. For that purpose, dummy ELF
220
316
To go even faster, GCC static register variables are used to keep the
221
317
state of the virtual CPU.
222
318
 
 
319
@node Register allocation
223
320
@section Register allocation
224
321
 
225
322
Since QEMU uses fixed simple instructions, no efficient register
227
324
register, most of the virtual CPU state can be put in registers without
228
325
doing complicated register allocation.
229
326
 
 
327
@node Condition code optimisations
230
328
@section Condition code optimisations
231
329
 
232
330
Good CPU condition codes emulation (@code{EFLAGS} register on x86) is a
245
343
the condition codes are not needed by the next instructions, no
246
344
condition codes are computed at all.
247
345
 
 
346
@node CPU state optimisations
248
347
@section CPU state optimisations
249
348
 
250
349
The x86 CPU has many internal states which change the way it evaluates
256
355
 
257
356
[The FPU stack pointer register is not handled that way yet].
258
357
 
 
358
@node Translation cache
259
359
@section Translation cache
260
360
 
261
 
A 2MByte cache holds the most recently used translations. For
 
361
A 16 MByte cache holds the most recently used translations. For
262
362
simplicity, it is completely flushed when it is full. A translation unit
263
363
contains just a single basic block (a block of x86 instructions
264
364
terminated by a jump or by a virtual CPU state change which the
265
365
translator cannot deduce statically).
266
366
 
 
367
@node Direct block chaining
267
368
@section Direct block chaining
268
369
 
269
370
After each translated basic block is executed, QEMU uses the simulated
279
380
architectures (such as x86 or PowerPC), the @code{JUMP} opcode is
280
381
directly patched so that the block chaining has no overhead.
281
382
 
 
383
@node Self-modifying code and translated code invalidation
282
384
@section Self-modifying code and translated code invalidation
283
385
 
284
386
Self-modifying code is a special challenge in x86 emulation because no
293
395
 
294
396
Correct translated code invalidation is done efficiently by maintaining
295
397
a linked list of every translated block contained in a given page. Other
296
 
linked lists are also maintained to undo direct block chaining. 
 
398
linked lists are also maintained to undo direct block chaining.
297
399
 
298
400
Although the overhead of doing @code{mprotect()} calls is important,
299
401
most MSDOS programs can be emulated at reasonnable speed with QEMU and
309
411
really needs to be invalidated. It avoids invalidating the code when
310
412
only data is modified in the page.
311
413
 
 
414
@node Exception support
312
415
@section Exception support
313
416
 
314
417
longjmp() is used when an exception such as division by zero is
315
 
encountered. 
 
418
encountered.
316
419
 
317
420
The host SIGSEGV and SIGBUS signal handlers are used to get invalid
318
421
memory accesses. The exact CPU state can be retrieved because all the
325
428
optimisations. It is not a big concern because the emulated code can
326
429
still be restarted in any cases.
327
430
 
 
431
@node MMU emulation
328
432
@section MMU emulation
329
433
 
330
434
For system emulation, QEMU uses the mmap() system call to emulate the
339
443
 
340
444
In order to avoid flushing the translated code each time the MMU
341
445
mappings change, QEMU uses a physically indexed translation cache. It
342
 
means that each basic block is indexed with its physical address. 
 
446
means that each basic block is indexed with its physical address.
343
447
 
344
448
When MMU mappings change, only the chaining of the basic blocks is
345
449
reset (i.e. a basic block can no longer jump directly to another one).
346
450
 
 
451
@node Hardware interrupts
347
452
@section Hardware interrupts
348
453
 
349
454
In order to be faster, QEMU does not check at every basic block if an
354
459
of the CPU emulator. Then the main loop can test if the interrupt is
355
460
pending and handle it.
356
461
 
 
462
@node User emulation specific details
357
463
@section User emulation specific details
358
464
 
359
465
@subsection Linux system call translation
411
517
shared object as the ld-linux.so ELF interpreter. That way, it can be
412
518
relocated at load time.
413
519
 
 
520
@node Bibliography
414
521
@section Bibliography
415
522
 
416
523
@table @asis
417
524
 
418
 
@item [1] 
 
525
@item [1]
419
526
@url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing
420
527
direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio
421
528
Riccardi.
433
540
x86 emulator on Alpha-Linux.
434
541
 
435
542
@item [5]
436
 
@url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/full_papers/chernoff/chernoff.pdf},
 
543
@url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/@/full_papers/chernoff/chernoff.pdf},
437
544
DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton
438
545
Chernoff and Ray Hookway.
439
546
 
442
549
Willows Software.
443
550
 
444
551
@item [7]
445
 
@url{http://user-mode-linux.sourceforge.net/}, 
 
552
@url{http://user-mode-linux.sourceforge.net/},
446
553
The User-mode Linux Kernel.
447
554
 
448
555
@item [8]
449
 
@url{http://www.plex86.org/}, 
 
556
@url{http://www.plex86.org/},
450
557
The new Plex86 project.
451
558
 
452
559
@item [9]
453
 
@url{http://www.vmware.com/}, 
 
560
@url{http://www.vmware.com/},
454
561
The VMWare PC virtualizer.
455
562
 
456
563
@item [10]
457
 
@url{http://www.microsoft.com/windowsxp/virtualpc/}, 
 
564
@url{http://www.microsoft.com/windowsxp/virtualpc/},
458
565
The VirtualPC PC virtualizer.
459
566
 
460
567
@item [11]
461
 
@url{http://www.twoostwo.org/}, 
 
568
@url{http://www.twoostwo.org/},
462
569
The TwoOStwo PC virtualizer.
463
570
 
464
571
@end table
465
572
 
 
573
@node Regression Tests
466
574
@chapter Regression Tests
467
575
 
468
576
In the directory @file{tests/}, various interesting testing programs
469
 
are available. There are used for regression testing.
470
 
 
 
577
are available. They are used for regression testing.
 
578
 
 
579
@menu
 
580
* test-i386::
 
581
* linux-test::
 
582
* qruncom.c::
 
583
@end menu
 
584
 
 
585
@node test-i386
471
586
@section @file{test-i386}
472
587
 
473
588
This program executes most of the 16 bit and 32 bit x86 instructions and
483
598
Various exceptions are raised to test most of the x86 user space
484
599
exception reporting.
485
600
 
 
601
@node linux-test
486
602
@section @file{linux-test}
487
603
 
488
604
This program tests various Linux system calls. It is used to verify
489
605
that the system call parameters are correctly converted between target
490
606
and host CPUs.
491
607
 
492
 
@section @file{hello-i386}
493
 
 
494
 
Very simple statically linked x86 program, just to test QEMU during a
495
 
port to a new host CPU.
496
 
 
497
 
@section @file{hello-arm}
498
 
 
499
 
Very simple statically linked ARM program, just to test QEMU during a
500
 
port to a new host CPU.
501
 
 
502
 
@section @file{sha1}
503
 
 
504
 
It is a simple benchmark. Care must be taken to interpret the results
505
 
because it mostly tests the ability of the virtual CPU to optimize the
506
 
@code{rol} x86 instruction and the condition code computations.
507
 
 
 
608
@node qruncom.c
 
609
@section @file{qruncom.c}
 
610
 
 
611
Example of usage of @code{libqemu} to emulate a user mode i386 CPU.
 
612
 
 
613
@node Index
 
614
@chapter Index
 
615
@printindex cp
 
616
 
 
617
@bye