freebsd-nq/sys/x86/include
Konstantin Belousov bd50262f70 PTI for amd64.
The implementation of the Kernel Page Table Isolation (KPTI) for
amd64, first version. It provides a workaround for the 'meltdown'
vulnerability.  PTI is turned off by default for now, enable with the
loader tunable vm.pmap.pti=1.

The pmap page table is split into kernel-mode table and user-mode
table. Kernel-mode table is identical to the non-PTI table, while
usermode table is obtained from kernel table by leaving userspace
mappings intact, but only leaving the following parts of the kernel
mapped:

    kernel text (but not modules text)
    PCPU
    GDT/IDT/user LDT/task structures
    IST stacks for NMI and doublefault handlers.

Kernel switches to user page table before returning to usermode, and
restores full kernel page table on the entry. Initial kernel-mode
stack for PTI trampoline is allocated in PCPU, it is only 16
qwords.  Kernel entry trampoline switches page tables. then the
hardware trap frame is copied to the normal kstack, and execution
continues.

IST stacks are kept mapped and no trampoline is needed for
NMI/doublefault, but of course page table switch is performed.

On return to usermode, the trampoline is used again, iret frame is
copied to the trampoline stack, page tables are switched and iretq is
executed.  The case of iretq faulting due to the invalid usermode
context is tricky, since the frame for fault is appended to the
trampoline frame.  Besides copying the fault frame and original
(corrupted) frame to kstack, the fault frame must be patched to make
it look as if the fault occured on the kstack, see the comment in
doret_iret detection code in trap().

Currently kernel pages which are mapped during trampoline operation
are identical for all pmaps.  They are registered using
pmap_pti_add_kva().  Besides initial registrations done during boot,
LDT and non-common TSS segments are registered if user requested their
use.  In principle, they can be installed into kernel page table per
pmap with some work.  Similarly, PCPU can be hidden from userspace
mapping using trampoline PCPU page, but again I do not see much
benefits besides complexity.

PDPE pages for the kernel half of the user page tables are
pre-allocated during boot because we need to know pml4 entries which
are copied to the top-level paging structure page, in advance on a new
pmap creation.  I enforce this to avoid iterating over the all
existing pmaps if a new PDPE page is needed for PTI kernel mappings.
The iteration is a known problematic operation on i386.

The need to flush hidden kernel translations on the switch to user
mode make global tables (PG_G) meaningless and even harming, so PG_G
use is disabled for PTI case.  Our existing use of PCID is
incompatible with PTI and is automatically disabled if PTI is
enabled.  PCID can be forced on only for developer's benefit.

MCE is known to be broken, it requires IST stack to operate completely
correctly even for non-PTI case, and absolutely needs dedicated IST
stack because MCE delivery while trampoline did not switched from PTI
stack is fatal.  The fix is pending.

Reviewed by:	markj (partially)
Tested by:	pho (previous version)
Discussed with:	jeff, jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
2018-01-17 11:44:21 +00:00
..
xen
_align.h spdx: initial adoption of licensing ID tags. 2017-11-18 14:26:50 +00:00
_inttypes.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
_limits.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
_stdint.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
_types.h Remove lint support from system headers and MD x86 headers. 2017-11-23 11:40:16 +00:00
acpica_machdep.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
apicreg.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
apicvar.h PTI for amd64. 2018-01-17 11:44:21 +00:00
apm_bios.h
bus_dma.h Clean up MD pollution of bus_dma.h: 2017-07-01 05:35:29 +00:00
bus.h amd64: Add a 48-bit MAXADDR constant 2018-01-13 17:55:22 +00:00
busdma_impl.h Add support for NUMA domains to bus dma tags. This causes all memory 2018-01-12 23:34:16 +00:00
cputypes.h Remove 'cpu' and 'cpu_class' on amd64. 2016-09-15 17:05:54 +00:00
dump.h
elf.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
endian.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
fdt.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
float.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
fpu.h Renumber copyright clause 4 2017-02-28 23:42:47 +00:00
frame.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
init.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
legacyvar.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
mca.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
metadata.h
mptable.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
ofw_machdep.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
pci_cfgreg.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
psl.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
ptrace.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
pvclock.h
reg.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
segments.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
setjmp.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
sigframe.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
signal.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
specialreg.h Enumerate and print Intel CPU features for Speculative Execution Side 2018-01-14 12:36:23 +00:00
stack.h
stdarg.h Introduce an architecture-agnostic <sys/_stdarg.h> to reduce 2017-12-25 20:54:00 +00:00
sysarch.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
trap.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
ucontext.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
vdso.h sys/x86: further adoption of SPDX licensing ID tags. 2017-11-27 15:11:47 +00:00
vmware.h
x86_smp.h SPDX: use the Beerware identifier. 2017-11-30 20:33:45 +00:00
x86_var.h PTI for amd64. 2018-01-17 11:44:21 +00:00