/*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2011 NetApp, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ /* * This file and its contents are supplied under the terms of the * Common Development and Distribution License ("CDDL"), version 1.0. * You may only use this file in accordance with the terms of version * 1.0 of the CDDL. * * A full copy of the text of the CDDL should have accompanied this * source. A copy of the CDDL is also available via the Internet at * http://www.illumos.org/license/CDDL. * * Copyright 2015 Pluribus Networks Inc. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #ifndef __FreeBSD__ #include #endif #include #include #include #include #include "ept.h" #define EPT_SUPPORTS_EXEC_ONLY(cap) ((cap) & (1UL << 0)) #define EPT_PWL4(cap) ((cap) & (1UL << 6)) #define EPT_MEMORY_TYPE_WB(cap) ((cap) & (1UL << 14)) #define EPT_PDE_SUPERPAGE(cap) ((cap) & (1UL << 16)) /* 2MB pages */ #define EPT_PDPTE_SUPERPAGE(cap) ((cap) & (1UL << 17)) /* 1GB pages */ #define INVEPT_SUPPORTED(cap) ((cap) & (1UL << 20)) #define AD_BITS_SUPPORTED(cap) ((cap) & (1UL << 21)) #define INVVPID_SUPPORTED(cap) ((cap) & (1UL << 32)) #define INVVPID_ALL_TYPES_MASK 0xF0000000000UL #define INVVPID_ALL_TYPES_SUPPORTED(cap) \ (((cap) & INVVPID_ALL_TYPES_MASK) == INVVPID_ALL_TYPES_MASK) #define INVEPT_ALL_TYPES_MASK 0x6000000UL #define INVEPT_ALL_TYPES_SUPPORTED(cap) \ (((cap) & INVEPT_ALL_TYPES_MASK) == INVEPT_ALL_TYPES_MASK) #define EPT_PWLEVELS 4 /* page walk levels */ #define EPT_ENABLE_AD_BITS (1 << 6) SYSCTL_DECL(_hw_vmm); SYSCTL_NODE(_hw_vmm, OID_AUTO, ept, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, NULL); static int ept_enable_ad_bits; static int ept_pmap_flags; int ept_init(int ipinum) { int use_hw_ad_bits, use_superpages, use_exec_only; uint64_t cap; cap = rdmsr(MSR_VMX_EPT_VPID_CAP); /* * Verify that: * - page walk length is 4 steps * - extended page tables can be laid out in write-back memory * - invvpid instruction with all possible types is supported * - invept instruction with all possible types is supported */ if (!EPT_PWL4(cap) || !EPT_MEMORY_TYPE_WB(cap) || !INVVPID_SUPPORTED(cap) || !INVVPID_ALL_TYPES_SUPPORTED(cap) || !INVEPT_SUPPORTED(cap) || !INVEPT_ALL_TYPES_SUPPORTED(cap)) return (EINVAL); ept_pmap_flags = ipinum & PMAP_NESTED_IPIMASK; use_superpages = 1; TUNABLE_INT_FETCH("hw.vmm.ept.use_superpages", &use_superpages); if (use_superpages && EPT_PDE_SUPERPAGE(cap)) ept_pmap_flags |= PMAP_PDE_SUPERPAGE; /* 2MB superpage */ use_hw_ad_bits = 1; TUNABLE_INT_FETCH("hw.vmm.ept.use_hw_ad_bits", &use_hw_ad_bits); if (use_hw_ad_bits && AD_BITS_SUPPORTED(cap)) ept_enable_ad_bits = 1; else ept_pmap_flags |= PMAP_EMULATE_AD_BITS; use_exec_only = 1; TUNABLE_INT_FETCH("hw.vmm.ept.use_exec_only", &use_exec_only); if (use_exec_only && EPT_SUPPORTS_EXEC_ONLY(cap)) ept_pmap_flags |= PMAP_SUPPORTS_EXEC_ONLY; return (0); } void ept_invalidate_mappings(ulong_t eptp) { hma_vmx_invept_allcpus((uintptr_t)eptp); } static int ept_pinit(pmap_t pmap) { return (pmap_pinit_type(pmap, PT_EPT, ept_pmap_flags)); } struct vmspace * ept_vmspace_alloc(vm_offset_t min, vm_offset_t max) { return (vmspace_alloc(min, max, ept_pinit)); } void ept_vmspace_free(struct vmspace *vmspace) { vmspace_free(vmspace); } uint64_t eptp(uint64_t pml4) { uint64_t eptp_val; eptp_val = pml4 | (EPT_PWLEVELS - 1) << 3 | PAT_WRITE_BACK; if (ept_enable_ad_bits) eptp_val |= EPT_ENABLE_AD_BITS; return (eptp_val); }