Initial import of kqemu-1.4.0pre1
[kqemu.git] / kqemu-tech.texi
blob34b15e261ceff82d4f9c5d297d4ff22d966b89c3
1 \input texinfo @c -*- texinfo -*-
3 @iftex
4 @settitle QEMU Accelerator Technical Documentation
5 @titlepage
6 @sp 7
7 @center @titlefont{QEMU Accelerator Technical Documentation}
8 @sp 3
9 @end titlepage
10 @end iftex
12 @chapter Introduction
14 The QEMU Accelerator (KQEMU) is a driver allowing a user application
15 to run x86 code in a Virtual Machine (VM). The code can be either user
16 or kernel code, in 64, 32 or 16 bit protected mode. KQEMU is very
17 similar in essence to the VM86 Linux syscall call, but it adds some
18 new concepts to improve memory handling.
20 KQEMU is ported on many host OSes (currently Linux, Windows, FreeBSD,
21 Solaris). It can execute code from many guest OSes (e.g. Linux,
22 Windows 2000/XP) even if the host CPU does not support hardware
23 virtualization.
25 In that document, we assume that the reader has good knowledge of the
26 x86 processor and of the problems associated with the virtualization
27 of x86 code.
29 @chapter API definition
31 We describe the version 1.3.0 of the Linux implementation. The
32 implementations on other OSes use the same calls, so they can be
33 understood by reading the Linux API specification.
35 @node phys_page_table
36 @section RAM, Physical and Virtual addresses
38 KQEMU manipulates three kinds of addresses:
40 @itemize
42 @item RAM addresses are between 0 and the available VM RAM size minus one. 
43 They are currently stored on 32 bit words.
45 @item Physical addresses are addresses after MMU translation.
47 @item Virtual addresses are addresses before MMU translation.
49 @end itemize
51 KQEMU has a physical page table which is used to associate a RAM
52 address or a device I/O address range to a given physical page. It
53 also tells if a given RAM address is visible as read-only memory. The
54 same RAM address can be mapped at several different physical
55 addresses. Only 4 GB of physical address space is supported in the
56 current KQEMU implementation. Hence the bits of order >= 32 of the
57 physical addresses are ignored.
59 @node dirty_pages
60 @section RAM page dirtiness
62 It is very important for the VM to be able to tell if a given RAM page
63 has been modified. It can be used to optimize VGA refreshes, to flush
64 a dynamic translator cache (when used with QEMU), to handle live
65 migration or to optimize MMU emulation.
67 In KQEMU, each RAM page has an associated @emph{dirty byte} in the
68 array @code{init_params.ram_dirty}. The dirty byte is set to
69 @code{0xff} if the corresponding RAM page is modified. That way, at
70 most 8 clients can manage a dirty bit in each page.
72 KQEMU reserves one dirty bit @code{0x04} for its internal use.
74 The client must notify KQEMU if some entries of the array
75 @code{init_params.ram_dirty} were modified from @code{0xff} to a
76 different value. The address of the corresponding RAM pages are stored
77 by the client in the array @code{init_parms.ram_pages_to_update}.
79 The client must also notify KQEMU if a RAM page has been modified
80 independently of the @code{init_params.ram_dirty} state. It is done
81 with the @code{init_params.modified_ram_pages} array. 
83 Symmetrically, KQEMU notifies the client if a RAM page has been
84 modified with the @code{init_params.modified_ram_pages} array. The
85 client can use this information for example to invalidate a dynamic
86 translation cache.
88 @section @file{/dev/kqemu} device
90 A user client wishing to create a new virtual machine must open the
91 device @file{/dev/kqemu}. There is no hard limit on the number of
92 virtual machines that can be created and run at the same time, except
93 for the available memory.
95 @section @code{KQEMU_GET_VERSION} ioctl
97 It returns the KQEMU API version as an int. The client must use it to
98 determine if it is compatible with the KQEMU driver.
100 @section @code{KQEMU_INIT} ioctl
102 Input parameter: @code{struct kqemu_init init_params}
104 It must be called once to initialize the VM. The following structure
105 is used as input parameter:
107 @example
108 struct kqemu_init @{
109     uint8_t *ram_base;
110     uint64_t ram_size;
111     uint8_t *ram_dirty;
112     uint64_t *pages_to_flush;
113     uint64_t *ram_pages_to_update;
114     uint64_t *modified_ram_pages;
116 @end example
118 The pointers @code{ram_base}, @code{ram_dirty},
119 @code{phys_to_ram_map}, @code{pages_to_flush},
120 @code{ram_pages_to_update} and @code{modified_ram_pages} must be page
121 aligned and must point to user allocated memory.
123 On Linux, due to a kernel bug related to memory swapping, the
124 corresponding memory must be mmaped from a file. We plan to remove
125 this restriction in a future implementation.
127 @code{ram_size} must be a multiple of 4K and is the quantity of RAM
128 allocated to the VM.
130 @code{ram_base} is a pointer to the VM RAM. It must contain at least
131 @code{ram_size} bytes.
133 @code{ram_dirty} is a pointer to a byte array of length
134 @code{ramsize/4096}. Each byte indicates if the corresponding VM RAM
135 page has been modified (see @ref{dirty_pages})
137 @code{pages_to_flush} is a pointer to an array of
138 @code{KQEMU_MAX_PAGES_TO_FLUSH} longs. It is used to indicate which
139 TLB must be flushed before executing code in the VM.
141 @code{ram_pages_to_update} is a pointer to an array of
142 @code{KQEMU_MAX_RAM_PAGES_TO_UPDATE} longs. It is used to notify the VM that
143 some RAM pages have been dirtied.
145 @code{modified_ram_pages} is a pointer to an array of
146 @code{KQEMU_MAX_MODIFIED_RAM_PAGES} longs. It is used to notify the VM or the
147 client that RAM pages have been modified.
149 The value 0 is return if the ioctl succeeded.
151 @section @code{KQEMU_SET_PHYS_MEM} ioctl
153 The following structure is used as input parameter:
155 @example
156 struct kqemu_phys_mem @{
157     uint64_t phys_addr;
158     uint64_t size;        
159     uint64_t ram_addr;
160     uint32_t io_index;
161     uint32_t padding1;
163 @end example
165 The ioctl modifies the internal KQEMU physical to ram mappings. After
166 the ioctl is executed, the physical address range @code{[phys_addr;
167 phys_addr + size[} is mapped to the RAM addresses @code{[ram_addr;
168 ram_addr + size[} if @code{io_index} is @code{KQEMU_IO_MEM_RAM} or
169 @code{KQEMU_IO_MEM_ROM}. If @code{KQEMU_IO_MEM_ROM} is used, the
170 writes to the RAM are ignored. 
172 When @code{io_index} is @code{KQEMU_IO_MEM_UNASSIGNED}, it means the
173 physical memory range corresponds to a device I/O region. When a
174 memory access is done to it, @code{KQEMU_EXEC} returns with
175 @code{cpu_state.retval} set to @code{KQEMU_RET_SOFTMMU}.
177 @section @code{KQEMU_MODIFY_RAM_PAGE} ioctl
179 Input parameter: @code{int nb_pages}
181 Notify the VM that @code{nb_pages} RAM pages were modified. The
182 corresponding RAM page addresses are written by the client in the
183 @code{init_state.modified_ram_pages} array given with the KQEMU_INIT ioctl.
185 Note: This ioctl does currently nothing, but the clients must use it
186 for later compatibility.
188 @section @code{KQEMU_EXEC} ioctl
190 Input/Output parameter: @code{struct kqemu_cpu_state cpu_state}
192 Structure definitions:
193 @example
194 struct kqemu_segment_cache @{
195     uint16_t selector;
196     uint16_t padding1;
197     uint32_t flags;
198     uint64_t base;
199     uint32_t limit;
200     uint32_t padding2;
203 struct kqemu_cpu_state @{
204     uint64_t regs[16];
205     uint64_t eip;
206     uint64_t eflags;
208     struct kqemu_segment_cache segs[6]; /* selector values */
209     struct kqemu_segment_cache ldt;
210     struct kqemu_segment_cache tr;
211     struct kqemu_segment_cache gdt; /* only base and limit are used */
212     struct kqemu_segment_cache idt; /* only base and limit are used */
214     uint64_t cr0;
215     uint64_t cr2;
216     uint64_t cr3;
217     uint64_t cr4;
218     uint64_t a20_mask;
220     /* sysenter registers */
221     uint64_t sysenter_cs;
222     uint64_t sysenter_esp;
223     uint64_t sysenter_eip;
224     uint64_t efer;
225     uint64_t star;
226     
227     uint64_t lstar;
228     uint64_t cstar;
229     uint64_t fmask;
230     uint64_t kernelgsbase;
232     uint64_t tsc_offset;
234     uint64_t dr0;
235     uint64_t dr1;
236     uint64_t dr2;
237     uint64_t dr3;
238     uint64_t dr6;
239     uint64_t dr7;
241     uint8_t cpl;
242     uint8_t user_only;
243     uint16_t padding1;
245     uint32_t error_code; /* error_code when exiting with an exception */
246     uint64_t next_eip; /* next eip value when exiting with an interrupt */
247     uint32_t nb_pages_to_flush;
248     int32_t retval;
250     uint32_t nb_ram_pages_to_update; 
252     uint32_t nb_modified_ram_pages;
254 @end example
256 Execute x86 instructions in the VM context. The full x86 CPU state is
257 defined in this structure. It contains in particular the value of the
258 8 (or 16 for x86_64) general purpose registers, the contents of the
259 segment caches, the RIP and EFLAGS values, etc...
261 If @code{cpu_state.user_only} is 1, a user only emulation is
262 done. @code{cpu_state.cpl} must be 3 in that case.
264 @code{KQEMU_EXEC} does the following:
266 @enumerate 
268 @item Update the internal dirty state of the
269 @code{cpu_state.nb_ram_pages_to_update} RAM pages from the array
270 @code{init_params.ram_pages_to_update}. If
271 @code{cpu_state.nb_ram_pages_to_update} has the value
272 @code{KQEMU_RAM_PAGES_UPDATE_ALL}, it means that all the RAM pages may
273 have been dirtied. The array @code{init_params.ram_pages_to_update} is
274 ignored in that case.
276 @item Update the internal KQEMU state by taking into account that the 
277 @code{cpu_state.nb_modified_ram_pages} RAM pages from the array
278 @code{init_params.modified_ram_pages} where modified by the client.
280 @item Flush virtual CPU TLBs corresponding to the virtual address from 
281 the array @code{init_params.pages_to_flush} of length
282 @code{cpu_state.nb_pages_to_flush}. If
283 @code{cpu_state.nb_pages_to_flush} is @code{KQEMU_FLUSH_ALL}, all the
284 TLBs are flushed. The array @code{init_params.pages_to_flush} is
285 ignored in that case.
287 @item Load the virtual CPU state from @code{cpu_state}.
289 @item Execute some code in the VM context.
291 @item Save the virtual CPU state into @code{cpu_state}.
293 @item Indicate the reason for which the execution was stopped in 
294 @code{cpu_state.retval}.
296 @item Update @code{cpu_state.nb_pages_to_flush} and 
297 @code{init_params.pages_to_flush} to notify the client that some
298 virtual CPU TLBs were flushed. The client can use this notification to
299 synchronize its own virtual TLBs with KQEMU.
301 @item Set @code{cpu_state.nb_ram_pages_to_update} to 1 if some 
302 RAM dirty bytes were transitionned from dirty (0xff) to a non dirty
303 value. Otherwise, @code{cpu_state.nb_ram_pages_to_update} is set to 0.
305 @item Update @code{cpu_state.nb_modified_ram_pages} and 
306 @code{init_params.modified_ram_pages} to notify the client that some
307 RAM pages were modified. 
309 @end enumerate
311 @code{cpu_state.retval} indicate the reason why the execution was
312 stopped:
314 @table @code
315 @item KQEMU_RET_EXCEPTION | n
316 The virtual CPU raised an exception and KQEMU cannot handle it. The
317 exception number @var{n} is stored in the 8 low order bits. The field
318 @code{cpu_state.error_code} contains the exception error code if it is
319 needed. It should be noted that in @emph{user only} emulation, KQEMU
320 handles no exceptions by itself.
322 @item KQEMU_RET_INT | n
323 (@emph{user only} emulation) The virtual CPU generated a software
324 interrupt (INT instruction for example). The exception number @var{n}
325 is stored in the 8 low order bits. The field @code{cpu_state.next_eip}
326 contains value of RIP after the instruction raising the
327 interrupt. @code{cpu_state.eip} contains the value of RIP at the
328 intruction raising the interrupt.
330 @item KQEMU_RET_SOFTMMU
331 The virtual CPU could not handle the current instruction. This is not
332 a fatal error. Usually the client just needs to interpret it. It can
333 happen because of the following reasons:
335 @itemize
336 @item memory access to an unassigned address or unknown device type ;
338 @item an instruction cannot be accurately executed by KQEMU 
339 (e.g. SYSENTER, HLT, ...) ;
341 @item more than KQEMU_MAX_MODIFIED_RAM_PAGES were modified ;
343 @item some unsupported bits were modified in CR0 or CR4 ;
345 @item GDT.base or LDT.base are not a multiple of 8 ;
347 @item the GDT or LDT tables were modified while CPL = 3 ;
349 @item EFLAGS.VM was set.
351 @end itemize
353 @item KQEMU_RET_INTR
354 A signal from the OS interrupted KQEMU.
356 @item KQEMU_RET_SYSCALL
357 (@emph{user only} emulation) The SYSCALL instruction was executed. The
358 field @code{cpu_state.next_eip} contains value of RIP after the
359 instruction. @code{cpu_state.eip} contains the RIP of the intruction.
361 @item KQEMU_RET_ABORT
362 An unrecoverable error was detected. This is usually due to a bug in
363 KQEMU, so it should never happen !
365 @end table
367 @chapter KQEMU inner working and limitations 
369 @section Inner working
371 The main priority when implementing KQEMU was simplicity and
372 security. Unlike other virtualization systems, it does not do any
373 dynamic translation nor code patching. 
375 @itemize
377 @item KQEMU always executes the target code at CPL = 3 on the host
378 processor. It means that KQEMU can use the page protections to ensure
379 that the VM cannot modify the host OS nor the KQEMU monitor. Moreover,
380 it means that KQEMU does not need to modify the segment limits to
381 ensure memory protection. Another advantage is that this methods works
382 with 64 bit code too.
384 @item KQEMU maintains a shadow page table simulating the TLBs of the 
385 virtual CPU.  The shadow page table persists between calls to
386 KQEMU_EXEC.
388 @item When the target CPL is 3, the target GDT and LDT are copied to 
389 the host GDT and LDT so that the LAR and LSL instructions return
390 a meaningful value. This is important for 16 bit code.
392 @item When the target CPL is different to 3, the host GDT and LDT 
393 are cleared so that any segment loading causes a General Protection
394 Fault. That way, KQEMU can intercept every segment loading.
396 @item All the code running with EFLAGS.IF = 0 is interpreted so that 
397 EFLAGS.IF can be accurately reset in the VM. Fortunately, moderns OSes
398 tend to execute very little code with interrupt disabled.
400 @item KQEMU maintains dirty bits for every RAM pages so that modified 
401 RAM pages can be tracked. It it useful to know if the GDT and LDT are
402 modified in user mode, and will be useful later to optimize shadow
403 page tables switching. It is also useful to maintain the coherency of
404 the user space QEMU translation cache.
406 @end itemize
408 @section General limitations
410 Note 1: KQEMU does not currently use the hardware virtualization
411 features of newer x86 CPUs. We expect that the limitations would be
412 different in that case.
414 Note 2: KQEMU supports both x86 and x86_64 CPUs.
416 Before entering the VM, the following conditions must be satisfied :
418 @enumerate
420 @item CR0.PE = 1 (protected mode must be enabled)
422 @item CR0.MP = 1 (native math support)
424 @item CR0.WP = 1 (write protection for user pages)
426 @item EFLAGS.VM = 0 (no VM86 support)
428 @item At least 8 consecutive GDT descriptors must be available 
429 (currently at a fixed location in the GDT).
431 @item At least 32 MB of virtual address must be free (currently at a 
432 fixed location).
434 @item All the pages containing the LDT and GDT must be RAM pages.
436 @end enumerate
438 If EFLAGS.IF is set, the following assumptions are made on the
439 executing code:
441 @enumerate
442 @item If EFLAGS.IOPL = 3, EFLAGS.IOPL = 0 is returned in EFLAGS.
444 @item POPF cannot be used to clear EFLAGS.IF
446 @item RDTSC returns host cycles (could be improved if needed).
448 @item The values returned by SGDT, SIDT, SLDT are invalid.
450 @item Reading CS.rpl and SS.rpl always returns 3 regardless of the CPL.
452 @item in 64 bit mode with CPL != 3, reading SS.sel does not give 0 
453 if the OS stored 0 in it.
455 @item LAR, LSL, VERR, VERW return invalid results if CPL != 3.
457 @item The CS and SS segment cache must be consistent with the descriptor
458 tables.
460 @item The DS, ES, FS, and GS segment cache must be consistent with the
461 descriptor tables for CPL = 3.
463 @item Some rarely used intructions trap to the user space client
464 (performance issue).
466 @end enumerate
468 If eflags.IF if reset the code is interpreted, so the VM code can be
469 accurately executed. Some intructions trap to the user space emulator
470 because the interpreter does not handle them. A limitation of the
471 interpreter is that currently segment limits are not always tested.
473 @section Security
475 The VM code is always run with CPL = 3 on the host, so @emph{the VM
476 code has no more priviliedge than regular user code}.
478 The MMU is used to protect the memory used by the KQEMU monitor. That
479 way, no segment limit patching is necessary. Moreover, the guest OS is
480 free to use any virtual address, in particular the ones near the start
481 or the end of the virtual address space. The price to pay is that CR3
482 must be modified at every emulated system call because different page
483 tables are needed for user and kernel modes.
485 @section Developments Ideas
487 @itemize
489 @item Instead of interpreting the code when IF=0, compile it dynamically. 
490 The dynamic compiler itself can be implemented in user space, so the
491 kernel module would be simplified.
493 @item Use APIs closer to KVM.
495 @item Optimization of the page table shadowing. A shadow page table cache 
496 could be implemented by tracking the modification of the guest page
497 tables. The exact performance gains are difficult to estimate because
498 the tracking itself would introduce some performance loss.
500 @item Support of guest SMP. There is no particular problem except 
501 when a RAM page must be unlocked because the host has not enough
502 memory. This particular case needs specific Inter Processor Interrupts
503 (IPI).
505 @item Dynamic relocation of the monitor code so that a 32 MB hole 
506 in the guest address space is found automatically without making
507 assumptions on the guest OS.
509 @end itemize