VirtualBox

source: vbox/trunk/doc/manual/en_US/user_Technical.xml@ 43324

Last change on this file since 43324 was 39765, checked in by vboxsync, 13 years ago

doc/manual: minor typos.

File size: 42.9 KB
Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
3"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
4<chapter id="TechnicalBackground">
5 <title>Technical background</title>
6
7 <para>The contents of this chapter are not required to use VirtualBox
8 successfully. The following is provided as additional information for
9 readers who are more familiar with computer architecture and technology and
10 wish to find out more about how VirtualBox works "under the hood".</para>
11
12 <sect1 id="vboxconfigdata">
13 <title>Where VirtualBox stores its files</title>
14
15 <para>In VirtualBox, a virtual machine and its settings are described in a
16 virtual machine settings file in XML format. In addition, most virtual
17 machine have one or more virtual hard disks, which are typically
18 represented by disk images (e.g. in VDI format). Where all these files are
19 stored depends on which version of VirtualBox created the machine.</para>
20
21 <sect2>
22 <title>Machines created by VirtualBox version 4.0 or later</title>
23
24 <para>Starting with version 4.0, by default, each virtual machine has
25 one directory on your host computer where all the files of that machine
26 are stored -- the XML settings file (with a
27 <computeroutput>.vbox</computeroutput> file extension) and its disk
28 images.</para>
29
30 <para>By default, this "machine folder" is placed in a common folder
31 called "VirtualBox VMs", which VirtualBox creates in the current system
32 user's home directory. The location of this home directory depends on
33 the conventions of the host operating system:</para>
34
35 <itemizedlist>
36 <listitem>
37 <para>On Windows, this is
38 <computeroutput>%HOMEDRIVE%%HOMEPATH%</computeroutput>; typically
39 something like <computeroutput>C:\Documents and
40 Settings\Username\</computeroutput>.</para>
41 </listitem>
42
43 <listitem>
44 <para>On Mac OS X, this is
45 <computeroutput>/Users/username</computeroutput>.</para>
46 </listitem>
47
48 <listitem>
49 <para>On Linux and Solaris, this is
50 <computeroutput>/home/username</computeroutput>.</para>
51 </listitem>
52 </itemizedlist>
53
54 <para>For simplicity, we will abbreviate this as
55 <computeroutput>$HOME</computeroutput> below. Using that convention, the
56 common folder for all virtual machines is
57 <computeroutput>$HOME/VirtualBox VMs</computeroutput>.</para>
58
59 <para>As an example, when you create a virtual machine called "Example
60 VM", you will find that VirtualBox creates<orderedlist>
61 <listitem>
62 <para>the folder <computeroutput>$HOME/VirtualBox VMs/Example
63 VM/</computeroutput> and, in that folder,</para>
64 </listitem>
65
66 <listitem>
67 <para>the settings file <computeroutput>Example
68 VM.vbox</computeroutput> and</para>
69 </listitem>
70
71 <listitem>
72 <para>the virtual disk image <computeroutput>Example
73 VM.vdi</computeroutput>.</para>
74 </listitem>
75 </orderedlist></para>
76
77 <para>This is the default layout if you use the "Create new virtual
78 machine" wizard as described in <xref linkend="gui-createvm" />. Once
79 you start working with the VM, additional files will show up: you will
80 find log files in a subfolder called
81 <computeroutput>Logs</computeroutput>, and once you have taken
82 snapshots, they will appear in a
83 <computeroutput>Snapshots</computeroutput> subfolder. For each VM, you
84 can change the location of its snapsnots folder in the VM
85 settings.</para>
86
87 <para>You can change the default machine folder by selecting
88 "Preferences" from the "File" menu in the VirtualBox main window. Then,
89 in the window that pops up, click on the "General" tab. Alternatively,
90 use <computeroutput>VBoxManage setproperty
91 machinefolder</computeroutput>; see <xref
92 linkend="vboxmanage-setproperty" />.</para>
93 </sect2>
94
95 <sect2>
96 <title>Machines created by VirtualBox versions before 4.0</title>
97
98 <para>If you have upgraded to VirtualBox 4.0 from an earlier version of
99 VirtualBox, you probably have settings files and disks in the earlier
100 file system layout.</para>
101
102 <para>Before version 4.0, VirtualBox separated the machine settings
103 files from virtual disk images. The machine settings files had an
104 <computeroutput>.xml</computeroutput> file extension and resided in a
105 folder called "Machines" under the global VirtualBox configuration
106 directory (see the next section). So, for example, on Linux, this was
107 the hidden <computeroutput>$HOME/.VirtualBox/Machines</computeroutput>
108 directory. The default hard disks folder was called "HardDisks" and
109 resided in the <computeroutput>.VirtualBox</computeroutput> folder as
110 well. Both locations could be changed by the user in the global
111 preferences. (The concept of a "default hard disk folder" has been
112 abandoned with VirtualBox 4.0, since disk images now reside in each
113 machine's folder by default.)</para>
114
115 <para>The old layout had several severe disadvantages.<orderedlist>
116 <listitem>
117 <para>It was very difficult to move a virtual machine from one
118 host to another because the files involved did not reside in the
119 same folder. In addition, the virtual media of all machines were
120 registered with a global registry in the central VirtualBox
121 settings file
122 (<computeroutput>$HOME/.VirtualBox/VirtualBox.xml</computeroutput>).</para>
123
124 <para>To move a machine to another host, it was therefore not
125 enough to move the XML settings file and the disk images (which
126 were in different locations), but the hard disk entries from the
127 global media registry XML had to be meticulously copied as well,
128 which was close to impossible if the machine had snapshots and
129 therefore differencing images.</para>
130 </listitem>
131
132 <listitem>
133 <para>Storing virtual disk images, which can grow very large,
134 under the hidden <computeroutput>.VirtualBox</computeroutput>
135 directory (at least on Linux and Solaris hosts) made many users
136 wonder where their disk space had gone.</para>
137 </listitem>
138 </orderedlist></para>
139
140 <para>Whereas new VMs created with VirtualBox 4.0 or later will conform
141 to the new layout, for maximum compatibility, old VMs are
142 <emphasis>not</emphasis> converted to the new layout. Otherwise machine
143 settings would be irrevocably broken if a user downgraded from 4.0 back
144 to an older version of VirtualBox.</para>
145 </sect2>
146
147 <sect2>
148 <title>Global configuration data</title>
149
150 <para>In addition to the files of the virtual machines, VirtualBox
151 maintains global configuration data. On Windows, Linux and Solaris, this
152 is in <computeroutput>$HOME/.VirtualBox</computeroutput> (which makes it
153 hidden on Linux and Solaris), whereas on a Mac this resides in
154 <computeroutput>$HOME/Library/VirtualBox</computeroutput>.</para>
155
156 <para>VirtualBox creates this configuration directory automatically if
157 necessary. Optionally, you can supply an alternate configuration
158 directory by setting the
159 <computeroutput><literal>VBOX_USER_HOME</literal></computeroutput>
160 environment variable. (Since the global
161 <computeroutput>VirtualBox.xml</computeroutput> settings file points to
162 all other configuration files, this allows for switching between several
163 VirtualBox configurations entirely.)</para>
164
165 <para>Most importantly, in this directory, VirtualBox stores its global
166 settings file, another XML file called
167 <computeroutput>VirtualBox.xml</computeroutput>. This includes global
168 configuration options and the list of registered virtual machines with
169 pointers to their XML settings files. (Neither the location of this file
170 nor its directory has changed with VirtualBox 4.0.)</para>
171
172 <para>Before VirtualBox 4.0, all virtual media (disk image files) were
173 also contained in a global registry in this settings file. For
174 compatibility, this media registry still exists if you upgrade
175 VirtualBox and there are media from machines which were created with a
176 version before 4.0. If you have no such machines, then there will be no
177 global media registry; with VirtualBox 4.0, each machine XML file has
178 its own media registry.</para>
179
180 <para>Also before VirtualBox 4.0, the default "Machines" folder and the
181 default "HardDisks" folder resided under the VirtualBox configuration
182 directory (e.g.
183 <computeroutput>$HOME/.VirtualBox/Machines</computeroutput> on Linux).
184 If you are upgrading from a VirtualBox version before 4.0, files in
185 these directories are not automatically moved in order not to break
186 backwards compatibility.</para>
187 </sect2>
188
189 <sect2>
190 <title>Summary of 4.0 configuration changes</title>
191
192 <table>
193 <title>ignoreme</title>
194
195 <tgroup cols="3">
196 <tbody>
197 <row>
198 <entry></entry>
199
200 <entry><emphasis role="bold">Before 4.0</emphasis></entry>
201
202 <entry><emphasis role="bold">4.0 or above</emphasis></entry>
203 </row>
204
205 <row>
206 <entry>Default machines folder</entry>
207
208 <entry><computeroutput>$HOME/.VirtualBox/Machines</computeroutput></entry>
209
210 <entry><computeroutput>$HOME/VirtualBox
211 VMs</computeroutput></entry>
212 </row>
213
214 <row>
215 <entry>Default disk image location</entry>
216
217 <entry><computeroutput>$HOME/.VirtualBox/HardDisks</computeroutput></entry>
218
219 <entry>In each machine's folder</entry>
220 </row>
221
222 <row>
223 <entry>Machine settings file extension</entry>
224
225 <entry><computeroutput>.xml</computeroutput></entry>
226
227 <entry><computeroutput>.vbox</computeroutput></entry>
228 </row>
229
230 <row>
231 <entry>Media registry</entry>
232
233 <entry>Global <computeroutput>VirtualBox.xml</computeroutput>
234 file</entry>
235
236 <entry>Each machine settings file</entry>
237 </row>
238
239 <row>
240 <entry>Media registration</entry>
241
242 <entry>Explicit open/close required</entry>
243
244 <entry>Automatic on attach</entry>
245 </row>
246 </tbody>
247 </tgroup>
248 </table>
249 </sect2>
250
251 <sect2>
252 <title>VirtualBox XML files</title>
253
254 <para>VirtualBox uses XML for both the machine settings files and the
255 global configuration file,
256 <computeroutput>VirtualBox.xml</computeroutput>.</para>
257
258 <para>All VirtualBox XML files are versioned. When a new settings file
259 is created (e.g. because a new virtual machine is created), VirtualBox
260 automatically uses the settings format of the current VirtualBox
261 version. These files may not be readable if you downgrade to an earlier
262 version of VirtualBox. However, when VirtualBox encounters a settings
263 file from an earlier version (e.g. after upgrading VirtualBox), it
264 attempts to preserve the settings format as much as possible. It will
265 only silently upgrade the settings format if the current settings cannot
266 be expressed in the old format, for example because you enabled a
267 feature that was not present in an earlier version of
268 VirtualBox.<footnote>
269 <para>As an example, before VirtualBox 3.1, it was only possible to
270 enable or disable a single DVD drive in a virtual machine. If it was
271 enabled, then it would always be visible as the secondary master of
272 the IDE controller. With VirtualBox 3.1, DVD drives can be attached
273 to arbitrary slots of arbitrary controllers, so they could be the
274 secondary slave of an IDE controller or in a SATA slot. If you have
275 a machine settings file from an earlier version and upgrade
276 VirtualBox to 3.1 and then move the DVD drive from its default
277 position, this cannot be expressed in the old settings format; the
278 XML machine file would get written in the new format, and a backup
279 file of the old format would be kept.</para>
280 </footnote> In such cases, VirtualBox backs up the old settings file
281 in the virtual machine's configuration directory. If you need to go back
282 to the earlier version of VirtualBox, then you will need to manually
283 copy these backup files back.</para>
284
285 <para>We intentionally do not document the specifications of the
286 VirtualBox XML files, as we must reserve the right to modify them in the
287 future. We therefore strongly suggest that you do not edit these files
288 manually. VirtualBox provides complete access to its configuration data
289 through its the <computeroutput>VBoxManage</computeroutput> command line
290 tool (see <xref linkend="vboxmanage" />) and its API (see <xref
291 linkend="VirtualBoxAPI" />).</para>
292 </sect2>
293 </sect1>
294
295 <sect1 id="technical-components">
296 <title>VirtualBox executables and components</title>
297
298 <para>VirtualBox was designed to be modular and flexible. When the
299 VirtualBox graphical user interface (GUI) is opened and a VM is started,
300 at least three processes are running:<orderedlist>
301 <listitem>
302 <para><computeroutput>VBoxSVC</computeroutput>, the VirtualBox
303 service process which always runs in the background. This process is
304 started automatically by the first VirtualBox client process (the
305 GUI, <computeroutput>VBoxManage</computeroutput>,
306 <computeroutput>VBoxHeadless</computeroutput>, the web service or
307 others) and exits a short time after the last client exits. The
308 service is responsible for bookkeeping, maintaining the state of all
309 VMs, and for providing communication between VirtualBox components.
310 This communication is implemented via COM/XPCOM.<note>
311 <para>When we refer to "clients" here, we mean the local clients
312 of a particular <computeroutput>VBoxSVC</computeroutput> server
313 process, not clients in a network. VirtualBox employs its own
314 client/server design to allow its processes to cooperate, but
315 all these processes run under the same user account on the host
316 operating system, and this is totally transparent to the
317 user.</para>
318 </note></para>
319 </listitem>
320
321 <listitem>
322 <para>The GUI process, <computeroutput>VirtualBox</computeroutput>,
323 a client application based on the cross-platform Qt library. When
324 started without the <computeroutput>--startvm</computeroutput>
325 option, this application acts as the VirtualBox manager, displaying
326 the VMs and their settings. It then communicates settings and state
327 changes to <computeroutput>VBoxSVC</computeroutput> and also
328 reflects changes effected through other means, e.g.,
329 <computeroutput>VBoxManage</computeroutput>.</para>
330 </listitem>
331
332 <listitem>
333 <para>If the <computeroutput>VirtualBox</computeroutput> client
334 application is started with the
335 <computeroutput>--startvm</computeroutput> argument, it loads the
336 VMM library which includes the actual hypervisor and then runs a
337 virtual machine and provides the input and output for the
338 guest.</para>
339 </listitem>
340 </orderedlist></para>
341
342 <para>Any VirtualBox front-end (client) will communicate with the service
343 process and can both control and reflect the current state. For example,
344 either the VM selector or the VM window or VBoxManage can be used to pause
345 the running VM, and other components will always reflect the changed
346 state.</para>
347
348 <para>The VirtualBox GUI application is only one of several available
349 front ends (clients). The complete list shipped with VirtualBox
350 is:<orderedlist>
351 <listitem>
352 <para><computeroutput>VirtualBox</computeroutput>, the Qt front end
353 implementing the manager and running VMs;</para>
354 </listitem>
355
356 <listitem>
357 <para><computeroutput>VBoxManage</computeroutput>, a less
358 user-friendly but more powerful alternative, described in <xref
359 linkend="vboxmanage" />.</para>
360 </listitem>
361
362 <listitem>
363 <para><computeroutput>VBoxSDL</computeroutput>, a simple graphical
364 front end based on the SDL library; see <xref
365 linkend="vboxsdl" />.</para>
366 </listitem>
367
368 <listitem>
369 <para><computeroutput>VBoxHeadless</computeroutput>, a VM front end
370 which does not directly provide any video output and keyboard/mouse
371 input, but allows redirection via VirtualBox Remote Desktop Extension;
372 see <xref linkend="vboxheadless" />.</para>
373 </listitem>
374
375 <listitem>
376 <para><computeroutput>vboxwebsrv</computeroutput>, the VirtualBox
377 web service process which allows for controlling a VirtualBox host
378 remotely. This is described in detail in the VirtualBox Software
379 Development Kit (SDK) reference; please see <xref
380 linkend="VirtualBoxAPI" /> for details.</para>
381 </listitem>
382
383 <listitem>
384 <para>The VirtualBox Python shell, a Python alternative to
385 VBoxManage. This is also described in the SDK reference.</para>
386 </listitem>
387 </orderedlist></para>
388
389 <para>Internally, VirtualBox consists of many more or less separate
390 components. You may encounter these when analyzing VirtualBox internal
391 error messages or log files. These include:</para>
392
393 <itemizedlist>
394 <listitem>
395 <para>IPRT, a portable runtime library which abstracts file access,
396 threading, string manipulation, etc. Whenever VirtualBox accesses host
397 operating features, it does so through this library for cross-platform
398 portability.</para>
399 </listitem>
400
401 <listitem>
402 <para>VMM (Virtual Machine Monitor), the heart of the
403 hypervisor.</para>
404 </listitem>
405
406 <listitem>
407 <para>EM (Execution Manager), controls execution of guest code.</para>
408 </listitem>
409
410 <listitem>
411 <para>REM (Recompiled Execution Monitor), provides software emulation
412 of CPU instructions.</para>
413 </listitem>
414
415 <listitem>
416 <para>TRPM (Trap Manager), intercepts and processes guest traps and
417 exceptions.</para>
418 </listitem>
419
420 <listitem>
421 <para>HWACCM (Hardware Acceleration Manager), provides support for
422 VT-x and AMD-V.</para>
423 </listitem>
424
425 <listitem>
426 <para>PDM (Pluggable Device Manager), an abstract interface between
427 the VMM and emulated devices which separates device implementations
428 from VMM internals and makes it easy to add new emulated devices.
429 Through PDM, third-party developers can add new virtual devices to
430 VirtualBox without having to change VirtualBox itself.</para>
431 </listitem>
432
433 <listitem>
434 <para>PGM (Page Manager), a component controlling guest paging.</para>
435 </listitem>
436
437 <listitem>
438 <para>PATM (Patch Manager), patches guest code to improve and speed up
439 software virtualization.</para>
440 </listitem>
441
442 <listitem>
443 <para>TM (Time Manager), handles timers and all aspects of time inside
444 guests.</para>
445 </listitem>
446
447 <listitem>
448 <para>CFGM (Configuration Manager), provides a tree structure which
449 holds configuration settings for the VM and all emulated
450 devices.</para>
451 </listitem>
452
453 <listitem>
454 <para>SSM (Saved State Manager), saves and loads VM state.</para>
455 </listitem>
456
457 <listitem>
458 <para>VUSB (Virtual USB), a USB layer which separates emulated USB
459 controllers from the controllers on the host and from USB devices;
460 this also enables remote USB.</para>
461 </listitem>
462
463 <listitem>
464 <para>DBGF (Debug Facility), a built-in VM debugger.</para>
465 </listitem>
466
467 <listitem>
468 <para>VirtualBox emulates a number of devices to provide the hardware
469 environment that various guests need. Most of these are standard
470 devices found in many PC compatible machines and widely supported by
471 guest operating systems. For network and storage devices in
472 particular, there are several options for the emulated devices to
473 access the underlying hardware. These devices are managed by
474 PDM.</para>
475 </listitem>
476
477 <listitem>
478 <para>Guest Additions for various guest operating systems. This is
479 code that is installed from within a virtual machine; see <xref
480 linkend="guestadditions" />.</para>
481 </listitem>
482
483 <listitem>
484 <para>The "Main" component is special: it ties all the above bits
485 together and is the only public API that VirtualBox provides. All the
486 client processes listed above use only this API and never access the
487 hypervisor components directly. As a result, third-party applications
488 that use the VirtualBox Main API can rely on the fact that it is
489 always well-tested and that all capabilities of VirtualBox are fully
490 exposed. It is this API that is described in the VirtualBox SDK
491 mentioned above (again, see <xref linkend="VirtualBoxAPI" />).</para>
492 </listitem>
493 </itemizedlist>
494 </sect1>
495
496 <sect1 id="hwvirt">
497 <title>Hardware vs. software virtualization</title>
498
499 <para>VirtualBox allows software in the virtual machine to run directly on
500 the processor of the host, but an array of complex techniques is employed
501 to intercept operations that would interfere with your host. Whenever the
502 guest attempts to do something that could be harmful to your computer and
503 its data, VirtualBox steps in and takes action. In particular, for lots of
504 hardware that the guest believes to be accessing, VirtualBox simulates a
505 certain "virtual" environment according to how you have configured a
506 virtual machine. For example, when the guest attempts to access a hard
507 disk, VirtualBox redirects these requests to whatever you have configured
508 to be the virtual machine's virtual hard disk -- normally, an image file
509 on your host.</para>
510
511 <para>Unfortunately, the x86 platform was never designed to be
512 virtualized. Detecting situations in which VirtualBox needs to take
513 control over the guest code that is executing, as described above, is
514 difficult. There are two ways in which to achieve this:<itemizedlist>
515 <listitem>
516 <para>Since 2006, Intel and AMD processors have had support for
517 so-called <emphasis role="bold">"hardware
518 virtualization"</emphasis>. This means that these processors can
519 help VirtualBox to intercept potentially dangerous operations that a
520 guest operating system may be attempting and also makes it easier to
521 present virtual hardware to a virtual machine.</para>
522
523 <para>These hardware features differ between Intel and AMD
524 processors. Intel named its technology <emphasis
525 role="bold">VT-x</emphasis>; AMD calls theirs <emphasis
526 role="bold">AMD-V</emphasis>. The Intel and AMD support for
527 virtualization is very different in detail, but not very different
528 in principle.<note>
529 <para>On many systems, the hardware virtualization features
530 first need to be enabled in the BIOS before VirtualBox can use
531 them.</para>
532 </note></para>
533 </listitem>
534
535 <listitem>
536 <para>As opposed to other virtualization software, for many usage
537 scenarios, VirtualBox does not <emphasis>require</emphasis> hardware
538 virtualization features to be present. Through sophisticated
539 techniques, VirtualBox virtualizes many guest operating systems
540 entirely in <emphasis role="bold">software</emphasis>. This means
541 that you can run virtual machines even on older processors which do
542 not support hardware virtualization.</para>
543 </listitem>
544 </itemizedlist></para>
545
546 <para>Even though VirtualBox does not always require hardware
547 virtualization, enabling it is <emphasis>required</emphasis> in the
548 following scenarios:<itemizedlist>
549 <listitem>
550 <para>Certain rare guest operating systems like OS/2 make use of
551 very esoteric processor instructions that are not supported with our
552 software virtualization. For virtual machines that are configured to
553 contain such an operating system, hardware virtualization is enabled
554 automatically.</para>
555 </listitem>
556
557 <listitem>
558 <para>VirtualBox's 64-bit guest support (added with version 2.0) and
559 multiprocessing (SMP, added with version 3.0) both require hardware
560 virtualization to be enabled. (This is not much of a limitation
561 since the vast majority of today's 64-bit and multicore CPUs ship
562 with hardware virtualization anyway; the exceptions to this rule are
563 e.g. older Intel Celeron and AMD Opteron CPUs.)</para>
564 </listitem>
565 </itemizedlist></para>
566
567 <warning>
568 <para>Do not run other hypervisors (open-source or commercial
569 virtualization products) together with VirtualBox! While several
570 hypervisors can normally be <emphasis>installed</emphasis> in parallel,
571 do not attempt to <emphasis>run</emphasis> several virtual machines from
572 competing hypervisors at the same time. VirtualBox cannot track what
573 another hypervisor is currently attempting to do on the same host, and
574 especially if several products attempt to use hardware virtualization
575 features such as VT-x, this can crash the entire host. Also, within
576 VirtualBox, you can mix software and hardware virtualization when
577 running multiple VMs. In certain cases a small performance penalty will
578 be unavoidable when mixing VT-x and software virtualization VMs. We
579 recommend not mixing virtualization modes if maximum performance and low
580 overhead are essential. This does <emphasis>not</emphasis> apply to
581 AMD-V.</para>
582 </warning>
583 </sect1>
584
585 <sect1>
586 <title>Details about software virtualization</title>
587
588 <para>Implementing virtualization on x86 CPUs with no hardware
589 virtualization support is an extraordinarily complex task because the CPU
590 architecture was not designed to be virtualized. The problems can usually
591 be solved, but at the cost of reduced performance. Thus, there is a
592 constant clash between virtualization performance and accuracy.</para>
593
594 <para>The x86 instruction set was originally designed in the 1970s and
595 underwent significant changes with the addition of protected mode in the
596 1980s with the 286 CPU architecture and then again with the Intel 386 and
597 its 32-bit architecture. Whereas the 386 did have limited virtualization
598 support for real mode operation (V86 mode, as used by the "DOS Box" of
599 Windows 3.x and OS/2 2.x), no support was provided for virtualizing the
600 entire architecture.</para>
601
602 <para>In theory, software virtualization is not overly complex. In
603 addition to the four privilege levels ("rings") provided by the hardware
604 (of which typically only two are used: ring 0 for kernel mode and ring 3
605 for user mode), one needs to differentiate between "host context" and
606 "guest context".</para>
607
608 <para>In "host context", everything is as if no hypervisor was active.
609 This might be the active mode if another application on your host has been
610 scheduled CPU time; in that case, there is a host ring 3 mode and a host
611 ring 0 mode. The hypervisor is not involved.</para>
612
613 <para>In "guest context", however, a virtual machine is active. So long as
614 the guest code is running in ring 3, this is not much of a problem since a
615 hypervisor can set up the page tables properly and run that code natively
616 on the processor. The problems mostly lie in how to intercept what the
617 guest's kernel does.</para>
618
619 <para>There are several possible solutions to these problems. One approach
620 is full software emulation, usually involving recompilation. That is, all
621 code to be run by the guest is analyzed, transformed into a form which
622 will not allow the guest to either modify or see the true state of the
623 CPU, and only then executed. This process is obviously highly complex and
624 costly in terms of performance. (VirtualBox contains a recompiler based on
625 QEMU which can be used for pure software emulation, but the recompiler is
626 only activated in special situations, described below.)</para>
627
628 <para>Another possible solution is paravirtualization, in which only
629 specially modified guest OSes are allowed to run. This way, most of the
630 hardware access is abstracted and any functions which would normally
631 access the hardware or privileged CPU state are passed on to the
632 hypervisor instead. Paravirtualization can achieve good functionality and
633 performance on standard x86 CPUs, but it can only work if the guest OS can
634 actually be modified, which is obviously not always the case.</para>
635
636 <para>VirtualBox chooses a different approach. When starting a virtual
637 machine, through its ring-0 support kernel driver, VirtualBox has set up
638 the host system so that it can run most of the guest code natively, but it
639 has inserted itself at the "bottom" of the picture. It can then assume
640 control when needed -- if a privileged instruction is executed, the guest
641 traps (in particular because an I/O register was accessed and a device
642 needs to be virtualized) or external interrupts occur. VirtualBox may then
643 handle this and either route a request to a virtual device or possibly
644 delegate handling such things to the guest or host OS. In guest context,
645 VirtualBox can therefore be in one of three states:</para>
646
647 <para><itemizedlist>
648 <listitem>
649 <para>Guest ring 3 code is run unmodified, at full speed, as much as
650 possible. The number of faults will generally be low (unless the
651 guest allows port I/O from ring 3, something we cannot do as we
652 don't want the guest to be able to access real ports). This is also
653 referred to as "raw mode", as the guest ring-3 code runs
654 unmodified.</para>
655 </listitem>
656
657 <listitem>
658 <para>For guest code in ring 0, VirtualBox employs a nasty trick: it
659 actually reconfigures the guest so that its ring-0 code is run in
660 ring 1 instead (which is normally not used in x86 operating
661 systems). As a result, when guest ring-0 code (actually running in
662 ring 1) such as a guest device driver attempts to write to an I/O
663 register or execute a privileged instruction, the VirtualBox
664 hypervisor in "real" ring 0 can take over.</para>
665 </listitem>
666
667 <listitem>
668 <para>The hypervisor (VMM) can be active. Every time a fault occurs,
669 VirtualBox looks at the offending instruction and can relegate it to
670 a virtual device or the host OS or the guest OS or run it in the
671 recompiler.</para>
672
673 <para>In particular, the recompiler is used when guest code disables
674 interrupts and VirtualBox cannot figure out when they will be
675 switched back on (in these situations, VirtualBox actually analyzes
676 the guest code using its own disassembler). Also, certain privileged
677 instructions such as LIDT need to be handled specially. Finally, any
678 real-mode or protected-mode code (e.g. BIOS code, a DOS guest, or
679 any operating system startup) is run in the recompiler
680 entirely.</para>
681 </listitem>
682 </itemizedlist></para>
683
684 <para>Unfortunately this only works to a degree. Among others, the
685 following situations require special handling:</para>
686
687 <para><orderedlist>
688 <listitem>
689 <para>Running ring 0 code in ring 1 causes a lot of additional
690 instruction faults, as ring 1 is not allowed to execute any
691 privileged instructions (of which guest's ring-0 contains plenty).
692 With each of these faults, the VMM must step in and emulate the code
693 to achieve the desired behavior. While this works, emulating
694 thousands of these faults is very expensive and severely hurts the
695 performance of the virtualized guest.</para>
696 </listitem>
697
698 <listitem>
699 <para>There are certain flaws in the implementation of ring 1 in the
700 x86 architecture that were never fixed. Certain instructions that
701 <emphasis>should</emphasis> trap in ring 1 don't. This affect for
702 example the LGDT/SGDT, LIDT/SIDT, or POPF/PUSHF instruction pairs.
703 Whereas the "load" operation is privileged and can therefore be
704 trapped, the "store" instruction always succeed. If the guest is
705 allowed to execute these, it will see the true state of the CPU, not
706 the virtualized state. The CPUID instruction also has the same
707 problem.</para>
708 </listitem>
709
710 <listitem>
711 <para>A hypervisor typically needs to reserve some portion of the
712 guest's address space (both linear address space and selectors) for
713 its own use. This is not entirely transparent to the guest OS and
714 may cause clashes.</para>
715 </listitem>
716
717 <listitem>
718 <para>The SYSENTER instruction (used for system calls) executed by
719 an application running in a guest OS always transitions to ring 0.
720 But that is where the hypervisor runs, not the guest OS. In this
721 case, the hypervisor must trap and emulate the instruction even when
722 it is not desirable.</para>
723 </listitem>
724
725 <listitem>
726 <para>The CPU segment registers contain a "hidden" descriptor cache
727 which is not software-accessible. The hypervisor cannot read, save,
728 or restore this state, but the guest OS may use it.</para>
729 </listitem>
730
731 <listitem>
732 <para>Some resources must (and can) be trapped by the hypervisor,
733 but the access is so frequent that this creates a significant
734 performance overhead. An example is the TPR (Task Priority) register
735 in 32-bit mode. Accesses to this register must be trapped by the
736 hypervisor, but certain guest operating systems (notably Windows and
737 Solaris) write this register very often, which adversely affects
738 virtualization performance.</para>
739 </listitem>
740 </orderedlist></para>
741
742 <para>To fix these performance and security issues, VirtualBox contains a
743 Code Scanning and Analysis Manager (CSAM), which disassembles guest code,
744 and the Patch Manager (PATM), which can replace it at runtime.</para>
745
746 <para>Before executing ring 0 code, CSAM scans it recursively to discover
747 problematic instructions. PATM then performs <emphasis>in-situ
748 </emphasis>patching, i.e. it replaces the instruction with a jump to
749 hypervisor memory where an integrated code generator has placed a more
750 suitable implementation. In reality, this is a very complex task as there
751 are lots of odd situations to be discovered and handled correctly. So,
752 with its current complexity, one could argue that PATM is an advanced
753 <emphasis>in-situ</emphasis> recompiler.</para>
754
755 <para>In addition, every time a fault occurs, VirtualBox analyzes the
756 offending code to determine if it is possible to patch it in order to
757 prevent it from causing more faults in the future. This approach works
758 well in practice and dramatically improves software virtualization
759 performance.</para>
760 </sect1>
761
762 <sect1>
763 <title>Details about hardware virtualization</title>
764
765 <para>With Intel VT-x, there are two distinct modes of CPU operation: VMX
766 root mode and non-root mode.<itemizedlist>
767 <listitem>
768 <para>In root mode, the CPU operates much like older generations of
769 processors without VT-x support. There are four privilege levels
770 ("rings"), and the same instruction set is supported, with the
771 addition of several virtualization specific instruction. Root mode
772 is what a host operating system without virtualization uses, and it
773 is also used by a hypervisor when virtualization is active.</para>
774 </listitem>
775
776 <listitem>
777 <para>In non-root mode, CPU operation is significantly different.
778 There are still four privilege rings and the same instruction set,
779 but a new structure called VMCS (Virtual Machine Control Structure)
780 now controls the CPU operation and determines how certain
781 instructions behave. Non-root mode is where guest systems
782 run.</para>
783 </listitem>
784 </itemizedlist></para>
785
786 <para>Switching from root mode to non-root mode is called "VM entry", the
787 switch back is "VM exit". The VMCS includes a guest and host state area
788 which is saved/restored at VM entry and exit. Most importantly, the VMCS
789 controls which guest operations will cause VM exits.</para>
790
791 <para>The VMCS provides fairly fine-grained control over what the guests
792 can and can't do. For example, a hypervisor can allow a guest to write
793 certain bits in shadowed control registers, but not others. This enables
794 efficient virtualization in cases where guests can be allowed to write
795 control bits without disrupting the hypervisor, while preventing them from
796 altering control bits over which the hypervisor needs to retain full
797 control. The VMCS also provides control over interrupt delivery and
798 exceptions.</para>
799
800 <para>Whenever an instruction or event causes a VM exit, the VMCS contains
801 information about the exit reason, often with accompanying detail. For
802 example, if a write to the CR0 register causes an exit, the offending
803 instruction is recorded, along with the fact that a write access to a
804 control register caused the exit, and information about source and
805 destination register. Thus the hypervisor can efficiently handle the
806 condition without needing advanced techniques such as CSAM and PATM
807 described above.</para>
808
809 <para>VT-x inherently avoids several of the problems which software
810 virtualization faces. The guest has its own completely separate address
811 space not shared with the hypervisor, which eliminates potential clashes.
812 Additionally, guest OS kernel code runs at privilege ring 0 in VMX
813 non-root mode, obviating the problems by running ring 0 code at less
814 privileged levels. For example the SYSENTER instruction can transition to
815 ring 0 without causing problems. Naturally, even at ring 0 in VMX non-root
816 mode, any I/O access by guest code still causes a VM exit, allowing for
817 device emulation.</para>
818
819 <para>The biggest difference between VT-x and AMD-V is that AMD-V provides
820 a more complete virtualization environment. VT-x requires the VMX non-root
821 code to run with paging enabled, which precludes hardware virtualization
822 of real-mode code and non-paged protected-mode software. This typically
823 only includes firmware and OS loaders, but nevertheless complicates VT-x
824 hypervisor implementation. AMD-V does not have this restriction.</para>
825
826 <para>Of course hardware virtualization is not perfect. Compared to
827 software virtualization, the overhead of VM exits is relatively high. This
828 causes problems for devices whose emulation requires high number of traps.
829 One example is the VGA device in 16-color modes, where not only every I/O
830 port access but also every access to the framebuffer memory must be
831 trapped.</para>
832 </sect1>
833
834 <sect1 id="nestedpaging">
835 <title>Nested paging and VPIDs</title>
836
837 <para>In addition to "plain" hardware virtualization, your processor may
838 also support additional sophisticated techniques:<footnote>
839 <para>VirtualBox 2.0 added support for AMD's nested paging; support
840 for Intel's EPT and VPIDs was added with version 2.1.</para>
841 </footnote><itemizedlist>
842 <listitem>
843 <para>A newer feature called <emphasis role="bold">"nested
844 paging"</emphasis> implements some memory management in hardware,
845 which can greatly accelerate hardware virtualization since these
846 tasks no longer need to be performed by the virtualization
847 software.</para>
848
849 <para>With nested paging, the hardware provides another level of
850 indirection when translating linear to physical addresses. Page
851 tables function as before, but linear addresses are now translated
852 to "guest physical" addresses first and not physical addresses
853 directly. A new set of paging registers now exists under the
854 traditional paging mechanism and translates from guest physical
855 addresses to host physical addresses, which are used to access
856 memory.</para>
857
858 <para>Nested paging eliminates the overhead caused by VM exits and
859 page table accesses. In essence, with nested page tables the guest
860 can handle paging without intervention from the hypervisor. Nested
861 paging thus significantly improves virtualization
862 performance.</para>
863
864 <para>On AMD processors, nested paging has been available starting
865 with the Barcelona (K10) architecture -- they call it now "rapid
866 virtualization indexing" (RVI). Intel added support for nested
867 paging, which they call "extended page tables" (EPT), with their
868 Core i7 (Nehalem) processors.</para>
869
870 <para>If nested paging is enabled, the VirtualBox hypervisor can
871 also use <emphasis role="bold">large pages</emphasis> to reduce TLB
872 usage and overhead. This can yield a performance improvement of up
873 to 5%. To enable this feature for a VM, you need to use the
874 <computeroutput>VBoxManage modifyvm
875 </computeroutput><computeroutput>--largepages</computeroutput>
876 command; see <xref linkend="vboxmanage-modifyvm" />.</para>
877 </listitem>
878
879 <listitem>
880 <para>On Intel CPUs, another hardware feature called <emphasis
881 role="bold">"Virtual Processor Identifiers" (VPIDs)</emphasis> can
882 greatly accelerate context switching by reducing the need for
883 expensive flushing of the processor's Translation Lookaside Buffers
884 (TLBs).</para>
885
886 <para>To enable these features for a VM, you need to use the
887 <computeroutput>VBoxManage modifyvm --vtxvpid</computeroutput> and
888 <computeroutput>--largepages</computeroutput> commands; see <xref
889 linkend="vboxmanage-modifyvm" />.</para>
890 </listitem>
891 </itemizedlist></para>
892 </sect1>
893</chapter>
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette