root / qemu-tech.texi @ 29517134
History | View | Annotate | Download (18 kB)
1 | 1f673135 | bellard | \input texinfo @c -*- texinfo -*- |
---|---|---|---|
2 | 1f673135 | bellard | |
3 | 1f673135 | bellard | @iftex |
4 | 1f673135 | bellard | @settitle QEMU Internals |
5 | 1f673135 | bellard | @titlepage |
6 | 1f673135 | bellard | @sp 7 |
7 | 1f673135 | bellard | @center @titlefont{QEMU Internals} |
8 | 1f673135 | bellard | @sp 3 |
9 | 1f673135 | bellard | @end titlepage |
10 | 1f673135 | bellard | @end iftex |
11 | 1f673135 | bellard | |
12 | 1f673135 | bellard | @chapter Introduction |
13 | 1f673135 | bellard | |
14 | 1f673135 | bellard | @section Features |
15 | 1f673135 | bellard | |
16 | 1f673135 | bellard | QEMU is a FAST! processor emulator using a portable dynamic |
17 | 1f673135 | bellard | translator. |
18 | 1f673135 | bellard | |
19 | 1f673135 | bellard | QEMU has two operating modes: |
20 | 1f673135 | bellard | |
21 | 1f673135 | bellard | @itemize @minus |
22 | 1f673135 | bellard | |
23 | 1f673135 | bellard | @item |
24 | 1f673135 | bellard | Full system emulation. In this mode, QEMU emulates a full system |
25 | b671f9ed | bellard | (usually a PC), including a processor and various peripherals. It can |
26 | 1f673135 | bellard | be used to launch an different Operating System without rebooting the |
27 | 1f673135 | bellard | PC or to debug system code. |
28 | 1f673135 | bellard | |
29 | 1f673135 | bellard | @item |
30 | 1f673135 | bellard | User mode emulation (Linux host only). In this mode, QEMU can launch |
31 | 1f673135 | bellard | Linux processes compiled for one CPU on another CPU. It can be used to |
32 | 1f673135 | bellard | launch the Wine Windows API emulator (@url{http://www.winehq.org}) or |
33 | 1f673135 | bellard | to ease cross-compilation and cross-debugging. |
34 | 1f673135 | bellard | |
35 | 1f673135 | bellard | @end itemize |
36 | 1f673135 | bellard | |
37 | 1f673135 | bellard | As QEMU requires no host kernel driver to run, it is very safe and |
38 | 1f673135 | bellard | easy to use. |
39 | 1f673135 | bellard | |
40 | 1f673135 | bellard | QEMU generic features: |
41 | 1f673135 | bellard | |
42 | 1f673135 | bellard | @itemize |
43 | 1f673135 | bellard | |
44 | 1f673135 | bellard | @item User space only or full system emulation. |
45 | 1f673135 | bellard | |
46 | 1f673135 | bellard | @item Using dynamic translation to native code for reasonnable speed. |
47 | 1f673135 | bellard | |
48 | 1f673135 | bellard | @item Working on x86 and PowerPC hosts. Being tested on ARM, Sparc32, Alpha and S390. |
49 | 1f673135 | bellard | |
50 | 1f673135 | bellard | @item Self-modifying code support. |
51 | 1f673135 | bellard | |
52 | 1f673135 | bellard | @item Precise exceptions support. |
53 | 1f673135 | bellard | |
54 | 1f673135 | bellard | @item The virtual CPU is a library (@code{libqemu}) which can be used |
55 | ad6a4837 | bellard | in other projects (look at @file{qemu/tests/qruncom.c} to have an |
56 | ad6a4837 | bellard | example of user mode @code{libqemu} usage). |
57 | 1f673135 | bellard | |
58 | 1f673135 | bellard | @end itemize |
59 | 1f673135 | bellard | |
60 | 1f673135 | bellard | QEMU user mode emulation features: |
61 | 1f673135 | bellard | @itemize |
62 | 1f673135 | bellard | @item Generic Linux system call converter, including most ioctls. |
63 | 1f673135 | bellard | |
64 | 1f673135 | bellard | @item clone() emulation using native CPU clone() to use Linux scheduler for threads. |
65 | 1f673135 | bellard | |
66 | 1f673135 | bellard | @item Accurate signal handling by remapping host signals to target signals. |
67 | 1f673135 | bellard | @end itemize |
68 | 1f673135 | bellard | @end itemize |
69 | 1f673135 | bellard | |
70 | 1f673135 | bellard | QEMU full system emulation features: |
71 | 1f673135 | bellard | @itemize |
72 | 1f673135 | bellard | @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 | 1f673135 | bellard | @end itemize |
74 | 1f673135 | bellard | |
75 | 1f673135 | bellard | @section x86 emulation |
76 | 1f673135 | bellard | |
77 | 1f673135 | bellard | QEMU x86 target features: |
78 | 1f673135 | bellard | |
79 | 1f673135 | bellard | @itemize |
80 | 1f673135 | bellard | |
81 | 1f673135 | bellard | @item The virtual x86 CPU supports 16 bit and 32 bit addressing with segmentation. |
82 | 1f673135 | bellard | LDT/GDT and IDT are emulated. VM86 mode is also supported to run DOSEMU. |
83 | 1f673135 | bellard | |
84 | 1f673135 | bellard | @item Support of host page sizes bigger than 4KB in user mode emulation. |
85 | 1f673135 | bellard | |
86 | 1f673135 | bellard | @item QEMU can emulate itself on x86. |
87 | 1f673135 | bellard | |
88 | 1f673135 | bellard | @item An extensive Linux x86 CPU test program is included @file{tests/test-i386}. |
89 | 1f673135 | bellard | It can be used to test other x86 virtual CPUs. |
90 | 1f673135 | bellard | |
91 | 1f673135 | bellard | @end itemize |
92 | 1f673135 | bellard | |
93 | 1f673135 | bellard | Current QEMU limitations: |
94 | 1f673135 | bellard | |
95 | 1f673135 | bellard | @itemize |
96 | 1f673135 | bellard | |
97 | 1f673135 | bellard | @item No SSE/MMX support (yet). |
98 | 1f673135 | bellard | |
99 | 1f673135 | bellard | @item No x86-64 support. |
100 | 1f673135 | bellard | |
101 | 1f673135 | bellard | @item IPC syscalls are missing. |
102 | 1f673135 | bellard | |
103 | 1f673135 | bellard | @item The x86 segment limits and access rights are not tested at every |
104 | 1f673135 | bellard | memory access (yet). Hopefully, very few OSes seem to rely on that for |
105 | 1f673135 | bellard | normal use. |
106 | 1f673135 | bellard | |
107 | 1f673135 | bellard | @item On non x86 host CPUs, @code{double}s are used instead of the non standard |
108 | 1f673135 | bellard | 10 byte @code{long double}s of x86 for floating point emulation to get |
109 | 1f673135 | bellard | maximum performances. |
110 | 1f673135 | bellard | |
111 | 1f673135 | bellard | @end itemize |
112 | 1f673135 | bellard | |
113 | 1f673135 | bellard | @section ARM emulation |
114 | 1f673135 | bellard | |
115 | 1f673135 | bellard | @itemize |
116 | 1f673135 | bellard | |
117 | 1f673135 | bellard | @item Full ARM 7 user emulation. |
118 | 1f673135 | bellard | |
119 | 1f673135 | bellard | @item NWFPE FPU support included in user Linux emulation. |
120 | 1f673135 | bellard | |
121 | 1f673135 | bellard | @item Can run most ARM Linux binaries. |
122 | 1f673135 | bellard | |
123 | 1f673135 | bellard | @end itemize |
124 | 1f673135 | bellard | |
125 | 1f673135 | bellard | @section PowerPC emulation |
126 | 1f673135 | bellard | |
127 | 1f673135 | bellard | @itemize |
128 | 1f673135 | bellard | |
129 | e80cfcfc | bellard | @item Full PowerPC 32 bit emulation, including privileged instructions, |
130 | 1f673135 | bellard | FPU and MMU. |
131 | 1f673135 | bellard | |
132 | 1f673135 | bellard | @item Can run most PowerPC Linux binaries. |
133 | 1f673135 | bellard | |
134 | 1f673135 | bellard | @end itemize |
135 | 1f673135 | bellard | |
136 | 1f673135 | bellard | @section SPARC emulation |
137 | 1f673135 | bellard | |
138 | 1f673135 | bellard | @itemize |
139 | 1f673135 | bellard | |
140 | e80cfcfc | bellard | @item Somewhat complete SPARC V8 emulation, including privileged |
141 | 3475187d | bellard | instructions, FPU and MMU. SPARC V9 emulation includes most privileged |
142 | 3475187d | bellard | instructions, FPU and I/D MMU, but misses VIS instructions. |
143 | 1f673135 | bellard | |
144 | 3475187d | bellard | @item Can run some 32-bit SPARC Linux binaries. |
145 | 3475187d | bellard | |
146 | 3475187d | bellard | @end itemize |
147 | 3475187d | bellard | |
148 | 3475187d | bellard | Current QEMU limitations: |
149 | 3475187d | bellard | |
150 | 3475187d | bellard | @itemize |
151 | 3475187d | bellard | |
152 | 3475187d | bellard | @item Tagged add/subtract instructions are not supported, but they are |
153 | 3475187d | bellard | probably not used. |
154 | 3475187d | bellard | |
155 | 3475187d | bellard | @item IPC syscalls are missing. |
156 | 3475187d | bellard | |
157 | 3475187d | bellard | @item 128-bit floating point operations are not supported, though none of the |
158 | 3475187d | bellard | real CPUs implement them either. FCMPE[SD] are not correctly |
159 | 3475187d | bellard | implemented. Floating point exception support is untested. |
160 | 3475187d | bellard | |
161 | 3475187d | bellard | @item Alignment is not enforced at all. |
162 | 3475187d | bellard | |
163 | 3475187d | bellard | @item Atomic instructions are not correctly implemented. |
164 | 3475187d | bellard | |
165 | 3475187d | bellard | @item Sparc64 emulators are not usable for anything yet. |
166 | 1f673135 | bellard | |
167 | 1f673135 | bellard | @end itemize |
168 | 1f673135 | bellard | |
169 | 1f673135 | bellard | @chapter QEMU Internals |
170 | 1f673135 | bellard | |
171 | 1f673135 | bellard | @section QEMU compared to other emulators |
172 | 1f673135 | bellard | |
173 | 1f673135 | bellard | Like bochs [3], QEMU emulates an x86 CPU. But QEMU is much faster than |
174 | 1f673135 | bellard | bochs as it uses dynamic compilation. Bochs is closely tied to x86 PC |
175 | 1f673135 | bellard | emulation while QEMU can emulate several processors. |
176 | 1f673135 | bellard | |
177 | 1f673135 | bellard | Like Valgrind [2], QEMU does user space emulation and dynamic |
178 | 1f673135 | bellard | translation. Valgrind is mainly a memory debugger while QEMU has no |
179 | 1f673135 | bellard | support for it (QEMU could be used to detect out of bound memory |
180 | 1f673135 | bellard | accesses as Valgrind, but it has no support to track uninitialised data |
181 | 1f673135 | bellard | as Valgrind does). The Valgrind dynamic translator generates better code |
182 | 1f673135 | bellard | than QEMU (in particular it does register allocation) but it is closely |
183 | 1f673135 | bellard | tied to an x86 host and target and has no support for precise exceptions |
184 | 1f673135 | bellard | and system emulation. |
185 | 1f673135 | bellard | |
186 | 1f673135 | bellard | EM86 [4] is the closest project to user space QEMU (and QEMU still uses |
187 | 1f673135 | bellard | some of its code, in particular the ELF file loader). EM86 was limited |
188 | 1f673135 | bellard | to an alpha host and used a proprietary and slow interpreter (the |
189 | 1f673135 | bellard | interpreter part of the FX!32 Digital Win32 code translator [5]). |
190 | 1f673135 | bellard | |
191 | 1f673135 | bellard | TWIN [6] is a Windows API emulator like Wine. It is less accurate than |
192 | 1f673135 | bellard | Wine but includes a protected mode x86 interpreter to launch x86 Windows |
193 | 36d54d15 | bellard | executables. Such an approach has greater potential because most of the |
194 | 1f673135 | bellard | Windows API is executed natively but it is far more difficult to develop |
195 | 1f673135 | bellard | because all the data structures and function parameters exchanged |
196 | 1f673135 | bellard | between the API and the x86 code must be converted. |
197 | 1f673135 | bellard | |
198 | 1f673135 | bellard | User mode Linux [7] was the only solution before QEMU to launch a |
199 | 1f673135 | bellard | Linux kernel as a process while not needing any host kernel |
200 | 1f673135 | bellard | patches. However, user mode Linux requires heavy kernel patches while |
201 | 1f673135 | bellard | QEMU accepts unpatched Linux kernels. The price to pay is that QEMU is |
202 | 1f673135 | bellard | slower. |
203 | 1f673135 | bellard | |
204 | 1f673135 | bellard | The new Plex86 [8] PC virtualizer is done in the same spirit as the |
205 | 1f673135 | bellard | qemu-fast system emulator. It requires a patched Linux kernel to work |
206 | 1f673135 | bellard | (you cannot launch the same kernel on your PC), but the patches are |
207 | 1f673135 | bellard | really small. As it is a PC virtualizer (no emulation is done except |
208 | 1f673135 | bellard | for some priveledged instructions), it has the potential of being |
209 | 1f673135 | bellard | faster than QEMU. The downside is that a complicated (and potentially |
210 | 1f673135 | bellard | unsafe) host kernel patch is needed. |
211 | 1f673135 | bellard | |
212 | 1f673135 | bellard | The commercial PC Virtualizers (VMWare [9], VirtualPC [10], TwoOStwo |
213 | 1f673135 | bellard | [11]) are faster than QEMU, but they all need specific, proprietary |
214 | 1f673135 | bellard | and potentially unsafe host drivers. Moreover, they are unable to |
215 | 1f673135 | bellard | provide cycle exact simulation as an emulator can. |
216 | 1f673135 | bellard | |
217 | 1f673135 | bellard | @section Portable dynamic translation |
218 | 1f673135 | bellard | |
219 | 1f673135 | bellard | QEMU is a dynamic translator. When it first encounters a piece of code, |
220 | 1f673135 | bellard | it converts it to the host instruction set. Usually dynamic translators |
221 | 1f673135 | bellard | are very complicated and highly CPU dependent. QEMU uses some tricks |
222 | 1f673135 | bellard | which make it relatively easily portable and simple while achieving good |
223 | 1f673135 | bellard | performances. |
224 | 1f673135 | bellard | |
225 | 1f673135 | bellard | The basic idea is to split every x86 instruction into fewer simpler |
226 | 1f673135 | bellard | instructions. Each simple instruction is implemented by a piece of C |
227 | 1f673135 | bellard | code (see @file{target-i386/op.c}). Then a compile time tool |
228 | 1f673135 | bellard | (@file{dyngen}) takes the corresponding object file (@file{op.o}) |
229 | 1f673135 | bellard | to generate a dynamic code generator which concatenates the simple |
230 | 1f673135 | bellard | instructions to build a function (see @file{op.h:dyngen_code()}). |
231 | 1f673135 | bellard | |
232 | 1f673135 | bellard | In essence, the process is similar to [1], but more work is done at |
233 | 1f673135 | bellard | compile time. |
234 | 1f673135 | bellard | |
235 | 1f673135 | bellard | A key idea to get optimal performances is that constant parameters can |
236 | 1f673135 | bellard | be passed to the simple operations. For that purpose, dummy ELF |
237 | 1f673135 | bellard | relocations are generated with gcc for each constant parameter. Then, |
238 | 1f673135 | bellard | the tool (@file{dyngen}) can locate the relocations and generate the |
239 | 1f673135 | bellard | appriopriate C code to resolve them when building the dynamic code. |
240 | 1f673135 | bellard | |
241 | 1f673135 | bellard | That way, QEMU is no more difficult to port than a dynamic linker. |
242 | 1f673135 | bellard | |
243 | 1f673135 | bellard | To go even faster, GCC static register variables are used to keep the |
244 | 1f673135 | bellard | state of the virtual CPU. |
245 | 1f673135 | bellard | |
246 | 1f673135 | bellard | @section Register allocation |
247 | 1f673135 | bellard | |
248 | 1f673135 | bellard | Since QEMU uses fixed simple instructions, no efficient register |
249 | 1f673135 | bellard | allocation can be done. However, because RISC CPUs have a lot of |
250 | 1f673135 | bellard | register, most of the virtual CPU state can be put in registers without |
251 | 1f673135 | bellard | doing complicated register allocation. |
252 | 1f673135 | bellard | |
253 | 1f673135 | bellard | @section Condition code optimisations |
254 | 1f673135 | bellard | |
255 | 1f673135 | bellard | Good CPU condition codes emulation (@code{EFLAGS} register on x86) is a |
256 | 1f673135 | bellard | critical point to get good performances. QEMU uses lazy condition code |
257 | 1f673135 | bellard | evaluation: instead of computing the condition codes after each x86 |
258 | 1f673135 | bellard | instruction, it just stores one operand (called @code{CC_SRC}), the |
259 | 1f673135 | bellard | result (called @code{CC_DST}) and the type of operation (called |
260 | 1f673135 | bellard | @code{CC_OP}). |
261 | 1f673135 | bellard | |
262 | 1f673135 | bellard | @code{CC_OP} is almost never explicitely set in the generated code |
263 | 1f673135 | bellard | because it is known at translation time. |
264 | 1f673135 | bellard | |
265 | 1f673135 | bellard | In order to increase performances, a backward pass is performed on the |
266 | 1f673135 | bellard | generated simple instructions (see |
267 | 1f673135 | bellard | @code{target-i386/translate.c:optimize_flags()}). When it can be proved that |
268 | 1f673135 | bellard | the condition codes are not needed by the next instructions, no |
269 | 1f673135 | bellard | condition codes are computed at all. |
270 | 1f673135 | bellard | |
271 | 1f673135 | bellard | @section CPU state optimisations |
272 | 1f673135 | bellard | |
273 | 1f673135 | bellard | The x86 CPU has many internal states which change the way it evaluates |
274 | 1f673135 | bellard | instructions. In order to achieve a good speed, the translation phase |
275 | 1f673135 | bellard | considers that some state information of the virtual x86 CPU cannot |
276 | 1f673135 | bellard | change in it. For example, if the SS, DS and ES segments have a zero |
277 | 1f673135 | bellard | base, then the translator does not even generate an addition for the |
278 | 1f673135 | bellard | segment base. |
279 | 1f673135 | bellard | |
280 | 1f673135 | bellard | [The FPU stack pointer register is not handled that way yet]. |
281 | 1f673135 | bellard | |
282 | 1f673135 | bellard | @section Translation cache |
283 | 1f673135 | bellard | |
284 | 15a34c63 | bellard | A 16 MByte cache holds the most recently used translations. For |
285 | 1f673135 | bellard | simplicity, it is completely flushed when it is full. A translation unit |
286 | 1f673135 | bellard | contains just a single basic block (a block of x86 instructions |
287 | 1f673135 | bellard | terminated by a jump or by a virtual CPU state change which the |
288 | 1f673135 | bellard | translator cannot deduce statically). |
289 | 1f673135 | bellard | |
290 | 1f673135 | bellard | @section Direct block chaining |
291 | 1f673135 | bellard | |
292 | 1f673135 | bellard | After each translated basic block is executed, QEMU uses the simulated |
293 | 1f673135 | bellard | Program Counter (PC) and other cpu state informations (such as the CS |
294 | 1f673135 | bellard | segment base value) to find the next basic block. |
295 | 1f673135 | bellard | |
296 | 1f673135 | bellard | In order to accelerate the most common cases where the new simulated PC |
297 | 1f673135 | bellard | is known, QEMU can patch a basic block so that it jumps directly to the |
298 | 1f673135 | bellard | next one. |
299 | 1f673135 | bellard | |
300 | 1f673135 | bellard | The most portable code uses an indirect jump. An indirect jump makes |
301 | 1f673135 | bellard | it easier to make the jump target modification atomic. On some host |
302 | 1f673135 | bellard | architectures (such as x86 or PowerPC), the @code{JUMP} opcode is |
303 | 1f673135 | bellard | directly patched so that the block chaining has no overhead. |
304 | 1f673135 | bellard | |
305 | 1f673135 | bellard | @section Self-modifying code and translated code invalidation |
306 | 1f673135 | bellard | |
307 | 1f673135 | bellard | Self-modifying code is a special challenge in x86 emulation because no |
308 | 1f673135 | bellard | instruction cache invalidation is signaled by the application when code |
309 | 1f673135 | bellard | is modified. |
310 | 1f673135 | bellard | |
311 | 1f673135 | bellard | When translated code is generated for a basic block, the corresponding |
312 | 1f673135 | bellard | host page is write protected if it is not already read-only (with the |
313 | 1f673135 | bellard | system call @code{mprotect()}). Then, if a write access is done to the |
314 | 1f673135 | bellard | page, Linux raises a SEGV signal. QEMU then invalidates all the |
315 | 1f673135 | bellard | translated code in the page and enables write accesses to the page. |
316 | 1f673135 | bellard | |
317 | 1f673135 | bellard | Correct translated code invalidation is done efficiently by maintaining |
318 | 1f673135 | bellard | a linked list of every translated block contained in a given page. Other |
319 | 1f673135 | bellard | linked lists are also maintained to undo direct block chaining. |
320 | 1f673135 | bellard | |
321 | 1f673135 | bellard | Although the overhead of doing @code{mprotect()} calls is important, |
322 | 1f673135 | bellard | most MSDOS programs can be emulated at reasonnable speed with QEMU and |
323 | 1f673135 | bellard | DOSEMU. |
324 | 1f673135 | bellard | |
325 | 1f673135 | bellard | Note that QEMU also invalidates pages of translated code when it detects |
326 | 1f673135 | bellard | that memory mappings are modified with @code{mmap()} or @code{munmap()}. |
327 | 1f673135 | bellard | |
328 | 1f673135 | bellard | When using a software MMU, the code invalidation is more efficient: if |
329 | 1f673135 | bellard | a given code page is invalidated too often because of write accesses, |
330 | 1f673135 | bellard | then a bitmap representing all the code inside the page is |
331 | 1f673135 | bellard | built. Every store into that page checks the bitmap to see if the code |
332 | 1f673135 | bellard | really needs to be invalidated. It avoids invalidating the code when |
333 | 1f673135 | bellard | only data is modified in the page. |
334 | 1f673135 | bellard | |
335 | 1f673135 | bellard | @section Exception support |
336 | 1f673135 | bellard | |
337 | 1f673135 | bellard | longjmp() is used when an exception such as division by zero is |
338 | 1f673135 | bellard | encountered. |
339 | 1f673135 | bellard | |
340 | 1f673135 | bellard | The host SIGSEGV and SIGBUS signal handlers are used to get invalid |
341 | 1f673135 | bellard | memory accesses. The exact CPU state can be retrieved because all the |
342 | 1f673135 | bellard | x86 registers are stored in fixed host registers. The simulated program |
343 | 1f673135 | bellard | counter is found by retranslating the corresponding basic block and by |
344 | 1f673135 | bellard | looking where the host program counter was at the exception point. |
345 | 1f673135 | bellard | |
346 | 1f673135 | bellard | The virtual CPU cannot retrieve the exact @code{EFLAGS} register because |
347 | 1f673135 | bellard | in some cases it is not computed because of condition code |
348 | 1f673135 | bellard | optimisations. It is not a big concern because the emulated code can |
349 | 1f673135 | bellard | still be restarted in any cases. |
350 | 1f673135 | bellard | |
351 | 1f673135 | bellard | @section MMU emulation |
352 | 1f673135 | bellard | |
353 | 1f673135 | bellard | For system emulation, QEMU uses the mmap() system call to emulate the |
354 | 1f673135 | bellard | target CPU MMU. It works as long the emulated OS does not use an area |
355 | 1f673135 | bellard | reserved by the host OS (such as the area above 0xc0000000 on x86 |
356 | 1f673135 | bellard | Linux). |
357 | 1f673135 | bellard | |
358 | 1f673135 | bellard | In order to be able to launch any OS, QEMU also supports a soft |
359 | 1f673135 | bellard | MMU. In that mode, the MMU virtual to physical address translation is |
360 | 1f673135 | bellard | done at every memory access. QEMU uses an address translation cache to |
361 | 1f673135 | bellard | speed up the translation. |
362 | 1f673135 | bellard | |
363 | 1f673135 | bellard | In order to avoid flushing the translated code each time the MMU |
364 | 1f673135 | bellard | mappings change, QEMU uses a physically indexed translation cache. It |
365 | 1f673135 | bellard | means that each basic block is indexed with its physical address. |
366 | 1f673135 | bellard | |
367 | 1f673135 | bellard | When MMU mappings change, only the chaining of the basic blocks is |
368 | 1f673135 | bellard | reset (i.e. a basic block can no longer jump directly to another one). |
369 | 1f673135 | bellard | |
370 | 1f673135 | bellard | @section Hardware interrupts |
371 | 1f673135 | bellard | |
372 | 1f673135 | bellard | In order to be faster, QEMU does not check at every basic block if an |
373 | 1f673135 | bellard | hardware interrupt is pending. Instead, the user must asynchrously |
374 | 1f673135 | bellard | call a specific function to tell that an interrupt is pending. This |
375 | 1f673135 | bellard | function resets the chaining of the currently executing basic |
376 | 1f673135 | bellard | block. It ensures that the execution will return soon in the main loop |
377 | 1f673135 | bellard | of the CPU emulator. Then the main loop can test if the interrupt is |
378 | 1f673135 | bellard | pending and handle it. |
379 | 1f673135 | bellard | |
380 | 1f673135 | bellard | @section User emulation specific details |
381 | 1f673135 | bellard | |
382 | 1f673135 | bellard | @subsection Linux system call translation |
383 | 1f673135 | bellard | |
384 | 1f673135 | bellard | QEMU includes a generic system call translator for Linux. It means that |
385 | 1f673135 | bellard | the parameters of the system calls can be converted to fix the |
386 | 1f673135 | bellard | endianness and 32/64 bit issues. The IOCTLs are converted with a generic |
387 | 1f673135 | bellard | type description system (see @file{ioctls.h} and @file{thunk.c}). |
388 | 1f673135 | bellard | |
389 | 1f673135 | bellard | QEMU supports host CPUs which have pages bigger than 4KB. It records all |
390 | 1f673135 | bellard | the mappings the process does and try to emulated the @code{mmap()} |
391 | 1f673135 | bellard | system calls in cases where the host @code{mmap()} call would fail |
392 | 1f673135 | bellard | because of bad page alignment. |
393 | 1f673135 | bellard | |
394 | 1f673135 | bellard | @subsection Linux signals |
395 | 1f673135 | bellard | |
396 | 1f673135 | bellard | Normal and real-time signals are queued along with their information |
397 | 1f673135 | bellard | (@code{siginfo_t}) as it is done in the Linux kernel. Then an interrupt |
398 | 1f673135 | bellard | request is done to the virtual CPU. When it is interrupted, one queued |
399 | 1f673135 | bellard | signal is handled by generating a stack frame in the virtual CPU as the |
400 | 1f673135 | bellard | Linux kernel does. The @code{sigreturn()} system call is emulated to return |
401 | 1f673135 | bellard | from the virtual signal handler. |
402 | 1f673135 | bellard | |
403 | 1f673135 | bellard | Some signals (such as SIGALRM) directly come from the host. Other |
404 | 1f673135 | bellard | signals are synthetized from the virtual CPU exceptions such as SIGFPE |
405 | 1f673135 | bellard | when a division by zero is done (see @code{main.c:cpu_loop()}). |
406 | 1f673135 | bellard | |
407 | 1f673135 | bellard | The blocked signal mask is still handled by the host Linux kernel so |
408 | 1f673135 | bellard | that most signal system calls can be redirected directly to the host |
409 | 1f673135 | bellard | Linux kernel. Only the @code{sigaction()} and @code{sigreturn()} system |
410 | 1f673135 | bellard | calls need to be fully emulated (see @file{signal.c}). |
411 | 1f673135 | bellard | |
412 | 1f673135 | bellard | @subsection clone() system call and threads |
413 | 1f673135 | bellard | |
414 | 1f673135 | bellard | The Linux clone() system call is usually used to create a thread. QEMU |
415 | 1f673135 | bellard | uses the host clone() system call so that real host threads are created |
416 | 1f673135 | bellard | for each emulated thread. One virtual CPU instance is created for each |
417 | 1f673135 | bellard | thread. |
418 | 1f673135 | bellard | |
419 | 1f673135 | bellard | The virtual x86 CPU atomic operations are emulated with a global lock so |
420 | 1f673135 | bellard | that their semantic is preserved. |
421 | 1f673135 | bellard | |
422 | 1f673135 | bellard | Note that currently there are still some locking issues in QEMU. In |
423 | 1f673135 | bellard | particular, the translated cache flush is not protected yet against |
424 | 1f673135 | bellard | reentrancy. |
425 | 1f673135 | bellard | |
426 | 1f673135 | bellard | @subsection Self-virtualization |
427 | 1f673135 | bellard | |
428 | 1f673135 | bellard | QEMU was conceived so that ultimately it can emulate itself. Although |
429 | 1f673135 | bellard | it is not very useful, it is an important test to show the power of the |
430 | 1f673135 | bellard | emulator. |
431 | 1f673135 | bellard | |
432 | 1f673135 | bellard | Achieving self-virtualization is not easy because there may be address |
433 | 1f673135 | bellard | space conflicts. QEMU solves this problem by being an executable ELF |
434 | 1f673135 | bellard | shared object as the ld-linux.so ELF interpreter. That way, it can be |
435 | 1f673135 | bellard | relocated at load time. |
436 | 1f673135 | bellard | |
437 | 1f673135 | bellard | @section Bibliography |
438 | 1f673135 | bellard | |
439 | 1f673135 | bellard | @table @asis |
440 | 1f673135 | bellard | |
441 | 1f673135 | bellard | @item [1] |
442 | 1f673135 | bellard | @url{http://citeseer.nj.nec.com/piumarta98optimizing.html}, Optimizing |
443 | 1f673135 | bellard | direct threaded code by selective inlining (1998) by Ian Piumarta, Fabio |
444 | 1f673135 | bellard | Riccardi. |
445 | 1f673135 | bellard | |
446 | 1f673135 | bellard | @item [2] |
447 | 1f673135 | bellard | @url{http://developer.kde.org/~sewardj/}, Valgrind, an open-source |
448 | 1f673135 | bellard | memory debugger for x86-GNU/Linux, by Julian Seward. |
449 | 1f673135 | bellard | |
450 | 1f673135 | bellard | @item [3] |
451 | 1f673135 | bellard | @url{http://bochs.sourceforge.net/}, the Bochs IA-32 Emulator Project, |
452 | 1f673135 | bellard | by Kevin Lawton et al. |
453 | 1f673135 | bellard | |
454 | 1f673135 | bellard | @item [4] |
455 | 1f673135 | bellard | @url{http://www.cs.rose-hulman.edu/~donaldlf/em86/index.html}, the EM86 |
456 | 1f673135 | bellard | x86 emulator on Alpha-Linux. |
457 | 1f673135 | bellard | |
458 | 1f673135 | bellard | @item [5] |
459 | 1f673135 | bellard | @url{http://www.usenix.org/publications/library/proceedings/usenix-nt97/full_papers/chernoff/chernoff.pdf}, |
460 | 1f673135 | bellard | DIGITAL FX!32: Running 32-Bit x86 Applications on Alpha NT, by Anton |
461 | 1f673135 | bellard | Chernoff and Ray Hookway. |
462 | 1f673135 | bellard | |
463 | 1f673135 | bellard | @item [6] |
464 | 1f673135 | bellard | @url{http://www.willows.com/}, Windows API library emulation from |
465 | 1f673135 | bellard | Willows Software. |
466 | 1f673135 | bellard | |
467 | 1f673135 | bellard | @item [7] |
468 | 1f673135 | bellard | @url{http://user-mode-linux.sourceforge.net/}, |
469 | 1f673135 | bellard | The User-mode Linux Kernel. |
470 | 1f673135 | bellard | |
471 | 1f673135 | bellard | @item [8] |
472 | 1f673135 | bellard | @url{http://www.plex86.org/}, |
473 | 1f673135 | bellard | The new Plex86 project. |
474 | 1f673135 | bellard | |
475 | 1f673135 | bellard | @item [9] |
476 | 1f673135 | bellard | @url{http://www.vmware.com/}, |
477 | 1f673135 | bellard | The VMWare PC virtualizer. |
478 | 1f673135 | bellard | |
479 | 1f673135 | bellard | @item [10] |
480 | 1f673135 | bellard | @url{http://www.microsoft.com/windowsxp/virtualpc/}, |
481 | 1f673135 | bellard | The VirtualPC PC virtualizer. |
482 | 1f673135 | bellard | |
483 | 1f673135 | bellard | @item [11] |
484 | 1f673135 | bellard | @url{http://www.twoostwo.org/}, |
485 | 1f673135 | bellard | The TwoOStwo PC virtualizer. |
486 | 1f673135 | bellard | |
487 | 1f673135 | bellard | @end table |
488 | 1f673135 | bellard | |
489 | 1f673135 | bellard | @chapter Regression Tests |
490 | 1f673135 | bellard | |
491 | 1f673135 | bellard | In the directory @file{tests/}, various interesting testing programs |
492 | 1f673135 | bellard | are available. There are used for regression testing. |
493 | 1f673135 | bellard | |
494 | 1f673135 | bellard | @section @file{test-i386} |
495 | 1f673135 | bellard | |
496 | 1f673135 | bellard | This program executes most of the 16 bit and 32 bit x86 instructions and |
497 | 1f673135 | bellard | generates a text output. It can be compared with the output obtained with |
498 | 1f673135 | bellard | a real CPU or another emulator. The target @code{make test} runs this |
499 | 1f673135 | bellard | program and a @code{diff} on the generated output. |
500 | 1f673135 | bellard | |
501 | 1f673135 | bellard | The Linux system call @code{modify_ldt()} is used to create x86 selectors |
502 | 1f673135 | bellard | to test some 16 bit addressing and 32 bit with segmentation cases. |
503 | 1f673135 | bellard | |
504 | 1f673135 | bellard | The Linux system call @code{vm86()} is used to test vm86 emulation. |
505 | 1f673135 | bellard | |
506 | 1f673135 | bellard | Various exceptions are raised to test most of the x86 user space |
507 | 1f673135 | bellard | exception reporting. |
508 | 1f673135 | bellard | |
509 | 1f673135 | bellard | @section @file{linux-test} |
510 | 1f673135 | bellard | |
511 | 1f673135 | bellard | This program tests various Linux system calls. It is used to verify |
512 | 1f673135 | bellard | that the system call parameters are correctly converted between target |
513 | 1f673135 | bellard | and host CPUs. |
514 | 1f673135 | bellard | |
515 | 15a34c63 | bellard | @section @file{qruncom.c} |
516 | 1f673135 | bellard | |
517 | 15a34c63 | bellard | Example of usage of @code{libqemu} to emulate a user mode i386 CPU. |