freebsd-dev/lib
Neel Natu 318224bbe6 Merge projects/bhyve_npt_pmap into head.
Make the amd64/pmap code aware of nested page table mappings used by bhyve
guests. This allows bhyve to associate each guest with its own vmspace and
deal with nested page faults in the context of that vmspace. This also
enables features like accessed/dirty bit tracking, swapping to disk and
transparent superpage promotions of guest memory.

Guest vmspace:
Each bhyve guest has a unique vmspace to represent the physical memory
allocated to the guest. Each memory segment allocated by the guest is
mapped into the guest's address space via the 'vmspace->vm_map' and is
backed by an object of type OBJT_DEFAULT.

pmap types:
The amd64/pmap now understands two types of pmaps: PT_X86 and PT_EPT.

The PT_X86 pmap type is used by the vmspace associated with the host kernel
as well as user processes executing on the host. The PT_EPT pmap is used by
the vmspace associated with a bhyve guest.

Page Table Entries:
The EPT page table entries as mostly similar in functionality to regular
page table entries although there are some differences in terms of what
bits are used to express that functionality. For e.g. the dirty bit is
represented by bit 9 in the nested PTE as opposed to bit 6 in the regular
x86 PTE. Therefore the bitmask representing the dirty bit is now computed
at runtime based on the type of the pmap. Thus PG_M that was previously a
macro now becomes a local variable that is initialized at runtime using
'pmap_modified_bit(pmap)'.

An additional wrinkle associated with EPT mappings is that older Intel
processors don't have hardware support for tracking accessed/dirty bits in
the PTE. This means that the amd64/pmap code needs to emulate these bits to
provide proper accounting to the VM subsystem. This is achieved by using
the following mapping for EPT entries that need emulation of A/D bits:
               Bit Position           Interpreted By
PG_V               52                 software (accessed bit emulation handler)
PG_RW              53                 software (dirty bit emulation handler)
PG_A               0                  hardware (aka EPT_PG_RD)
PG_M               1                  hardware (aka EPT_PG_WR)

The idea to use the mapping listed above for A/D bit emulation came from
Alan Cox (alc@).

The final difference with respect to x86 PTEs is that some EPT implementations
do not support superpage mappings. This is recorded in the 'pm_flags' field
of the pmap.

TLB invalidation:
The amd64/pmap code has a number of ways to do invalidation of mappings
that may be cached in the TLB: single page, multiple pages in a range or the
entire TLB. All of these funnel into a single EPT invalidation routine called
'pmap_invalidate_ept()'. This routine bumps up the EPT generation number and
sends an IPI to the host cpus that are executing the guest's vcpus. On a
subsequent entry into the guest it will detect that the EPT has changed and
invalidate the mappings from the TLB.

Guest memory access:
Since the guest memory is no longer wired we need to hold the host physical
page that backs the guest physical page before we can access it. The helper
functions 'vm_gpa_hold()/vm_gpa_release()' are available for this purpose.

PCI passthru:
Guest's with PCI passthru devices will wire the entire guest physical address
space. The MMIO BAR associated with the passthru device is backed by a
vm_object of type OBJT_SG. An IOMMU domain is created only for guest's that
have one or more PCI passthru devices attached to them.

Limitations:
There isn't a way to map a guest physical page without execute permissions.
This is because the amd64/pmap code interprets the guest physical mappings as
user mappings since they are numerically below VM_MAXUSER_ADDRESS. Since PG_U
shares the same bit position as EPT_PG_EXECUTE all guest mappings become
automatically executable.

Thanks to Alan Cox and Konstantin Belousov for their rigorous code reviews
as well as their support and encouragement.

Thanks for John Baldwin for reviewing the use of OBJT_SG as the backing
object for pci passthru mmio regions.

Special thanks to Peter Holm for testing the patch on short notice.

Approved by:	re
Discussed with:	grehan
Reviewed by:	alc, kib
Tested by:	pho
2013-10-05 21:22:35 +00:00
..
atf
clang Add LLDB bmake infrastructure 2013-09-20 01:52:02 +00:00
csu Add an elf note on ARM to store the MACHINE_ARCH an executable was built 2013-09-26 07:53:18 +00:00
libalias
libarchive Temporarily disable iconv for non-shared library builds. The dynamic 2013-09-26 17:55:36 +00:00
libauditd
libbegemot
libblocksruntime
libbluetooth
libbsm
libbsnmp
libbz2
libc accept(2): Update portability note for accept4(). 2013-10-01 21:17:18 +00:00
libc++ Add a c++/v1/tr1 include directory containing symlinks to all of the standard 2013-09-04 15:02:14 +00:00
libcalendar
libcam Fix a typo: XPORT_SPI should be tested against transport, nor protocol. 2013-06-03 21:52:19 +00:00
libcom_err
libcompat
libcompiler_rt Unconditionally compile the __sync_* atomics support functions into compiler-rt 2013-08-31 08:50:45 +00:00
libcrypt
libcxxrt
libdevinfo
libdevstat
libdwarf When decoding SLEB128, make sure sign extension is performed for 2013-06-30 21:06:47 +00:00
libedit Import NetBSD readline.c,v 1.104: do not crash with add_history(NULL). 2013-09-26 17:54:58 +00:00
libefi
libelf Add support to the ARM platform specific section types. 2013-08-31 18:13:20 +00:00
libexecinfo Don't install private libexecinfo headers 2013-09-03 13:31:43 +00:00
libexpat
libfetch Even though it doesn't really make sense in the context of a CONNECT 2013-08-22 07:43:36 +00:00
libgeom Since there's no defined schema for the XML that's generated by the 2013-07-19 06:42:15 +00:00
libgpib
libgssapi
libiconv
libiconv_compat Turn off warns for this do-nothing file. clang noticed. 2013-08-13 08:12:57 +00:00
libiconv_modules Use Makefile.inc instead of .export. 2013-09-06 10:40:38 +00:00
libipsec Improve compatibility with recent flex from flex.sourceforge.net. 2013-05-03 23:51:32 +00:00
libipx
libjail
libkiconv The iconv in libc did two things - implement the standard APIs, the GNU 2013-08-13 07:15:01 +00:00
libkse
libkvm In r227839, when removing libkvm dependency on procfs(5), 2013-07-10 19:44:43 +00:00
libldns LDNS needs OpenSSL. This wasn't a problem as long as it was only build 2013-09-08 19:39:18 +00:00
liblzma
libmagic
libmandoc
libmd
libmemstat Refine UMA bucket allocation to reduce space consumption and improve 2013-06-18 04:50:20 +00:00
libmilter
libmp
libnandfs
libnetbsd
libnetgraph
libngatm
libopie
libpam Make libldns and libssh private. 2013-09-08 10:04:26 +00:00
libpcap MFV: libpcap 1.4.0. 2013-05-30 08:02:00 +00:00
libpmc Always install pmc.foo(3) manpages. 2013-05-03 08:20:10 +00:00
libproc Fix the return value when we found a symbol in .dynstr. This nasty bug was 2013-08-10 07:39:15 +00:00
libprocstat Change the cap_rights_t type from uint64_t to a structure that we can extend 2013-09-05 00:09:56 +00:00
libradius mdoc sweep. 2013-05-12 22:22:12 +00:00
librpcsec_gss
librpcsvc
librt Convert this piece of code to use C11 atomics. 2013-06-30 08:59:33 +00:00
librtld_db
libsbuf
libsdp
libsm
libsmb
libsmdb
libsmutil
libstand Move pos++ out of the complicated equation, introduced at r240780. 2013-07-01 17:23:13 +00:00
libstdbuf
libstdthreads
libtacplus mdoc sweep. 2013-05-12 22:22:12 +00:00
libtelnet
libthr The SUSv4tc1 requires that pthread_setcancelstate() shall be not a 2013-06-19 04:47:41 +00:00
libthread_db Ensure we set all fpu registers to zero by using the address and size of 2013-08-17 14:42:40 +00:00
libufs
libugidfw
libulog
libunbound Build and install the Unbound caching DNS resolver daemon. 2013-09-15 14:51:23 +00:00
libusb Correct two comments. 2013-09-05 12:21:11 +00:00
libusbhid
libutil The round of expand_number() cleanups. 2013-08-30 11:21:52 +00:00
libvgl
libvmmapi Merge projects/bhyve_npt_pmap into head. 2013-10-05 21:22:35 +00:00
libwrap
liby
libyaml
libypclnt
libz MFV: Update zlib to 1.2.8. 2013-05-05 06:20:49 +00:00
msun After r255294, building lib/msun's symbol map (using clang as the 2013-09-12 20:51:48 +00:00
ncurses Convert old make variable modifiers :U and :L to bmake :tu and :tl. 2013-06-02 11:44:23 +00:00
Makefile Remove BIND. 2013-09-30 17:23:45 +00:00
Makefile.inc