Modern computers employ virtual memory to decouple processes, e.g., applications running on top of an operating system, from the physical memory addresses backing the address space of the processes. Using virtual memory enables processes to have a large contiguous address space, and allows the computer to run more processes than can fit simultaneously in their entirety in the available physical memory (i.e., to “over-commit” memory). To do this, virtual memory space is divided into pages of a fixed size (for example, x86 architectures use page sizes of 4 KB, 2 MB, or 1 GB), and each page of the virtual memory space may or may not be mapped onto a page within the physical memory of the same page size. Much of the description in this patent will be in terms of x86 architectures. However, a person of skill in the art will understand how to apply the teachings of the invention to other processor architectures.
Translation of a virtual memory address to a physical memory address is done by traversing page tables in memory that contain mapping information. To speed up translation, a translation look-aside buffer (TLB) is typically used. The TLB provides faster translation of virtual addresses to physical addresses than does accessing page tables in memory because the TLB can provide the beginning-to-end mapping in a single step, and because the TLB can be implemented in a small (and, therefore, fast to access) data structure closer to or in the CPU itself. However, the TLB is limited in size and it is possible that a virtual memory page cannot be found in the TLB. Whenever this happens, a “TLB miss” occurs, and the mapping must be performed by a traversal of the page tables, commonly known as a “page walk,” a much slower process than look-ups in the TLB.
In virtualized computer systems, where multiple virtual machines, each having an operating system and applications running therein, can be configured to run on a single hardware platform, memory management for the virtual machines is carried out by the emulated memory management units (MMUs). One emulated MMU is provided for each virtual machine and the emulated MMU manages the mappings of guest virtual addresses directly to physical memory addresses, also referred to as machine memory addresses, using shadow page tables. Shadow page tables have the same structure as conventional page tables and, as with conventional page tables, shadow page tables need not be traversed if the guest virtual address that needs to be mapped has an entry in the TLB.
Memory management for the virtual machines may also be carried out by MMUs configured in CPUs that support nested page walks. In such systems, a first set of page tables, referred to herein as guest page tables (gPTs), map the virtual address space of each application running in the virtual machines, referred to herein as guest virtual address space, to a physical address space that has been emulated for a virtual machine, referred to herein as guest physical address space. Additionally, a second set of page tables, referred to herein as nested page tables (NPTs) (also known as extended page tables), map the guest physical address space to the address space of machine memory, referred to herein as machine memory address space. Both the first and second sets of page tables are hierarchically arranged and a pointer to the top-level, root table for each set of page tables is stored in a distinct register. In x86 architectures that support nested page walks, the register that stores a pointer to the root table of the gPTs is known as the gCR3 register and the register that stores a pointer to the root table of the NPTs is known as the nCR3 register. It should be recognized that non-x86 architectures employing guest page tables and nested page tables, or the like, may have different structures and accessed in a different manner.
The mapping begins with the guest page walker module of MMU 101 retrieving a pointer to the root table of gPTs 110 from the gCR3 register, which is an address in the guest physical address space. Bits [47:39] of guest virtual address 100 and 3 trailing bits of zeros define the index into the root table and are copied into the 12 least significant bits of this guest physical address. The resulting guest physical address, known as the gL4 address, is translated into a machine memory address using the nested page walker module of MMU 101 and NPTs 120, and the translated address is used to retrieve an address of the next lower-level (L3) table, which is also an address in the guest physical address space. Bits [38:30] of guest virtual address 100 and 3 trailing bits of zeros define the index into this L3 table and are copied into the 12 least significant bits of this guest physical address. The resulting guest physical address, known as the gL3 address, is translated into a machine memory address using the nested page walker module of MMU 101 and NPTs 120, and the translated address is used to retrieve an address of the next lower-level (L2) table, which is also an address in the guest physical address space. Bits [29:21] of guest virtual address 100 and 3 trailing bits of zeros define the index into this L2 table and are copied into the 12 least significant bits of this guest physical address. The resulting guest physical address, known as the gL2 address, is translated into a machine memory address using the nested page walker module of MMU 101 and NPTs 120, and the translated address is used to retrieve an address of the next lower-level (L1) table, which is also an address in the guest physical address space. Bits [20:12] of guest virtual address 100 and 3 trailing bits of zeros define the index into this L1 table and are copied into the 12 least significant bits of this guest physical address. The resulting guest physical address, known as the gL1 address, is translated into a machine memory address using the nested page walker module of MMU 101 and NPTs 120, and the translated address is used to retrieve an address of a data page, which is also an address in the guest physical address space. Bits [11:0] of guest virtual address 100 define the index into this data page and are copied into the 12 least significant bits of this guest physical address. The resulting guest physical address, known as the gPA address, is translated into a machine memory address using the nested page walker module of MMU 101 and NPTs 120, and the translated address is used to retrieve the desired content, i.e., data 150.
Copy-on-write (COW) is a commonly used optimization technique that operates at the granularity of pages. As mentioned above, x86 architectures employ page sizes of 4 KB, 2 MB, or 1 GB, and therefore COW may be implemented in x86 architectures using 4 KB page sizes, 2 MB pages sizes, or 1 GB page sizes. However, 4 KB page size may be finer granularity than what is needed in common workloads. A larger page size would be desirable because it would lower the pressure on the TLB. A 2 MB page size, however, is too coarse and cannot be easily shared to realize the benefits from COW optimization techniques. In addition, once a page is allocated as COW, a write to that page, even a single byte write, would force the entire page to be copied. From a computational efficiency point of view, smaller page sizes for COW optimization would be preferred because the computational overhead associated with such an event increases with larger page sizes. It may be possible to build x86 page tables with page sizes between 4 KB and 2 MB, but this might not be desirable because such a change could affect memory system performance adversely in other ways or be an implementation burden.
One or more embodiments of the present invention provide techniques for increasing the granularity of page sizes so that page sizes can be selected according to particular use cases. In the use case of COW optimization, one or more embodiments of the present invention permit page sizes to be set statically between 4 KB and 2 MB, e.g., 64 KB, which has been empirically determined as a preferred page size for COW optimization, or configured dynamically among multiple page sizes.
A data structure according to an embodiment of the present invention includes a set of tables that are hierarchically arranged, the set of tables including a root table and a plurality of lower-level tables. One entry of the lower-level tables references a physical memory page, also referred to herein as machine memory page, in common with at least one other entry of the lower-level tables, and the entries each have an address value of the physical memory page and a bit value indicating that the address value is referencing a data page and not a location of another lower-level table.
A data structure according to another embodiment of the present invention is used by a memory management unit (MMU) of a computer system having virtual machines instantiated therein, where the MMU is configured to execute a first mapping from a guest virtual address space to a guest physical address space and a second mapping from the guest physical address space to a machine memory address space. The data structure includes a set of first mapping tables that are hierarchically arranged and define mappings between guest virtual addresses and guest physical addresses, and a set of second mapping tables that are hierarchically arranged and define mappings between guest physical addresses and machine memory addresses, the set of second mapping tables including a root table and a plurality of lower-level tables. One entry of the lower-level tables references a machine memory page in common with at least one other entry of the lower-level tables, and the entries each have an address value of the machine memory page and a bit value indicating that the address value is referencing a data page and not a location of another lower-level table.
A method of performing a write operation in a computer system having virtual machines instantiated therein, according to an embodiment of the present invention, includes the steps of receiving a write operation from a virtual machine, extracting a write target address from the write operation, modifying a binary representation of the write target address by copying the value of a first bit of the binary representation to a second bit of the binary representation, wherein the second bit is more significant than the first bit, translating the write target address to a machine memory address using the modified binary representation, determining that the write operation at the machine memory address is not permitted, copying the contents at the machine memory address to a different machine memory address and designating the different machine memory address to be writable, and performing the write operation at the different memory address.
Further embodiments of the present invention include, without limitation, a non-transitory computer-readable storage medium that includes instructions that enable a processing unit to implement one or more aspects of the above methods as well as a computer system configured to implement one or more aspects of the above methods.
It should be recognized that the various terms, layers and categorizations used to describe the virtualization components in
The MMU (e.g., MMU 231a) obtains the location of root table 421 by accessing a register 420 (e.g., CR3 register in x86 architectures). Then, successive 9-bit segments of the binary representation of the virtual address are used to index into the page tables at different levels. In the embodiment described herein, where the 2 MB page is divided into 1 MB pages that are separately addressable, the 20 least significant bits of the binary representation of the virtual address are used as an offset into data page 425. Bits [63:48] are not used in the mapping in this embodiment as is the convention in memory management schemes for x86 architectures.
The structure of the page tables shown in
If M is one, as is the case when a 2 MB page is divided into two 1 MB pages, corresponding entries from two different third-level tables reference different portions of the same data page, the entry from the third-level table corresponding to a value of zero at bit [20] referencing the bottom half of the data page and the entry from the third-level table corresponding to a value of one at bit [20] referencing the top half of the data page. Thus, for example, referring to
If M is two, corresponding entries from four third-level tables reference different portions of the same data page and each such entry is associated with a distinct quarter section of the data page, each such section having a size of 512 KB. More generally, if M is N, corresponding entries from 2N third-level tables reference different portions of the same data page and each such entry is associated with a distinct 1/2N section of the data page, each such section having a size of 2 MB/2N. Thus, if M is 5, the 2 MB page would be divided into 32 separately addressable 64 KB sections.
It should be recognized that the page sizes can be changed according to particular use cases by changing the value of M. For example, in the case of COW optimization, if it is determined that the efficiency gained from COW optimization is small, the value of M may be increased. However, the larger values of M and thus, smaller page sizes, may increase the TLB pressure too much. Therefore, if the TLB pressure is too high, the value of M may be decreased accordingly until a good balance between COW optimization and TLB pressure is achieved.
In
In an alternative embodiment, a hardware configuration register, whose bits [47:21] can be set or cleared by hypervisor 210, is provided. For each bit that is set in this bit vector, the corresponding bit of the virtual address is claimed as an alias bit. So if M bits are set in this configuration register, there are 2M aliases. The hardware will then copy bits [20:(20-(M−1))] into the bit positions of the virtual address corresponding to the bits that are set in the hardware configuration register, from highest-to-lowest. The bits that are set to 1 in the hardware configuration register need not be contiguous.
In a further embodiment of the present invention, the check for permitting aliasing can be configurable. In this further embodiment, a pair of hardware registers is provided to specify the aliasing acceptance requirements for each bit that is claimed as an aliasing bit. For each aliasing bit M, the two corresponding bits in these hardware registers can be configured to select from the following rules: (00) bit M must be zero; (01) bit M must match sign bit [47]; (10) bit M must match the inverse of sign bit [47]; and (11) bit M must be one.
At step 510, the MMU extracts the guest virtual address contained in the guest memory access in binary form. At step 512, the MMU checks to see if the creation of 2M aliased virtual address regions within the page tables is permitted by confirming that bits [46:46-(M−1)] of the guest virtual address, hereinafter called “virtual address bits for aliasing,” have values equal to the sign bit at bit [47] of the guest virtual address. If the virtual address bits for aliasing do not have values equal to the sign bit, aliasing is not permitted and the method proceeds to step 514, where a page fault occurs and normal guest memory access is performed after the virtual address translation is carried out by the VMM. The method terminates after step 514. If the virtual address bits for aliasing have values that are equal to the sign bit, aliasing is permitted and the method proceeds to step 515, where the values of M most significant bits from bits [20:0] are respectively copied into bit [46] to bit [46-(M−1)].
The TLB is checked at step 518 to see if it contains a mapping for the modified virtual address, in particular bits [47:21] of the modified virtual address. In embodiments where the TLB is implemented as a direct mapping TLB or set associative TLB, and a separate TLB is configured for each of the different page sizes handled by MMU 231a, a separate check is made for each of the different TLBs. If the TLB contains the mapping, step 520 is carried out where the MMU retrieves the machine address that the TLB associates with the guest virtual address and the method continues onto the decision block at step 524. If the TLB misses at step 518, the MMU, at step 522, traverses the page tables to obtain the machine address associated with the guest virtual address and updates the TLB to associate the machine address obtained from the page walk to the guest virtual address. The method then continues onto the decision block at step 524. In the embodiment described herein, it should be recognized that the machine address retrieved from the TLB at step 520 or obtained from the page walk at step 522 represents the start of a data page having the size of 2 MB.
At step 524, the MMU determines whether or not the guest memory access is a write memory access to a portion of the data page (referred to hereinafter as a “sub-page”) that has been designated copy-on-write by examining the read/write bit of the entry in the TLB or the page table, that includes a pointer to (i.e., an address of) the data page. If the read/write bit is set as ‘1’ indicating that a write to the sub-page is permitted, step 525 is carried out and the write memory access is carried out in the normal manner. On the other hand, if the read/write bit is set as ‘0’ indicating that a write to the sub-page is not permitted, step 526 is carried out. At step 526, the VMM causes the contents of the sub-page that is read-only to be copied to a new sub-page memory location, and the pointer to the sub-page stored in the page tables and the TLB is changed to the address of the new sub-page memory location. In addition, R/W bit 443 of the corresponding page table and TLB entry is changed from ‘0’ to ‘1’ and a write memory access is carried out on the new sub-page.
Page tables depicted in
The structure of nested page tables shown in
In one embodiment of the present invention, when aliasing is permitted, values of M bits of the guest physical address are respectively copied into bits [47:47-(M−1)] of the guest physical address. For example, if M is two, the values of bits [20:19] from the guest physical address are copied into bits [47:46]. This copying is performed by MMU 231b. Thus, for example, existing x86 processors may be modified to perform this copying when performing memory accesses. This copying creates 2M aliased physical address regions within the nested page tables. The contents of corresponding entries of bottom-level tables in multiple aliased physical address regions are configured to reference the same machine memory page (e.g., bottom-level tables 624g, 624h reference machine memory page 625D in
In the page table data structure illustrated in
In an alternative embodiment, a hardware configuration register, whose bits [47:21] can be set or cleared by hypervisor 210, is provided. For each bit that is set in this bit vector, the corresponding bit of the guest physical address is claimed as an alias bit. So if M bits are set in this configuration register, there are 2M aliases. The hardware will then copy bits [20:20-(M−1)] into the bit positions of the guest physical address corresponding to the bits that are set in the hardware configuration register, from highest-to-lowest. The bits that are set to 1 in the hardware configuration register need not be contiguous.
At step 710, the MMU extracts the guest virtual address contained in the guest memory access in binary form. The TLB is then checked at step 714. If there is a TLB hit, the corresponding machine address is retrieved from the TLB (step 727) and step 728 is executed thereafter. If there is a TLB miss, the loop including steps 716, 718, 720, 722, and 724 is executed to carry out the guest and nested page table walks. After the guest and nested page walks, the TLB is updated at step 726. At step 728, the MMU determines whether or not the guest memory access is a write memory access to a portion of the data page (referred to hereinafter as a “sub-page”) that has been designated copy-on-write by examining the read/write bit of the entry in the TLB or the page table, that includes a pointer to (i.e., an address of) the data page. If the read/write bit is set as ‘1’ indicating that a write to the sub-page is permitted, step 729 is carried out and the write memory access is carried out in the normal manner. On the other hand, if the read/write bit is set as ‘0’ indicating that a write to the sub-page is not permitted, step 730 is carried out. At step 730, the VMM causes the read-only portion of the contents of the sub-page to be copied to a new sub-page memory location, and the pointer to the sub-page stored in the page tables and the TLB is changed to the address of the new sub-page memory location. In addition, R/W bit 443 of the corresponding page table and TLB entry is changed from ‘0’ to ‘1’ and a write memory access is carried out on the new sub-page.
The guest and nested page walks begin with the MMU accessing the guest physical address stored in gCR3 at step 716. During subsequent passes through the loop, the guest physical addresses are retrieved from machine memory addresses obtained from the nested page walks carried out at step 722. At step 718, the MMU examines bits [47:47-(M−1)] of the guest physical address retrieved at step 716 for zeroes. If one or more of the bits [47:47-(M−1)] are not zero, the MMU issues a page fault and the guest memory access is performed in a conventional manner (step 750), and the method terminates thereafter. On the other hand, if bits [47:47-(M−1)] are all zeroes, the MMU copies bit values at bits [20:20-(M−1)] into bits [47:47 -(M−1)] at step 720 to produce the guest physical address that is to be translated into a machine address through the nested page walk at step 722. After nested page walk is executed at step 722, step 724 is carried out to determine if the guest and nested page walks are complete (e.g., the machine memory address of the data page corresponding to the guest virtual address to be mapped is obtained). If the guest and nested page walks are not complete, the flow returns to step 716 where the next guest physical address to be mapped is retrieved. If the guest and nested page walks are complete, the TLB is updated at step 726, and step 728 is executed thereafter as described above.
The various embodiments described herein may employ various computer-implemented operations involving data stored in computer systems. For example, these operations may require physical manipulation of physical quantities which usually, though not necessarily, take the form of electrical or magnetic signals where they, or representations of them, are capable of being stored, transferred, combined, compared, or otherwise manipulated. Further, such manipulations are often referred to in terms, such as producing, identifying, determining, or comparing. Any operations described herein that form part of one or more embodiments of the invention may be useful machine operations. In addition, one or more embodiments of the invention also relate to a device or an apparatus for performing these operations. The apparatus may be specially constructed for specific required purposes, or it may be a general purpose computer selectively activated or configured by a computer program stored in the computer. In particular, various general purpose machines may be used with computer programs written in accordance with the description provided herein, or it may be more convenient to construct a more specialized apparatus to perform the required operations.
The various embodiments described herein may be practiced with other computer system configurations including hand-held devices, microprocessor systems, microprocessor-based or programmable consumer electronics, minicomputers, mainframe computers, and the like.
One or more embodiments of the present invention may be implemented as one or more computer programs or as one or more computer program modules embodied in one or more computer readable media. The term computer readable medium refers to any data storage device that can store data which can thereafter be input to a computer system; computer readable media may be based on any existing or subsequently developed technology for embodying computer programs in a manner that enables them to be read by a computer. Examples of a computer readable medium include a hard drive, network attached storage (NAS), read-only memory, random-access memory (e.g., a flash memory device), a CD-ROM (Compact Disc-ROM), a CD-R, or a CD-RW, a DVD (Digital Versatile Disc), a magnetic tape, and other optical and non-optical data storage devices. The computer readable medium can also be distributed over a network coupled computer system so that the computer readable code is stored and executed in a distributed fashion.
Although one or more embodiments of the present invention have been described in some detail for clarity of understanding, it will be apparent that certain changes and modifications may be made within the scope of the claims. Accordingly, the described embodiments are to be considered as illustrative and not restrictive, and the scope of the claims is not to be limited to details given herein, but may be modified within the scope and equivalents of the claims. In the claims, elements and/or steps do not imply any particular order of operation, unless explicitly stated in the claims.
Plural instances may be provided for components, operations or structures described herein as a single instance. Finally, boundaries between various components, operations and data stores are somewhat arbitrary, and particular operations are illustrated in the context of specific illustrative configurations. Other allocations of functionality are envisioned and may fall within the scope of the invention(s). In general, structures and functionality presented as separate components in exemplary configurations may be implemented as a combined structure or component. Similarly, structures and functionality presented as a single component may be implemented as separate components. These and other variations, modifications, additions, and improvements may fall within the scope of the appended claims(s).