freebsd-nq/sys/dev/hyperv/vmbus
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
..
amd64 PTI for amd64. 2018-01-17 11:44:21 +00:00
i386 PTI for amd64. 2018-01-17 11:44:21 +00:00
hyperv_busdma.c hyperv/busdma: Take BUS_DMA_ZERO into account 2016-05-24 05:26:52 +00:00
hyperv_machdep.h hyperv: Move machine dependent bits into machine dependent files. 2016-06-06 05:55:37 +00:00
hyperv_reg.h hyperv: Allow userland to ro-mmap reference TSC page 2016-12-15 03:32:24 +00:00
hyperv_var.h hyperv/timesync: Support "sent TC" to improve accuracy. 2016-12-08 05:37:39 +00:00
hyperv.c hyperv/vmbus: Expose Hyper-V major version. 2017-10-10 08:23:19 +00:00
vmbus_br.c hyperv/vmbus: Add __FBSDID 2016-10-14 05:47:59 +00:00
vmbus_brvar.h hyperv/vmbus: Add functions to test RX/TX bufring emptiness 2016-09-28 04:08:20 +00:00
vmbus_chan.c hyperv/vmbus: Nuke unnecessary critical sections. 2016-12-28 03:07:58 +00:00
vmbus_chanvar.h hyperv/vmbus: Add channel polling support. 2016-12-12 05:04:55 +00:00
vmbus_et.c hyperv: Update copyright for the files changed in 2017 2017-08-14 06:00:50 +00:00
vmbus_if.m hyperv/vmbus: Add DEVMETHOD to map cpu to event taskq. 2016-11-30 07:45:05 +00:00
vmbus_reg.h hyperv/vmbus: Stringent header length and total length check. 2016-09-05 03:21:31 +00:00
vmbus_res.c hyperv/vmbus: Reorganize vmbus device tree 2017-05-10 05:28:14 +00:00
vmbus_var.h hyperv/vmbus: Nuke unnecessary critical sections. 2016-12-28 03:07:58 +00:00
vmbus_xact.c hyperv/vmbus: Add result polling support for xact API. 2016-11-28 07:27:08 +00:00
vmbus.c PTI for amd64. 2018-01-17 11:44:21 +00:00